I have built a plant watering system using an Uno R4 Wifi device. The software runs using local variables, only updating my Arduino Cloud dashboard when local variables such as plant moisture, water reservoir levels, and pump on/off status change. It's been running well for days. Last night dashboard updates stopped working and hasn't worked all day today. The time of the last dashboard update exactly coincides with my router reboot. Reboot is regularly scheduled and normally takes about 2-3 minutes to complete, after which router comes back online. The Uno R4 IP address did not change in this case, only a temporary loss of internet connectivity.
I am out of town for 2 weeks and am really hoping my plants are still getting watered based on the Uno local settings, and this is just an issue of the Uno losing internet connectivity to the dashboard.
My questions are:
What are the recommended ways to handle this condition in software going forward?
Adding a watchdog timer to reset the Uno?
Believe this is a required feature in case my software crashes but I would have expected underlying software to automatically attempt reconnect to the internet.
Add software to detect Cloud connectivity failures and attempt to reestablish connectivity? Pointers would be nice if such a solution exists.
Very curious what happens in the 4 lines of code where my dashboard variables get updated with the local variable, when internet connectivity is temporarily lost? Will the attempt to update a dashboard variable, crash or hang my Uno software? Or can i expect the software to gracefully continue without updating the dashboard?
What code is needed in the loop() to re-establish connectivity when it internet connectivity is finally restored.
I will reply to my own message with what I have since discovered in order to possibly help others....
Adding a watchdog to reset the Uno?
Answer: calling the function ArduinoCloud.begin(ArduinoIoTPreferredConnection,true); with the parameter "true" enables the watchdog timer on my device (Uno R4 WiFi).
Add software to detect Cloud connectivity failures and attempt to reestablish connectivity?
Answer: Added the following function called checkWiFiConnection() which both handles my routers scheduled reboot, as well as extended periods of internet failure . Here is the code snippet...
unsigned long prevInternetMilliSecs;
unsigned long currInternetMillSecs;
#define interval 3600000 // (1000ms * 60secs * 60mins) to restart WiFi if no connectivity after 60 mins
int attempts = 0;
/* check if WiFi still connected
*/
void checkWiFiConnection(){
IPAddress newIP;
// Checking WiFi connection
Serial.println("checking WiFi connection");
// It's possible that current IPAddress is set to expire
// so call the maintain() function to renew IPAddress
byte rtnVal = Ethernet.maintain();
switch(rtnVal) {
case 1: Serial.println(F("\r\nDHCP renew fail"));
break;
case 2: Serial.println(F("\r\nDHCP renew ok"));
break;
case 3: Serial.println(F("\r\nDHCP rebind fail"));
break;
case 4: Serial.println(F("\r\nDHCP rebind ok"));
break;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("wifi is connected");
//print the current IP address
newIP = WiFi.localIP();
Serial.println(newIP);
if (newIP != currIP){
Serial.print("IPaddress has changed from ");
Serial.print(currIP);
Serial.print(" to ");
Serial.println(newIP);
currIP = newIP;
}
attempts = 0;
prevInternetMilliSecs = millis();
currInternetMilliSecs = prevInternetMilliSecs;
return;
}
Serial.println("wifi is not connected");
// get current time...
currInternetMilliSecs = millis();
// restart internet once we pass the required interval duration
if (currInternetMilliSecs - prevInternetMilliSecs >= interval){
Serial.println("WiFi connection is down, trying to reconnect");
// reset previous time to now
prevInternetMilliSecs = currInternetMilliSecs;
// See if IP Adress has changed for this MAC Address
// if large enough delay,try reconnecting and
// keep track of attempts
Serial.println("-------------------------------------");
Serial.print("Attempting to re-connect to WiFi ");
Serial.print(++attempts);
Serial.println(" attempts so far");
Serial.println("-------------------------------------");
WiFi.begin(SSID,PASS);
}
}
What happens to dashboard variables when internet connectivity is lost.
Answer: Nothing....Variables will get updated when internet connectivity gets re-established, otherwise local code continues to function as expected.
Code in loop() to re-establish connectivity?
Answer: I just call the checkWiFiConnection() function within my loop to handle this case. Like I stated earlier, handles both scheduled router reboots and extended periods while internet is disconnected.
Thanks for sharing - i have a connection drop issue with r4 in my garage but works great in house so will give the restart a try.
I also had questions about variables on reboot - I personally did not want the r4 to restart from where it left off so I used call back function explained here