Hi, everyone, I've browsed a lot for a code snipped but all I found were people that facing the same issue. :-/
The job sounds simple: I want 2 ESP32 comunicating via UDP, one as server, one as client. In station mode no problem. But I want do this without my router / homenetwork by making the "server" ESP32 an access point. Result: The client ESP connects successfully to the AP but parsePacket() returns always 0.
I guess I'm dealing with a general misunderstanding of how the UDP layer works. What I'm doing wrong?
Perhaps ESP-NOW would be a better choice.
That was my first approach. Low throughput and small packet size...
should work OK running a UDP server on an AP with client connecting to it
post your code?
You should be able to. Once the AP is started
udp.begin(WiFi.localIP(), udpPort);
It starts on the client and server the same way.
Check out the example for WiFiUDPClient. That is how the client sends. The "server" waits for data with
while(udp.available() {
char ch = udp.read();
}
Hi, I use this project as base:
In this case both ESP32 act in station mode. this worx for me too. Here is what I've modified:
=== Server ===
#include <WiFi.h>
#include <WiFiUdp.h>
WiFiUDP udp;
byte packetBuffer[255];
unsigned int localPort = 1234;
const char * ssid = "BlaBla";
const char * password = "SuperBla";
byte bix=0;
void setup() {
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
delay(100); // some do, some don't ...
udp.begin(localPort);
}
void loop() {
udp.beginPacket("192.168.4.2", localPort); // IP client ESP
udp.write(packetBuffer,255);
udp.endPacket();
packetBuffer[0]=bix++; // just an upcounting number for tests
delay(500);
}
=== Client ===
#include <WiFi.h>
#include <WiFiUdp.h>
WiFiUDP udp;
unsigned int localPort = 1234;
const char * ssid = "BlaBla";
const char * password = "SuperBla";
void setup() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
udp.begin(localPort);
}
void loop() {
int packetSize = udp.parsePacket(); // always zero :-(((
if (packetSize) {
//...
}
}
So what is your problem. You should describe how the way that does not work deviates from the working version and post this not working code-version too.
Do what does this mean?
A packetsize of 250 bytes is too small and sending an ESP-NOW message is 2 milliseconds is too low throughput?
You should be much clearer in specifiying your requirements
Post both sketches.
The ESP32 acting as AP-sketch
and
the ESP32 acting in station-mode which connects to your AP-ESP32
Please refer my last post. There you'll find the AP and STATION side. Sorry, poor formatted, looks like 1 sketch :-/
Example UDP chat program using Access point
// ESP32 Access Point UDP server chat program
#include <WiFi.h>
#include <WiFiUdp.h>
// Replace with your network credentials
const char* ssid = "ESP32_Access_point";
const char* password = "123456789";
// UDP information
unsigned int localPort = 10000; // local port to listen on
unsigned int remotePort = 10000; // remote port to transmiit too
WiFiUDP udp;
void setup() {
Serial.begin(115200);
while (!Serial) delay(1);
delay(1000);
Serial.setTimeout(60000l);
Serial.println();
Serial.println("ESP32 WiFi ACCess Point UDP chat - send UDP datagrams ");
Serial.begin(115200);
// setup Wi-Fi network with SSID and password
Serial.printf("Setting AP (Access Point)… '%s'\n", ssid);
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
Serial.print("Ethernet UDP started ");
displayIPaddress(WiFi.localIP(), localPort);
// Begin udp port
udp.begin(localPort);
Serial.print("Opening udp port ");
Serial.println(localPort);
Serial.println("Waiting for UDP client datagram");
}
void loop() {
static bool clientConnected=false;
// if Serial text available send as a datagram
if (Serial.available()) {
udp.begin(localPort);
char text[100] = { 0 };
Serial.readBytesUntil('\n', text, 100);
if(!clientConnected) return; // if no UDP client return
Serial.print("Transmitting to ");
displayIPaddress(udp.remoteIP(), remotePort);
Serial.println(text);
udp.beginPacket(udp.remoteIP(), remotePort); // transmit datagram
udp.print(text);
udp.endPacket();
}
// check for receiving UDP datagram from client
int packetSize = udp.parsePacket(); // check if datagram received
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
displayIPaddress(udp.remoteIP(), udp.remotePort());
clientConnected=true;
// read the packet into packetBuffer
char packetBuffer[100] = { 0 }; // buffer to hold incoming packet,
udp.read(packetBuffer, packetSize); //UDP_TX_PACKET_MAX_SIZE); // receive datagram
Serial.print("Contents: ");
Serial.println(packetBuffer);
}
}
// read IP address from keyboard and check it
IPAddress getIPaddress(const char* prompt) {
IPAddress ip;
while (1) { // read IP (end with new line)
Serial.print(prompt);
while (Serial.available() == 0) delay(10);
char text[40] = { 0 };
Serial.readBytesUntil('\n', (char*)text, 40);
for (int i = 0; i < 40; i++)
if (text[i] < ' ') text[i] = 0; // remove CR or LF
Serial.print(text);
if (ip.fromString(text)) break; // if IP OK break while
Serial.println(" invalid IP try again");
}
return ip;
}
// print IPAdress and port
void displayIPaddress(const IPAddress address, unsigned int port) {
Serial.print(" IP ");
for (int i = 0; i < 4; i++) {
Serial.print(address[i], DEC);
if (i < 3) Serial.print(".");
}
Serial.print(" port ");
Serial.println(port);
}
and station mode client which connects to AP
// ESP32 UDP chat program - connect to Access point
#include <WiFi.h>
#include <WiFiUdp.h>
// network SSID (network name). and network password.
const char* ssid = "ESP32_Access_point";
const char* password = "123456789";
// UDP information
unsigned int localPort = 10000; // local port to listen on
unsigned int remotePort = 10000; // remote port to transmiit too
WiFiUDP udp;
IPAddress remoteIP(192, 168, 4, 1);
void setup() {
Serial.begin(115200);
while (!Serial) delay(1);
delay(1000);
Serial.setTimeout(60000l);
Serial.println();
Serial.println("ESP32 WiFi UDP chat - send UDP datagrams ");
Serial.println("Connecting to Access Point 'ESP32_Access_point'");
WiFi.mode(WIFI_STA); // Connect to Wifi network.
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { // Wait for connection
delay(500);
Serial.print(".");
}
Serial.print("\nGateway IP address: ");
Serial.println(WiFi.gatewayIP());
Serial.print("Ethernet UDP started ");
displayIPaddress(WiFi.localIP(), localPort);
// Begin udp port
udp.begin(localPort);
Serial.print("Opening udp port ");
Serial.println(localPort);
// remote IP address of Access Point
Serial.print("\nAP remote IP address ");
displayIPaddress(remoteIP, remotePort);
}
void loop() {
if (Serial.available()) { // if Serial text available send as a datagram
udp.begin(localPort);
char text[100] = { 0 };
Serial.readBytesUntil('\n', text, 100);
Serial.print("Transmitting to ");
displayIPaddress(remoteIP, remotePort);
Serial.println(text);
udp.beginPacket(remoteIP, remotePort); // transmit datagram
udp.print(text);
udp.endPacket();
}
// check for datagram from AP server
int packetSize = udp.parsePacket(); // check if datagram received
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
displayIPaddress(udp.remoteIP(), udp.remotePort());
// read the packet into packetBuffer
char packetBuffer[100] = { 0 }; // buffer to hold incoming packet,
udp.read(packetBuffer, packetSize); //UDP_TX_PACKET_MAX_SIZE); // receive datagram
Serial.print("Contents: ");
Serial.println(packetBuffer);
}
}
// read IP address from keyboard and check it
IPAddress getIPaddress(const char* prompt) {
IPAddress ip;
while (1) { // read IP (end with new line)
Serial.print(prompt);
while (Serial.available() == 0) delay(10);
char text[40] = { 0 };
Serial.readBytesUntil('\n', (char*)text, 40);
for (int i = 0; i < 40; i++)
if (text[i] < ' ') text[i] = 0; // remove CR or LF
Serial.print(text);
if (ip.fromString(text)) break; // if IP OK break while
Serial.println(" invalid IP try again");
}
return ip;
}
// print IPAdress and port
void displayIPaddress(const IPAddress address, unsigned int port) {
Serial.print(" IP ");
for (int i = 0; i < 4; i++) {
Serial.print(address[i], DEC);
if (i < 3) Serial.print(".");
}
Serial.print(" port ");
Serial.println(port);
}
Access Point serial monitor
ESP32 WiFi ACCess Point UDP chat - send UDP datagrams
Setting AP (Access Point)… 'ESP32_Access_point'
AP IP address: 192.168.4.1
Ethernet UDP started IP 0.0.0.0 port 10000
Opening udp port 10000
Waiting for UDP client datagram
Received packet of size 26
From IP 192.168.4.2 port 10000
Contents: hello Access Point server
Received packet of size 17
From IP 192.168.4.2 port 10000
Contents: test2 1234567890
Transmitting to IP 192.168.4.2 port 10000
hello client
Transmitting to IP 192.168.4.2 port 10000
test to client 1234509876
client serial monitor
ESP32 WiFi UDP chat - send UDP datagrams
Connecting to Access Point 'ESP32_Access_point'
.
Gateway IP address: 192.168.4.1
Ethernet UDP started IP 192.168.4.2 port 10000
Opening udp port 10000
AP remote IP address IP 192.168.4.1 port 10000
Transmitting to IP 192.168.4.1 port 10000
hello Access Point server
Transmitting to IP 192.168.4.1 port 10000
test2 1234567890
Received packet of size 13
From IP 192.168.4.1 port 10000
Contents: hello client
Received packet of size 26
From IP 192.168.4.1 port 10000
Contents: test to client 1234509876
Tnx!!! Looks promising :-)) At first look is seems almost same code for the UDP part - BUT - on the client (AP) side there is <udp.begin(localPort)> inside the loop() called each time some serials come in... My code uses udp.begin only once in setup. Could that be the answer? I'll test it.
not sure why it is in loop()
moved it to setup() and code still works OK
IT WORX Your code doin' well in my ESP32 / ESP32S2.
After stripping your lines down to its 'UDP bones' it looked almost like mine - except - for one thing... A foolisch slip far away from my suspected UDP misunderstanding.
void loop() {
int packetSize = udp.parsePacket();
// Aaaaarrrgggghhhhhh... :-((
// Serial.print(" Rec from : "); Serial.println(udp.remoteIP());
// Serial.print(" Size : "); Serial.println(packetSize);
if (packetSize) {
// !!! much better here...
Serial.print(" Rec from : "); Serial.println(udp.remoteIP());
Serial.print(" Size : "); Serial.println(packetSize);
// ... and so on ...
}
}
So tnx again for all your support. Btw, in the ESP32S2 example folder is a demo for an UDP client / server using the 'AsyncUDP' lib. This example can be modified in the same way. Avoiding a router by making the server side an AP.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.