ESP32 with UDP send/receive text. Chat mobile-mobile UDP. Testing Extension UDP by Ullis (@Ulrich_Bien)

  • We are going to test @Ulrich_Bien 's UDP extension to send/receive text from ESP32 and from two mobiles.

  • The extension and complete documentation can be downloaded from...
    AI2 UDP Extension: https://ullisroboterseite.de/android-AI2-UDP-en.html

  • I will use the version: 4.4 (2022-03-31)

  • App Inventor version: nb190b

  • Android 5 and Android 9.

  • ESP32.

5 Likes

1.- App send/receive text to/from ESP32. LED2 turn on/off. Check LED2 status. WiFi. UDP.

  • Connect to our WiFi network.

  • The port of this Server UDP is set to: 1234

  • App sends text to ESP32.

  • If ESP32 receives "on", it turns on LED2 and returns the message "Received: on".

  • If ESP32 receives "off", it turns off LED2 and returns the message "Received: off".

  • If ESP32 receives "consulta", it returns the message "Now LED2 is HIGH (LOW)".

  • In this example, the application will send the message to the entire "Broadcast" network (255.255.255.255) and it will be received by all devices that have port 1234 open with UDP.

  • The code for the ESP32 is an adaptation of...
    https://github.com/espressif/arduino-esp32/blob/master/libraries/AsyncUDP/examples/AsyncUDPServer/AsyncUDPServer.ino

#include "WiFi.h"
#include "AsyncUDP.h"

const char * ssid = "Nombre_WiFi";
const char * password = "Clave_WiFi";

AsyncUDP udp;

void setup(){
    pinMode(2, OUTPUT); // HERE LED2
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
        Serial.println("WiFi Failed");
        while(1) {
            delay(1000);
        }
    }
    if(udp.listen(1234)) {
        Serial.print("UDP Listening on IP: ");
        Serial.println(WiFi.localIP());
        udp.onPacket([](AsyncUDPPacket packet) {
            Serial.print("UDP Packet Type: ");
            Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
            Serial.print(", From: ");
            Serial.print(packet.remoteIP());
            Serial.print(":");
            Serial.print(packet.remotePort());
            Serial.print(", To: ");
            Serial.print(packet.localIP());
            Serial.print(":");
            Serial.print(packet.localPort());
            Serial.print(", Length: ");
            Serial.print(packet.length());
            Serial.print(", Data: ");
            Serial.write(packet.data(), packet.length());
            Serial.println();

// https://stackoverflow.com/questions/58253174/saving-packet-data-to-string-with-asyncudp-on-esp32
             // String  mensaje = (char*)(packet.data());
            char* tmpStr = (char*) malloc(packet.length() + 1);
            memcpy(tmpStr, packet.data(), packet.length());
            tmpStr[packet.length()] = '\0'; // ensure null termination
            String mensaje = String(tmpStr);
            free(tmpStr); // Strign(char*) creates a copy so we can delete our one

            Serial.println(mensaje);
            if(mensaje == "on"){
              digitalWrite(2, HIGH);
              packet.print("Received: on");
            }
            if(mensaje == "off"){
              digitalWrite(2, LOW);
              packet.print("Received: off");
            }
            if(mensaje == "consulta"){
              if(digitalRead(2 == HIGH)){packet.print("Now LED2 is HIGH");}
              if(digitalRead(2 == LOW)) {packet.print("Now LED2 is LOW");}
            }

            //reply to the client
            //packet.printf("Got %u bytes of data", packet.length());
        });
    }
}

void loop(){
    delay(1000);
    //Send broadcast
    udp.broadcast("Anyone here?");
}

To convert the packet.data to String I have used...
https://stackoverflow.com/questions/58253174/saving-packet-data-to-string-with-asyncudp-on-esp32

- Blocks.

p118B_ESP32_UDP.aia (53.7 KB)

1 Like

2.- Chat two mobiles. UDP. WiFi.

  • Notice that ports 4444 and 5555 are "crossed".
  • In each mobile we load the same application that we have seen in the previous post: p118B_ESP32_UDP.aia (53.7 KB)
2 Likes

3.- Comments.

1 Like

4.- Windows send/receive UDP. Packet Sender.