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;
}