You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to have my Nano 33 IoT listen to UDP broadcast packets from a server. I need this because I'll have several devices connected to a central arduino and I want to send status to all of them. The code I have is a slight modification of the example but structurally the same:
#include<WiFiNINA.h>
#include<WiFiUdp.h>// WiFi credentialschar ssid[] = "<SSID>";
char pass[] = "<PASS>";
IPAddress broadcastIp(10,20,1,255);
unsignedint localPort = 8888; // local port to listen on
WiFiUDP Udp;
unsignedlong timestamp;
voidsetup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect// check for the WiFi module:if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continuewhile (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:delay(10000);
}
Serial.println("Connected to WiFi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
Udp.begin(localPort);
}
voidloop() {
// periodically send a messageif (millis() - timestamp > 3000) {
Serial.println("Sending");
Udp.beginPacket(broadcastIp, localPort);
Udp.write("hello");
Udp.endPacket();
timestamp = millis();
}
// if there's data available, read a packetint 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());
char packetBuffer[255]; //buffer to hold incoming packet// read the packet into packetBuffferint len = Udp.read(packetBuffer, sizeof(packetBuffer) / sizeof(packetBuffer[0]));
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents:");
Serial.println(packetBuffer);
}
}
voidprintWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
This code runs and I can see that I'm sending a broadcast packet and I can receive it on other devices in the network using nc -luvw0 8888 so I know it is sending. However I don't receive anything on the arduino (ie in my loop I never get anything. IF I modify the sending from what it is now
Udp.beginPacket(broadcastIp, localPort);
to be
Udp.beginPacket(WiFi.localIP();, localPort);
that is to send the UDP packet directly to the device rather than a broadcast message. Then the arduino gets the message, but of course no other device in the network gets it.
How can I receive broadcast packets?
The text was updated successfully, but these errors were encountered:
I am trying to have my Nano 33 IoT listen to UDP broadcast packets from a server. I need this because I'll have several devices connected to a central arduino and I want to send status to all of them. The code I have is a slight modification of the example but structurally the same:
This code runs and I can see that I'm sending a broadcast packet and I can receive it on other devices in the network using
nc -luvw0 8888
so I know it is sending. However I don't receive anything on the arduino (ie in my loop I never get anything. IF I modify the sending from what it is nowto be
that is to send the UDP packet directly to the device rather than a broadcast message. Then the arduino gets the message, but of course no other device in the network gets it.
How can I receive broadcast packets?
The text was updated successfully, but these errors were encountered: