-->
Page 1 of 1

question about communicate between two nodemcu esp8266

PostPosted: Tue Feb 26, 2019 4:22 am
by elhamkh
hi i want to establish communication between two nodemcu esp8266 wifi module as client and server without home router by arduino IDE using UDP protocol . after uploaded codes on two device, only server send packet to client but client is not able to send reply . i want to have bi-directional communication between them . please guide me about this issue
best regards.......

client code 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);

}

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

server code 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);
}
//======================================================================