Communication Setup between Arduino Mega and ESP8266

Hello guys,
I have a Problem setting up Communication between my ESP8266 Module and my Arduino Mega. I got it working before, but i wanted to switch up my Communication Protocoll to a text based, comand like Protocoll with human readable commands instead of number combinations. I will put the Full Code as a File since its too long for here, but I will describe the basic Idea first. The ESP receives a command over a TCP Socket, which is working fine right now and forwards this TCp message to my Arduino, where it will be processed. The Arduino then sends a Message back to the ESP which forwards this response to the client of the TCP socket. The ESP therefore is just the middleman of communication. The Problem I am facing is that i cannot get the ESP to correctly receive the msg back from the Arduino.

The TCP String gets converted to a char array and send over the SerialTransfer Library by Power Broker to the Arduino to support up to 256 characters for future usage. Currently the Arduino just copies the char array into the txBuff of Serialtransfer and sends it back, but the ESP does not receive it correctly. (Cannot display it corretly using client.write() onto the TCP Socket)

Relevant Arduino Mega Code:
Object Definition:

// Serial Transfer of ESP
SerialTransfer myTransfer;

Inside Setup:

// Init Serial Connection
  Serial.begin(9600);
  Serial3.begin(115200);
  // Init Serial Transfer Instance
  myTransfer.begin(Serial3);

Relevant Method of Arduino Mega which gets called in loop().

void echoEspSerial() {
  char request[64];

  Serial.print(".");

  if(myTransfer.available()) {
    for(int i = 0; i < 64; i++) {
      request[i] = myTransfer.rxBuff[i];
      myTransfer.txBuff[i] = request[i];
    }

    Serial.println();
    Serial.println(request);

    // myTransfer.txBuff[63] = '\n';

    myTransfer.sendData(64);
  }
}

All of the ESP Code is relevant and i cannot Copy all of it into this post sop please just llok at the Files. The Full Arduino Mega Code is there as well.

If there is any questions left, just aks please.

Thanks in advance for any help!

esp_sketch.ino (2.38 KB)

megaSketch.ino (16.6 KB)

Here is the Code of the ESP8266:

// Library for ESP8266 Support
#include <ESP8266WiFi.h>
// Wifi Client Library
#include <WiFiClient.h>
// Include SerialTransfer
#include <SerialTransfer.h>

#include <FireTimer.h>


#define MILLIS_UART 2000

#define DEBUG true

#define BUFFER_SIZE 64

// WiFi Data
const char* ssid = "Martin Router King";
const char* password = "t030599f080799";

// WiFi Objects
WiFiClient client;
WiFiServer server(1026);

// SerialTransfer Object
SerialTransfer myTransfer;

FireTimer uartTimer;




void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  myTransfer.begin(Serial);

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  if(DEBUG) Serial.print("Waiting to connect");

  while(WiFi.status() != WL_CONNECTED) {
    delay(100);
    if(DEBUG) Serial.print(".");
  }

  if(DEBUG) {
    Serial.print("\nConnected to: "); Serial.println(ssid);
    Serial.print("IP Adress: "); Serial.println(WiFi.localIP());
  }

  server.begin();

  digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
  readTCP();
}

void readTCP() {
  client = server.available();

  if(client && client.connected()) {
    client.write("Client Connected");
    // Read TCP Input as String
    String request = client.readStringUntil('\n');
    char requestChar[BUFFER_SIZE];
    request.toCharArray(requestChar, BUFFER_SIZE);

    // client.write("\nJust copying reequest\n");
    // client.write(request);

    // Send TCP MSG as char Array to Arduino
    for(int i = 0; i < BUFFER_SIZE; i++) {
      myTransfer.txBuff[i] = requestChar[i];
    }
    myTransfer.sendData(BUFFER_SIZE);

    uartTimer.begin(MILLIS_UART);

    // Wait for Arduino to respond
    while(!myTransfer.available()) {
      if(uartTimer.fire()) {
        client.write("Failed to get response");
        break;
      }
    }

    // Recv Message from Arduino and send it to TCp Socket
    writeTCP(client);

    delay(1000);

    // Clear Buffer and disconnect Client
    client.flush();
    client.stop();
  }
}

void writeTCP(WiFiClient client) {
  char reply[BUFFER_SIZE];
  if(myTransfer.available()) {
      for(int i = 0; i < BUFFER_SIZE; i++) {
        reply[i] = myTransfer.rxBuff[i];
      }

      // Actually makes a difference
      // ESP failes to get response otherwise
      reply[BUFFER_SIZE - 1] = '\n';
  }

  client.write(reply);
}