This code works, even though it shouldn't. Should I leave well enough alone?

The following code works. It sends the # 1 to my Arduino Access point.

void loop() {

          Udp.begin(localPort);
          Udp.beginPacket(destinationIP, destinationPort); 
          Udp.write("1");
          Udp.endPacket();
          Udp.stop();

 
}

It stops after about 8 times. Odd? So I changed one line at the top:

void loop() {
          printWifiStatus();<-----_CHANGE
          Udp.begin(localPort);
          Udp.beginPacket(destinationIP, destinationPort); 
          Udp.write("1");
          Udp.endPacket();
          Udp.stop();
}

Now, it loops forever (good). But why? PrintWifiStatus() is simply output:

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

So I guess my question is, why does this fix the bug, and what causes that bug in the first place? There is no reason for it stop sending after about 8 number 1’s.

you left out all the everything else. so how can we know?

Ok, here is the complete code. Not much more to it.

#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
 
int status = WL_IDLE_STATUS;
 
char ssid[] = "MyAP";       
char pass[] = "MyPWD";  
 
 
IPAddress destinationIP(192, 168, 4, 1);  
unsigned int destinationPort = 6555;    
unsigned int localPort = 9999;     
 
 
WiFiUDP Udp;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
 
 
 
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
 
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  
 
}

void loop() {
   
          printWifiStatus();
          Udp.begin(localPort);
          Udp.beginPacket(destinationIP, destinationPort); 
          Udp.write(count);
          Udp.endPacket();
          Udp.stop();

 
  
}

 

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

For the benefit of the Arduino Community, I modified the code a bit. This allows the client to see if the server came offline for some reason, and if so, reconnects.

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");

   while (rssi ==0) {
    WiFi.end();
    Serial.print("RECONNECTING 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);
     rssi = WiFi.RSSI();
    // wait 10 seconds for connection:
    delay(10000);
  }
}

mydogcooperisapita:
Now, it loops forever (good). But why? PrintWifiStatus() is simply output:

Likely because it takes time and therefore slows down your loop. Depeding on where the network is handled e.g. a separate module. That module might still be busy. Try a short delay instead.