ESP8266 Send and receive via UDP

Hello guys

I am new to ESP8266 programming using arduino IDE. I am trying to establish UDP communication between two ESP8266 devices, one as master and client. I observed as expected ,that not all data were successfully received by the master. Consider the client sends a byte x, the master receives it and i programmed the master to send the byte x back to the client. If the client receives x , it will compare it against the previously send data (x) .if it is not equal or not received any data from master, the client will simply resend the same data until it receives data from master. Now the problem is , the master receives the data, the client is not receiving the data. I have attached the code for both master and client. I am not able to find out the mistake in my code or am i missing something in my code ?

Client code

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

WiFiUDP Client;

byte serdata=0;
byte fromserver=0;

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  WiFi.mode(WIFI_STA); 
  WiFi.begin("test2","22345678");
  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());   
    Client.begin(81);
}

void loop() 
{
const int port=80;
int i=0;  
   /*Serial.print("connecting to ");
    Serial.println(ip);*/
udpsend();
  
  char serverack = Client.parsePacket();
  Serial.println(serverack);
  if (serverack)
    {
    fromserver=Client.read();
    Serial.println(fromserver);
    delay(1000);
      if (fromserver==serdata)
        {
          serdata=serdata+1;
        }
      else
         {
         Serial.println("not equal");
         Serial.println(fromserver);
          udpsend();
         }
    }
   else
   {
    Serial.println("no data");
    delay(1000);
    }
    
    //client.stop();
    delay(1000);
}

void udpsend()
  {
    const char ip[]="192.168.4.1";
  Client.beginPacket(ip,80);
  Client.write(serdata);
  Client.endPacket();
  delay(1000);
  return;
  }

Master code

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

const char* ssid = "test2";
const char* pw = "22345678";
char data[200] ={};
int packetsize = 0; 
String receiveddata="";
//WiFiServer Server(80);
WiFiUDP Server;

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

void loop() {
  
    char message = Server.parsePacket();
    packetsize = Server.available();
    if (message)
    {
      
     Server.read(data,packetsize);
     delay(100);
     IPAddress remoteip=Server.remoteIP();
     delay(100);
     Serial.println(Server.beginPacket(remoteip,81));
     Serial.println(remoteip);
     Serial.println(Server.remotePort());
     Serial.println(data);
     Server.write(data);
     Server.endPacket();
    }

    if(packetsize) {
      Serial.println(packetsize);
      for (int i=0;packetsize > i ;i++)
      {
        receiveddata+= (char)data[i];
      }
        
      Serial.print(receiveddata);
      Serial.println();
      receiveddata="";
    }
    delay(100);
  
  if(WiFi.status()==!WL_CONNECTED)
  {
    Serial.print(".");
    delay(100);
  }
}

Thanks in advance

Sriniketh

Something doesn't sound right. Maybe it is just semantics. The server sends a packet to the client, then the client returns a packet to the server. How can the server receive a packet if the client didn't receive one?

I guess my question is: How do you know the client is not receiving a packet?

SurferTim:
Something doesn't sound right. Maybe it is just semantics. The server sends a packet to the client, then the client returns a packet to the server. How can the server receive a packet if the client didn't receive one?

I guess my question is: How do you know the client is not receiving a packet?

Hi , I am sorry i made a mistake while posting the question. The code for master and client was interchanged. Now i edited it. Answering to your question. The client first sends the data using the function udpsend() , as per the program the first data is 0, i connected the master via serial port and it acknowledged that it had received a byte from the client. If the master sends the data i.e 0 back to the client and if the client receives it, it will increment the data to 1 and the master will receive data as 1 in the next iteration. But the master kept on receiving only 0 everytime. It means either master has not sent the data or client has not received it. But i am not sure how to check

Thanks in advance

Sriniketh

The parsePacket call returns the size of the packet.

I would start by printing the value received.

Moin,

dein Code funktioniert wenn du im Client aus dem Sendebyte / Integer ein Char machst. Dann musst du natürlich auch dein Vergleicher überarbeiten.

MFG

you can use this code it's too simple.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
const char* ssid = "<>"; // SSID
const char* password = "<>"; // Password
const char* host = "182.64.29.28"; // Your public IP address
const int port = 2222 ; // Port serveur - Server Port
const int watchdog = 50000; // Fréquence du watchdog - Watchdog frequency
unsigned long previousMillis = millis();
String data ="1234";
WiFiClient client;
WiFiServer server(2222);
void setup() {
Serial.begin(9600);
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.connect(host, port);
client.println("Hello"); // Send to server
}

void loop() {
//**Resieve from Server//
while(client.available()){
char line = client.read();
Serial.print(line);
}
}