I am using UDP to connect two nodemcu modules. One nodemcu is wireless acces point and another nodemcu connects to access point as client.
This code sends client's IP adress to AP when client connects:
Udp.beginPacket("192.168.4.1", UDPPort);//send ip to server
char ipBuffer[20];
WiFi.localIP().toString().toCharArray(ipBuffer, 20);
Udp.write(ipBuffer);
Udp.endPacket();
Serial.println("Sent ip adress to server");
But on the server side I don't recieve this packet.
Client:
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
unsigned int UDPPort = 2390; // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet
char replyBuffer[] = "acknowledged"; // a string to send back
WiFiUDP Udp;
void setup() {
Serial.begin(115200);
WiFi.begin("Wi-Fi");
Serial.println();
Serial.print("Wait for WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());
Udp.begin(UDPPort);
Udp.beginPacket("192.168.4.1", UDPPort);//send ip to server
char ipBuffer[255];
WiFi.localIP().toString().toCharArray(ipBuffer, 255);
Udp.write(ipBuffer);
Udp.endPacket();
Serial.println("Sent ip adress to server");
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(replyBuffer);
Udp.endPacket();
}
}
Server:
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
unsigned int UDPPort = 2390; // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet
char ReplyBuffer[] = "acknowledged"; // a string to send back
WiFiUDP Udp;
void setup() {
Serial.begin(115200);
WiFi.softAP("Wi-Fi");
Udp.begin(UDPPort);
Serial.println();
Serial.println("Started ap. Local ip: " + WiFi.localIP().toString());
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}
Another thing doesn't work: If I send a packet from another device connected to AP nodemcu to client nodemcu(also connected to AP), packet is recieved, but I get no acknowledgement reply back to device.
Everything else works - If i send a packet from another device to AP nodemcu, packet is recieved and i get acknowledgement. Also, if I connect to my home wi-fi router with client nodemcu and listen for packet from my pc, i get client's ip adress when it connects.