arduino-libraries / WiFiNINA

141 stars 107 forks source link

IP broadcast in AP mode results in packet loss #77

Open cwerner77 opened 5 years ago

cwerner77 commented 5 years ago

Hi folks,

I have observed an issue when sending UDP broadcast messages with my Arduino MKR WiFi 1010.

When running the Arduino as access point (AP mode, using WiFi.beginAP()) more than 80% of my packets are dropped (checked with Wireshark).

When connecting the Arduino to an existing access point (station mode, using WiFi.begin()) everything works as expected with rest of my code unchanged.

If I am using IP unicast instead of broadcast, both variants work as expected.

That's why I think that I hit a bug in WiFiNINA related to IP broadcast in AP mode.

Arduino version is 1.8.9, WiFi firmware version is 1.2.1.

Here is my example sketch:

#include <WiFiNINA.h>
#include <WiFiUdp.h>

char ssid[] = "broadcasttest";
WiFiUDP Udp;
int count=0;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // 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");
  } else {
    Serial.print("Firmware:");
    Serial.println(fv);
  }

  Serial.println("\nConnecting to WiFi.../Creating AP...");
  //WiFi.begin(ssid);
  WiFi.beginAP(ssid);
  Serial.println("Ready!");
  printWifiStatus();
  Udp.begin(2323);
}

void loop() {
    Udp.beginPacket(IPAddress(255,255,255,255), 7373);
    Udp.write((byte)40);
    if (!Udp.endPacket()) Serial.println("Error sending packet!"); else Serial.println("Packet sent!");
    Serial.println(count++);
    delay(1000);
}

void printWifiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}
cwerner77 commented 5 years ago

I could reproduce this problem on a WeMos D1 board using ESP8266WiFi instead of WiFiNINA.

There are also other reports on similar problems with esp32-based hardware (https://esp32.com/viewtopic.php?t=8948). I think this issue might be related to espressif's core implementation and not to WiFiNINA.

However, any hints are welcome!