ESP8266 occasionally drops data

Hey there!

I have problem with a setup of two ESP8266s: The first one reads a temperature and sends it as a preformatted string via client.println(). The second one receives that string and forwards it via Serial.println().

It works so far, but I discovered that a lot of sent strings are dropped (=not received). I guess it is because my code is not very good so maybe some of you might be able to push me into the right direction. Thanks! :slight_smile:

This is the code of the receiving ESP8266.

#include <ESP8266WiFi.h>

const char*  WIFI_SSID     = "XXXXXXXXXXXXXXXX";
const char*  WIFI_PASSWORD = "XXXXXXXXXXX";
const String MASTER_ID     = "8";
const int WIFI_CHANNEL  = 3;
const int SERVER_PORT = 80;

IPAddress local_IP(192,168,1,1);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);

WiFiServer server(SERVER_PORT);
WiFiClient client;

char message[50]={};


void setup()
{
  Serial.begin(9600);
  delay(500);
  WiFi.mode(WIFI_AP);
  Serial.println();
  Serial.print("Setting soft-AP configuration... ");
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  Serial.print("Starting soft-AP ... ");
  Serial.println(WiFi.softAP(WIFI_SSID,WIFI_PASSWORD,WIFI_CHANNEL) ? "Ready" : "Failed!");
  Serial.print("Soft-AP address: ");
  Serial.println(WiFi.softAPIP());
  server.begin();
  Serial.println("Server online and awaiting data.");
  delay(500);
}


void loop()
{
  client = server.available();
  client.setNoDelay(true);
  int i = 0;
  String data = MASTER_ID + ",";
  if (client.available() > 0)
  {
    #if 1
      client.setTimeout(1000);
      data = MASTER_ID + "," + client.readStringUntil('\n');
      Serial.println(data);
    #else
      while (client.available() > 0)
      {
        message[i] = client.read();
        i = i + 1;
      }
      for (int j=0; j<=i; j++)
      {
        data = data + message[j];
      }
      Serial.println(data);
    #endif
  }
  delay(1);
}

This is the code of the sending ESP8266. Please note that I’ve taken out the code that would read the SHT21 sensor for testing purposes.

#include <ESP8266WiFi.h>

const char* WIFI_SSID = "XXXXXXXXXXXXXXXX";
const char* WIFI_PASSWORD = "XXXXXXXXXXX";
const int SERVER_PORT = 80;
const String NODE_ID = "1";

IPAddress serverIP(192, 168, 1, 1);

WiFiClient client;

int wifitimeout = 0;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  delay(500);

  Serial.println();
  Serial.println("Starting up...");

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.println();
  Serial.print("Waiting for wireless network.");

  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
    if (wifitimeout == 50)
    {
      Serial.println("Could not connect to WiFi Network.");
      Serial.println("Device will restart in ten seconds.");
      ESP.deepSleep(10000000);
    }
    wifitimeout++;
  }

  Serial.println();
  Serial.println("WiFi connection established!");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  client.connect(serverIP, SERVER_PORT);
  delay(500);
}


void loop()
{
  // using random data for testing purposes
  String temperature = String(random(1,100));
  String humidity = String(random(1,100));
  String data = NODE_ID + ',' + temperature + ',' + humidity;
  
  if (client.connect(serverIP, SERVER_PORT))
  {
    Serial.print("Sending data:");
    Serial.println(data);
    client.println(data);
    // Serial.println("Disconnecting.");
    // client.stop();
  }

  // usually there would be a ESP.deepSleep() here but
  // I used a delay() for testing purposes
  delay(5000);
}

Update: I had closer look at the documentation and some examples. Then I revamped my code and it works great now, even with sleep times of one second or so.

This is the code of the receiving ESP8266:

#include <ESP8266WiFi.h>

const char WIFI_SSID[] = "WiFi";
const char WIFI_PASSWORD[] = "ESP8266WIFI";
const int SERVER_PORT = 80;

const String MASTER_ID = "8";

WiFiServer server(SERVER_PORT);

IPAddress local_IP(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnetmask(255, 255, 255, 0);

void setup()
{
  Serial.begin(9600);
  Serial.println();
  Serial.println("Starting Soft-AP.");
  WiFi.mode(WIFI_AP);
  WiFi.softAP(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Setting soft-AP configuration ... ");
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnetmask) ? "Ready" : "Failed!");
  server.begin();
  Serial.println();
  Serial.println("Server online and awaiting data.");
  Serial.print("IP: ");
  Serial.println(WiFi.softAPIP());
  Serial.print("MAC: ");
  Serial.println(WiFi.softAPmacAddress());
  Serial.println();
}

void loop()
{
  WiFiClient client = server.available();
  if (!client)
  {
    return;
  }
  String inboundData = client.readStringUntil('\n');
  Serial.println("Inbound data: " + MASTER_ID + ',' + inboundData);
  client.flush();
}

This is the code of the sending ESP8266:

#include <ESP8266WiFi.h>
#include <Wire.h>

const char WIFI_SSID[] = "WiFi";
const char WIFI_PASSWORD[] = "ESP8266WIFI";
const int SERVER_PORT = 80;

IPAddress serverIP(192, 168, 1, 1);

const String NODE_ID = "1";
int WiFiTimeout = 0;

WiFiClient client;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  Serial.println();
  Serial.println("Starting up...");
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.println();
  Serial.print("Waiting for wireless network.");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
    if (WiFiTimeout == 50)
    {
      Serial.println("Could not connect to WiFi Network.");
      Serial.println("Device will restart in ten seconds.");
      ESP.deepSleep(10000000);
    }
    WiFiTimeout++;
  }
  Serial.println();
  Serial.println("WiFi connection established!");
  Serial.print("LocalIP:");
  Serial.println(WiFi.localIP());
  Serial.print("MAC: ");
  Serial.println(WiFi.macAddress());
  Serial.print("Gateway: ");
  Serial.println(WiFi.gatewayIP());
  Serial.print("AP MAC: ");
  Serial.println(WiFi.BSSIDstr());
}

void loop()
{
  String outboundData = NODE_ID + ',' + fetchTemperature() + ',' + fetchHumidity();
  client.connect(serverIP, SERVER_PORT);
  Serial.print("Outbound data: ");
  Serial.println(outboundData);
  client.println(outboundData);
  client.flush();
  client.stop();
  Serial.println("Data sent. Device will restart in ten seconds.");
  ESP.deepSleep(10000000);
}

String fetchTemperature()
{
  int firstTempByte = 0, secondTempByte = 0, thirdTempByte = 0;
  Wire.beginTransmission(0x40);
  Wire.write(0xF3);
  Wire.endTransmission();
  delay(200);
  Wire.requestFrom(0x40, 3);
  delay(25);
  while (Wire.available() > 2)
  {
    firstTempByte = (int)Wire.read();
    secondTempByte = (int)Wire.read();
    thirdTempByte = (int)Wire.read();
  }
  float calculatedTemperature = -46.85 + (175.72 * (((firstTempByte << 8) + secondTempByte) / (pow(2, 16)) ));
  return String(calculatedTemperature);
}

String fetchHumidity()
{
  int firstHumiByte = 0, secondHumiByte = 0, thirdHumiByte = 0;
  Wire.beginTransmission(0x40);
  Wire.write(0xF5);
  Wire.endTransmission();
  delay(200);
  Wire.requestFrom(0x40, 3);
  delay(25);
  while (Wire.available() > 2)
  {
    firstHumiByte = (int)Wire.read();
    secondHumiByte = (int)Wire.read();
    thirdHumiByte = (int)Wire.read();
  }
  float calculatedHumidity = -6 + (125 * (((firstHumiByte << 8) + secondHumiByte) / (pow(2, 16))));
  return String(calculatedHumidity);
}

Which ESP8266 module do you have?
ESP8266 is the IC.

The modules are ESP-01, ESP-05, etc.

The receiving unit is a NodeMCU(ESP-12) board and the sending unit is a Wemos D1 mini Pro.