-->
Page 1 of 1

ESP8266 TCP communication data loss

PostPosted: Fri Mar 31, 2017 4:25 pm
by Sri1234
Hi guys, I am new to ESP8266 programming. I have two ESP device. One as AP (master) and other as Station (client). The station reads the serial data from the microcontroller (I verified the serial data coming out of microcontroller with Hterm terminal software). The client sends the serial data received from the microcontroller to the master via TCP protocol. This is the objective of my work. But before using microcontroller data, i tried to manually input the serial data from PC using Hterm by typing characters on the window. The problem i am facing is, sometimes the master receives the data sent by client and sometimes it is not receiving the data. I am losing data more than 60% of the time. I am not able to troubleshoot the issue. As far as i can understand, TCP protocol will try to send the data till it gets delivered to the master. But it is not happening in this case. Kindly guide me through this.

Sketch - Master / Server

Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "test";
const char* pw = "12345678";
WiFiServer server(80);
WiFiClient client;

void setup() {
  Serial.begin(9600);
  //setup AP
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, pw);
  Serial.println("AP started at: ");
  Serial.print(WiFi.softAPIP());
  server.begin();
  Serial.println("Server started...");
}

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

  if(client)
  {
    //Serial.println("Client connected!");
    //Serial.print("HasClient: ");
    //Serial.println(server.hasClient());
    if(client.available())
    {
    char message = client.read();
    Serial.println(message);
    Serial.println('A');
    }
   
   
    delay(1);
       
  }
}


Sketch - Client

Code: Select all#include <ESP8266WiFi.h>


void setup() {
 
  Serial.begin(9600);
  WiFi.mode(WIFI_STA);
  WiFi.begin("test","12345678");
  Serial.println();
    Serial.println();
    Serial.print("Wait for WiFi... ");
   
  while(WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
        delay(500);
  }
     Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
 

}

void loop()
{
 
  //byte serdata[3]={5};
 
  const int port=80;
  const char ip[]="192.168.4.1"; 
   WiFiClient client;
 
  client.connect(ip,port);
   
   /*Serial.print("connecting to ");
    Serial.println(ip);*/
 
    if (!client.connect(ip, port))
    {
        Serial.println("connection failed");
        Serial.println("wait 5 sec...");
        delay(2000);
        return;
    }
    while (Serial.available()>0)
    {
     
      serin=Serial.read();
      serdata=serin;
         
     
       client.write(serdata);
       Serial.println(serdata);
      }
   
    delay(1);
   
}