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
  // Init Serial Transfer Instance

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

void echoEspSerial() {
  char request[64];


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


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


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() {
  WiFi.begin(ssid, password);


  digitalWrite(LED_BUILTIN, HIGH);

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

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

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


  digitalWrite(LED_BUILTIN, LOW);

void loop() {

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];


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

    // Recv Message from Arduino and send it to TCp Socket


    // Clear Buffer and disconnect Client

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';