Hi all, I am using Windows 10, ESP32S, and AsyncUDP library.
I have a project where I need to communicate with a server app using UDP for discovery and then with TP for everything else. I am stuck in the discovery phase.
My intention is to send a UDP broadcast that the server will receive and answer. The reply contains initialization information for to my client so that, among other things, it can switch over to TCP. I have not done UDP/TCP programming on the Arduino platform before, so I thought I would start by using one of the provided examples: AsyncUDPClient.ino. I made the following changes:
Used my own WiFi credentials.
Removed many lines from the receive packet code.
Changed the port # from 1234 to what the server is using 6309 (for UDP). Code follows.
#include "WiFi.h"
#include "AsyncUDP.h"
const char * ssid = "...";
const char * password = "...";
AsyncUDP udp;
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed");
while(1) {
delay(1000);
}
}
//The server is receiving on port 6309
//if(udp.listen(6309)) {
if(udp.connect(IPAddress(255,255,255,255), 6309)) {
Serial.println("UDP connected");
udp.onPacket([](AsyncUDPPacket packet) {
Serial.print(", From: ");
Serial.print(packet.remoteIP());
Serial.print(", Data: ");
Serial.write(packet.data(), packet.length());
Serial.println();
//reply to the client
packet.printf("Got %u bytes of data", packet.length());
});
//Send unicast
udp.print("Hello Server!");
}
}
void loop()
{
delay(1000);
//Send broadcast on port 1234
udp.broadcastTo("Server, are you there?", 6309);
}
type or paste code here
Documentation is scarce, so I assumed that the two port references (1234 originally), now 6309 refer to the port the server is listening to. With the server running, and this sketch running, the server does not receive anything. Obviously, the message that is sent in the sketch is not a valid request for the server, but in that case, the server just displays whatever it receives.
After some research, I found some people are using the Listen method, instead of the connect method, but no reason is given. I tried this and still it did not work. Thinking that maybe one of the port references was for the local sending port, I modified these two references to test various combinations, but still the server was not receiving anything. I went as far as changing the broadcast address (255,255,255,255) to the server's ip, but still no success. Much of the information that I found is different than what I am using, and since only code snippets are provided, I can only guess that others are using a different library, especially when the networking hardware is an Ethernet shield or the ESP 8266.
Why isn't the sample sketch working? What am I missing? Any orientation is welcomed. Thank you for your time and assistance. Saga