-
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.
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)
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)
3.- Comments.
-
Search the internet for the difference between TCP and UDP protocols.
-
Packet size: Experimentally I have verified that up to 65507 characters can be sent, but 4096 are received.
-
UDP can work in Unicast, Broadcast and Multicast.
-
This tutorial in Spanish:
http://kio4.com/arduino/118B_Wemos_UDP_AppInventor.htm -
Other codes for ESP32/ESP8266 and UDP:
http://kio4.com/arduino/118_Wemos_UDP.htm