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