Greetings community,
I'm fairly new to Arduino coding so please have patience with me
Been struggling to understand why ping() works for a short period of time then fails. It initially runs fine for an hour or two then pings returns -1.
My Wi-Fi runs fairly robust with having several dozen devices operating error free. So I doubt the issue is the Wi-Fi network.
The code includes a bunch of serial.print statements to help pinpoint where it's failing but to no alas. A conditional loop was added in the main loop() section to count the number of times it fails as a temporary solution. And after 10 attempts fail to re-establish a successful ping it forces the board to reboot. Not exactly the logic that I'm looking for but it does kick start the board back to an operational state for another hour or so.
The board is UNO Wifi Rev 2. Most of the code used was found online and some custom written. I had expanded the integers to 'long int' thinking maybe that was the issue, as is shown below.
I'd appreciate if you could look the code over, run it on same board, etc., and provide me some guidance what I'm doing wrong.
/*
This example connects to a encrypted WiFi network (WPA/WPA2).
Then it prints the MAC address of the WiFi 101 Shield,
the IP address obtained, and other network details.
Then it continuously pings given host specified by IP Address or name.
Circuit:
WiFi 101 Shield attached / MKR1000 or embedded WiFi circuitry as found in UNO WiFi R2.
created 13 July 2010
by dlf (Metodo2 srl)
modified 09 June 2016
by Petar Georgiev
modified 04 April 2024
by Endreola
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include <avr/wdt.h>
#include <utility/wifi_drv.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
long int status = WL_IDLE_STATUS; // the WiFi radio's status
long int i = 0; // the number of consecutive times ping failed
long int n = 0; // number of times it cannot connect to wifi
long int relay = 13; // Tells Arduino the relay is connected to pin 13
long int pingResult;
long int debug = 1; // set debug = 0 to suppress Serial.print diagnostic messages
// Specify IP address or hostname
String hostName = "www.google.com";
//String hostName = "1.1.1.1";
void checkWiFi() {
status = WiFi.status();
if (debug) {
Serial.println("Checking WiFi connection");
Serial.print("WiFi status = ");
Serial.println(status);
}
if (status != 3) {
// WiFiDrv::analogWrite(26, 32);
// Serial.println("Connecting to WiFi...");
connectWiFi();
}
}
void connectWiFi() {
status = WiFi.status();
while (status != WL_CONNECTED) {
if (debug) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
}
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 5 seconds before connecting:
delay(5000);
if (n >= 10) {
powerCycleIoT();
}
n++;
}
// you're connected now, so print out the data:
// Serial.println("");
// Serial.println("You're connected to the network");
printCurrentNet();
printWiFiData();
if (debug) {
Serial.print("WiFi Status = ");
Serial.println(status);
Serial.println();
}
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
if (debug) {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
}
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
if (debug) {
Serial.print("BSSID: ");
}
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
if (debug) {
Serial.print("signal strength (RSSI): ");
Serial.println(rssi);
}
// print the encryption type:
byte encryption = WiFi.encryptionType();
if (debug) {
Serial.print("Encryption Type: ");
Serial.println(encryption, HEX);
Serial.println();
}
}
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
if (debug) { Serial.print("0"); }
}
if (debug) { Serial.print(mac[i], HEX); }
if (i > 0) {
if (debug) { Serial.print(":"); }
}
}
if (debug) { Serial.println(); }
}
void printWiFiData() {
// print your WiFi 101 Shield's IP address:
IPAddress ip = WiFi.localIP();
if (debug) {
Serial.print("IP address : ");
Serial.println(ip);
Serial.print("Subnet mask: ");
Serial.println((IPAddress)WiFi.subnetMask());
Serial.print("Gateway IP : ");
Serial.println((IPAddress)WiFi.gatewayIP());
}
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
if (debug) { Serial.print("MAC address: "); }
printMacAddress(mac);
}
void powerOnIoT() {
if (debug) { Serial.println("Powering ON IoT."); }
digitalWrite(relay, HIGH); // Turn the relay on (HIGH is the voltage level = 1)
}
void powerCycleIoT() {
// WiFiDrv::analogWrite(27, 64);
if (debug) { Serial.println("Recycling power on IoT."); }
digitalWrite(relay, LOW);
if (debug) { Serial.println("Pausing 30 seconds to allow device to properly settle down."); }
delay(30000);
digitalWrite(relay, HIGH);
if (debug) { Serial.println("Pausing 10 minutes for device to fully recover after a power recycle."); }
delay(600000);
//Serial.println(">>>>>>>> uncomment the prior 2 lines");
n = 0;
}
void reboot() {
if (debug) { Serial.print("Rebooting..."); }
wdt_enable(WDTO_2S);
delay(500);
wdt_reset();
while (1) {}
}
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
}
WiFiDrv::pinMode(25, OUTPUT); //define GREEN LED
WiFiDrv::pinMode(26, OUTPUT); //define RED LED
WiFiDrv::pinMode(27, OUTPUT); //define BLUE LED
// wdt_enable(WDTO_4S);
//wdt_enable(WDTO_2S);
pinMode(relay, OUTPUT); // Initialize the Atmel GPIO pin as an output
powerOnIoT();
if (debug) { Serial.println("starting setup routine"); }
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
if (debug) { Serial.println("WiFi 101 Shield not present"); }
// don't continue:
while (true);
}
checkWiFi();
}
void loop() {
WiFiDrv::analogWrite(25, 32);
pingResult = WiFi.ping(hostName);
WiFiDrv::analogWrite(25, 0);
if (pingResult >= 0) {
i = 0;
if (debug) {
Serial.print("pinging ");
Serial.print(hostName);
Serial.print(", ");
Serial.print(pingResult);
Serial.println(" ms");
}
} else {
if (debug) {
Serial.print("Ping failure, result = ");
Serial.println(pingResult);
}
printCurrentNet();
printWiFiData();
i++; // Keep track of the number of times ping failed
if (debug) {
Serial.print("Looping ");
Serial.println(i);
}
checkWiFi();
}
// Serial.println("Pausing for 1 minute...");
delay(60000);
if (i == 10) { // ping failed 10 times in a row, reboot router
i = 0;
powerCycleIoT();
//setup();
//loop();
reboot();
}
}
TIA