2 x ESP8266 + 2 x Arduino - Send strings from server to client

I'm using an Arduino MEGA board (1) connected to an ESP8266 acting as server and another board (2) with the ESP8266 acting as client.

The first Arduino MEGA collects data from sensors and sends a string over the ESP8266 towards the client. Each string starts with # and ends with ";".

This is the code for the ESP8266 used as server:

#include <SPI.h>
#include <ESP8266WiFi.h>

char ssid[] = "xxx"; 
char pass[] = "xxxx";               
WiFiServer server(80);
                    
IPAddress ip(192, 168, 1, 4);  //Reserved Ip address for your server            
IPAddress gateway(192,168,1,1);  //Gateway of your network          
IPAddress subnet(255,255,255,0);  //Subnet mask of your network         

void setup() {
  Serial.begin(115200);                   
  WiFi.config(ip, gateway, subnet);  //Sets configuration for WiFi network       
  WiFi.begin(ssid, pass);            //Connects to network     
  while (WiFi.status() != WL_CONNECTED) {  //If not connected to network
    Serial.println("Not Connected!");      //Print this
    delay(500);
  }
  Serial.println("Connected - I'm the server!"); //When connected print this
  server.begin();                   //Starts server      
}

void loop () {
  WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {
      String request = client.readStringUntil('\r');    
      client.flush();
      client.println(Serial.read()); //Sends data received from Arduino
    }
    client.stop();   //Terminates the connection with the client             
  }
}

This is the code on the ESP8266 used as client:

#include <SPI.h>
#include <ESP8266WiFi.h>

char ssid[] = "xxx";     
char pass[] = "xxxx";  
         
IPAddress server(192,168,1,4);  //connect to reserved Ip address for your server      
WiFiClient client;

void setup() {
  Serial.begin(115200);               
  WiFi.begin(ssid, pass);     //Connects to network       
  while (WiFi.status() != WL_CONNECTED) { //If not connected to network
    Serial.println("Not Connected!");     //Print this
    delay(500);
  }
  Serial.println("Connected - I'm the client!");  //When connected print this
}

void loop () {
  client.connect(server, 80);    //Connects to server host 
  client.println("Hello\r");  //sends message for debugging
  String answer = client.readStringUntil('\r'); // received and reads server data
  answer = answer.toInt();
  if(answer != "-1"){     //same as if(Serial.available())
    Serial.println(answer);  //Prints answer for Arduino to recieve
  }
  client.flush();         
}

This is the code I use on the first Arduino to send the string:

 String stringa = "#";
    stringa.concat(percentage15);
    stringa.concat(",");
    stringa.concat(percentage14);
    stringa.concat(",");
    stringa.concat(percentage13);
    stringa.concat(",");

    stringa.concat(percentage12);
    stringa.concat(",");
    stringa.concat(percentage6);
    stringa.concat(",");
    stringa.concat(percentage7);
    stringa.concat(",");

    stringa.concat(percentage5);
    stringa.concat(",");
    stringa.concat(percentage4);
    stringa.concat(",");
    stringa.concat(percentage1);
    stringa.concat(",");

    stringa.concat(percentage3);
    stringa.concat(",");
    stringa.concat(percentage2);
    stringa.concat(",");
    stringa.concat(percentage8);
    stringa.concat(",");

    stringa.concat(percentage9);
    stringa.concat(",");
    stringa.concat(percentage10);
    stringa.concat(",");
    stringa.concat(percentage11);
    
    stringa.concat(";");
    stringa.concat("\r");
    
    Serial.println(stringa);
    Serial3.print(stringa); //send string to ESP8266 connected on Serial3

    
  delay(1000);

I'm able to receive the string on the second Arduino, however, sometimes, I receive truncated strings or strings that are longer than normal.

For example, if I send:

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,389.69,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,237.33,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,257.84,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,269.56,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,397.02,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,262.23,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,234.40,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,287.14,152.00,-19.00,-3.00,3.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,375.04,152.00,-19.00,-3.00,3.00,114.00,152.00,106.00;

I receive:

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,389.69,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,237.33,152.00,-20.00,;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,257.84,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00; #114.00,141.00,152.00,114.00,114.00,152.00,141.00,269.56,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,397.02,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,262.23,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,234.40,152.00,-20.00,-3.00,4.00,114.00,152.00,106.00;

#114.00,141.00,152.00,114.00,114.00,152.00,141.00,287.14;

#114.00,141.00,152.00;

Can't understand why this happens!

i also tried to change the code in the ESP8266 client from:

String request = client.readStringUntil('\r');

to

String request = client.readStringUntil(';');

but in this case, nothing is received.

How can I improve and solve this problem?

Can You as temporary test and change readstringuntil into some readcharacter..... and serial.print them?

1 Like

there are some problems with your code.

  1. client.flush() sends data buffered in the transmit buffer. so do it after printing, not before

  2. you don't check if the connect was successful

  3. you only stop the connection on the server side

  4. it would be better to rewrite both sketches to leave the connection open and not to stop it and connect every loop

  5. client.println(Serial.read()); sends ascii codes of read characters as text on separate lines, instead of characters. use client.write(Serial.read()); for exact copy.

  6. the output you show doesn't match the code

1 Like

in this case, it prints nothing!

I tried to change the position of the flush() in the server code as:

void loop () {
  WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {
      String request = client.readStringUntil('\r');    
      client.println(Serial.read()); 
      client.flush();
    }
    client.stop();   //Terminates the connection with the client             
  }
}

but it doesn't solve the problem.
I noticed that the problem with the truncated strings increases if I decrease the delay() used to send the string in the loop() in the Arduino server code.

the output you show doesn't match the code

what do you mean?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.