Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By elhamkh
#80801 Hi there
i want to establish communication between two nodemcu esp8266 as a client and a server without home router by UDP protocol using arduino IDE , after upload of codes , just server sends packet to client but client is not able to send reply . how can i make bilateral communication between them
i dont know what is problem
i introduced two ip address that are for client and server

code of client is :

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

const char *ssid = "********";
const char *pass = "*****";

unsigned int localPort = 2000; // local port to listen for UDP packets

IPAddress ServerIP(192,168,4,1);
IPAddress ClientIP(192,168,4,3);

// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;

char packetBuffer[255]; //Where we get the UDP data

char replyBuffer[99] = "Hi there! Got the message "; // a reply string to send back

//======================================================================
// Setup
//======================================================================
void setup()
{
Serial.begin(115200);
Serial.println();

WiFi.begin(ssid, pass); //Connect to access point

Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}


//======================================================================
// MAIN LOOP
//======================================================================
void loop()
{
int cb = udp.parsePacket();
{
udp.beginPacket(ClientIP, 2000);
udp.write(replyBuffer,99); //Send one byte to ESP8266
udp.endPacket();
delay(1000);
}


// We've received a UDP packet, send it to serial
udp.read(packetBuffer, 255); // read the packet into the buffer, we are reading only one byte
Serial.print(packetBuffer);
delay(2000);

}

//=======================================================================

code of server is :
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid = "********";
const char *pass = "******";

unsigned int localPort = 2000; // local port to listen for UDP packets

IPAddress ServerIP(192,168,4,1);
IPAddress ClientIP(192,168,4,2);

// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;

char packetBuffer[]="this buffer is just for test of server"; //Where we get the UDP data
char replyBuffer[99];


//=======================================================================
// Setup
//=======================================================================
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.softAP(ssid, pass); //Create Access point

//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
//======================================================================
// MAIN LOOP
//======================================================================
void loop()
{


int cb = udp.parsePacket();

{
udp.beginPacket(ClientIP, 2000);
udp.write(packetBuffer,255); //Send one byte to ESP8266
udp.endPacket();
delay(1000);
}


udp.read(replyBuffer, 99); // read the packet into the buffer, we are reading only one byte
Serial.print(replyBuffer);
delay(2000);
}
//======================================================================

please help me to find solution
best regards.....
User avatar
By DIRR70
#81384 Hello elhamkh,

Basically your attempt looks good, but you are messing around with the IP addresses and the loop method.

You shouldn't use fixed IPs, because it is absolutely not said that the client IP always will be the same. In case that your used IPs where correct (currently) you still need to address the right destination. So, if the server is running an AP on "192.168.4.1" (ESP default) and the client really got the IP "192.168.4.2" the server needs to send its messages to "192.168.4.2" and the client to "192.168.4.1". Where your "192.168.4.3" comes from - I don't know.

After connecting to an AP you can check (and possibly dump to serial) your local IP explained in all WiFi examples:
Code: Select allSerial.println(WiFi.localIP());

After that the client should use that IP and the server should remind the
Code: Select alludp.remoteIP();

to know to what IP he should send data back.

Also please consider, that the loop function is called quite often (never measured it, but several 100 times a second). If you send a packet with every loop I'm afraid you overcharge things a little bit.

Last, but not least, please think about your chosen protocol. UDP is a non checked protocol. Data packets are "fired and forgot". If they get scrambled or lost - nothing happens - there's no guarantee for data to arrive. TCP, however, will check whether the packet has arrived and repeatedly send it if not (or if it got scrambled). So, for a bidirectional communication TCP might be the better choice. Of course you have to address correctly here, too ;)