Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WifiUDP does not get broadcast packets. #279

Open
romanini opened this issue Apr 5, 2024 · 0 comments
Open

WifiUDP does not get broadcast packets. #279

romanini opened this issue Apr 5, 2024 · 0 comments

Comments

@romanini
Copy link

romanini commented Apr 5, 2024

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 credentials
char ssid[] = "<SSID>";
char pass[] = "<PASS>";

IPAddress broadcastIp(10,20,1,255);

unsigned int localPort = 8888;  // local port to listen on

WiFiUDP Udp;

unsigned long timestamp;

void setup() {
  //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 continue
    while (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);
}

void loop() {
  // periodically send a message
  if (millis() - timestamp > 3000) {
    Serial.println("Sending");
    Udp.beginPacket(broadcastIp, localPort);
    Udp.write("hello");
    Udp.endPacket();
    timestamp = millis();
  }

  // if there's data available, read a packet
  int 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 packetBufffer
    int len = Udp.read(packetBuffer, sizeof(packetBuffer) / sizeof(packetBuffer[0]));
    if (len > 0) {
    packetBuffer[len] = 0;
    }
    Serial.println("Contents:");
    Serial.println(packetBuffer);
  }
}


void printWifiStatus() {
  // 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant