ESP8266 send UDP string to AP

17,292

Solution 1

I got exactly the same problem. I've just solve it. Your code is almost the same as mine.

All ESP- modules can be AP and station. That means, a ESP module has local network for itself.

In my case, client module(ESP) is station mode and server module(ESP) is SoftAP mode.

The ip of the server module is 192.168.4.9 and I set the ip of gateway is 192.168.4.1.

When client module connected to the AP of server module, the ip of client was 192.168.4.103, and then i tried to send udp packets from the client module to the AP. Nothing happened but when i send the same packet from other device such as pc it worked.

I tried to access the AP of server module on my laptop. I found a wired SSID the name was 'EPS_4510DB'. It was actually the client module's one. The gateway ip of ESP_4510DB was 192.168.4.1.

Suddenly I realised the network of client module's AP and of server module's AP is the same, and the client module(for station)'s network linked its own AP network.

In other word, the client module sent udp packets to AP's network of itself instead of one of the server module.

So i changed the ip and i was able to send udp packets to server module.

And as @Barry Bea mentioned, I think you can also disable the AP of client module by using WiFi.mode(WIFI_STA) in client module, not server module.

I hope it helps someone~

Solution 2

I have painstakingly been trying to trouble shoot a similar problem...

I too could not get any packages sent by my ESP8266's

I changed your line;

WiFi.softAP("Wi-Fi");
to ....
WiFi.mode(WIFI_STA);

works EVERY TIME now....with no package loss..

Share:
17,292
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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.