Broadcasting UDP over local AP - poor performance

I'm using an Arduino Uno Wifi Rev2. I have a simple PoC sketch that initializes the WiFi module and sends simulated NMEA data as UDP packets to the broadcast address. My setup code includes a pre-compiler switch to either connect to a known WiFi network (my house) or create a new access point. Code is below.

Once the sketch has initialized and is sending test data I run a separate client script I run on my local computer that just listens for UDP broadcast data and prints it out. I've run this script against many other UDP broadcast applications so I'm pretty sure it's solid.

Everything works great if the Arduino connects to a known WiFi network. The client script prints out every sent packet with only a slight delay.

But when it creates its own access point, the client script only receives sporadic packets, perhaps 25-40% of the ones sent by the Arduino.

Any idea why performance would be so poor for local access points?

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

WiFiUDP udp;
int wifiStatus = WL_IDLE_STATUS;

float nmeaDepth = 5;
unsigned long nextMsg;
const unsigned long msgSpace = 1250;

char wifiNetwork[] = "myhomenetwork";
char wifiPswd[] = "*********";
IPAddress udpAddr(255, 255, 255, 255);
IPAddress subnet;
unsigned int udpPort = 55554;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  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 < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }

// here is where I either connect to a known wifi network or create my own access point
#if 0
  while( wifiStatus != WL_CONNECTED ) {
    Serial.println("");
    Serial.print("Attempting connection: ");
    Serial.println(wifiNetwork);
    wifiStatus = WiFi.begin(wifiNetwork, wifiPswd);

    int wifiTest = 0;
    while( wifiTest++ < 10 && wifiStatus != WL_CONNECTED ) {
      wifiStatus = WiFi.status();
      Serial.print(".");
      delay(500);
    }
  }

  Serial.println();
  Serial.print("Connected to ");
  Serial.println(wifiNetwork);
#else
  char accessPoint[] = "Cloud Nine";
  wifiStatus = WiFi.beginAP(accessPoint);
  if( wifiStatus != WL_AP_LISTENING ) {
    Serial.println("Can't create access point");
    return;
  }

  wifiStatus = WL_CONNECTED;
  Serial.print("Broadcasting on ");
  Serial.println(accessPoint);
#endif

  if( ! udp.begin(udpPort) )
    Serial.println("begin broadcast failed");

  Serial.print("Ready. subnet mask is ");
  Serial.println(WiFi.subnetMask());
  subnet = WiFi.subnetMask();

  randomSeed(analogRead(0));
  nextMsg = millis() + msgSpace;
}



void loop() {

  char nmeaBuf[32], tmp[8];
  unsigned char *c, checkSum = 0;

  if( wifiStatus != WL_CONNECTED ) return;
  if( millis() < nextMsg ) return;

  dtostrf(nmeaDepth, 0, 1, tmp);
  sprintf(nmeaBuf, "$SDDPT,%s,0.4", tmp);
  c = (unsigned char *) (nmeaBuf+1);
  while( *c ) checkSum ^= *c++;
  sprintf(nmeaBuf + strlen(nmeaBuf), "*%02X\r\n", (int) checkSum);

  // adjust by a random value with the bounds of +/- 2 meters
  float depthOffset = (float) random(-200, 200) / 100;
  if( nmeaDepth + depthOffset < 0 || nmeaDepth + depthOffset > 50 ) depthOffset *= -1;

  nmeaDepth += depthOffset;

  Serial.print(nmeaBuf);
  if( udp.beginPacket(udpAddr, udpPort) ) {
    udp.write(nmeaBuf, strlen(nmeaBuf));
    if( ! udp.endPacket() ) Serial.println("endPacket failure");
  }
  else
    Serial.println("beginPacket failure");

  nextMsg = millis() + msgSpace;
}

Update: much better performance when creating an access point and sending UDP packets directly to my computer's local IP address as opposed to the "broadcast" address. So that suggests there's an issue with the broadcast address?

Why are you not using an ESP8266?

Paul__B:
Why are you not using an ESP8266?

I suppose primarily because the Uno Wifi Rev2 is what I had available. Is there some reason this shouldn't work, or to think that the ESP8266 would work better? I'm open to switching, but it would be good to have a better sense of why this isn't working and rule out programming issues on my part.

Well, I suspect it is better supported overall and would be easier to work with, but don't have the experience to look at your code as such.

If you have the old stuff and want to use it, fair enough, but if you were starting out, the ESP8266 is simpler, more powerful (which means, more responsive which may be your problem), more compact and vastly cheaper. :grinning:

Paul__B:
Well, I suspect it is better supported overall and would be easier to work with, but don't have the experience to look at your code as such.

If you have the old stuff and want to use it, fair enough, but if you were starting out, the ESP8266 is simpler, more powerful (which means, more responsive which may be your problem), more compact and vastly cheaper. :grinning:

Excellent points! But the Uno Wifi is not "old stuff," I think it was just released last year, no? Maybe I should reach out directly to tech support? Do they monitor these forums?

It might be released not too long ago however that are not so passive as the esp boards, thus less people will be able to help you.

I honestly don't think tech support will be there to help you out but you can try.

IF you decide to look into the ESP world, a small, cheap allrounder is the Wemos D1 mini.

Bringamosa:
IF you decide to look into the ESP world, a small, cheap all rounder is the WeMOS D1 mini.

An excellent recommendation. My favourite.

ZoggDC:
Excellent points! But the Uno Wifi is not "old stuff," I think it was just released last year, no?

So it appears, but this is the first I have heard of it. I can't see it getting much support on the price/ functionality point.

I honestly don't think tech support will be there to help you out but you can try.

Can confirm. I did get a reply but no real insights into the issue or helpful information.