Sub topic 11
Staying Connected Without Blocking
This chapter assumes you know setup(), loop(), Serial, and basic Wi-Fi terms such as SSID, IP address, and router. It also assumes a WiFiNINA board and the WiFiNINA library. The same design works with other Arduino Wi-Fi libraries when their status functions have similar behavior.
A Wi-Fi connection can disappear because the router restarts, the signal becomes weak, or the access point changes channels. A sketch that connects only once will then stop communicating. A reliable sketch checks the connection during every pass through loop() and starts another attempt when needed.
Why long delays cause problems
A blocking operation prevents the rest of the sketch from running. For example, this pattern waits ten seconds after every failed attempt:
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(10000);
}While the sketch waits, it cannot read a sensor, update an output, process a button, or service an MQTT connection. A short delay may be acceptable during startup, but repeated long delays make the device slow and unresponsive.
A non-blocking design returns to loop() quickly. It uses millis(), which reports the number of milliseconds since the board started, to decide when another attempt is allowed.
Retry with an interval
The following sketch tries to reconnect at most once every ten seconds. The rest of the application can continue running between attempts.
#include <WiFiNINA.h>
#include "arduino_secrets.h"
const unsigned long RETRY_INTERVAL_MS = 10000;
unsigned long lastAttempt = 0;
void ensureWifi() {
int status = WiFi.status();
if (status == WL_CONNECTED) {
return;
}
if (status == WL_NO_MODULE) {
Serial.println("Wi-Fi module not found");
return;
}
if (millis() - lastAttempt < RETRY_INTERVAL_MS) {
return;
}
lastAttempt = millis();
Serial.println("Wi-Fi is disconnected; starting a connection attempt");
WiFi.begin(SECRET_SSID, SECRET_PASS);
}
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Wi-Fi module not found");
while (true) {}
}
lastAttempt = millis() - RETRY_INTERVAL_MS;
ensureWifi();
}
void loop() {
ensureWifi();
if (WiFi.status() == WL_CONNECTED) {
// Read sensors, handle buttons, and perform network work here.
}
// Other time-sensitive work continues on every loop pass.
}The subtraction expression is intentional:
millis() - lastAttemptArduino's millis() counter eventually wraps back to zero. Unsigned subtraction continues to work across that wraparound, while comparing absolute times can fail.
The interval prevents a rapid retry loop. Rapid retries waste power, fill the serial log, and may cause the access point to reject the device temporarily. Choose a longer interval for battery-powered devices or networks that recover slowly.
Keep network clients alive
Reconnecting Wi-Fi does not automatically restore every higher-level connection. An HTTP client must create a new request. An MQTT client must reconnect to its broker and resubscribe to topics. Call an MQTT library's service function, such as poll(), on every loop pass while the connection is active.
A network operation should also have a timeout. Otherwise, a server that stops responding can create a different kind of blocking behavior.
Exercise
Exercise
Why does the sketch store lastAttempt instead of calling WiFi.begin() on every pass through loop()?
Show answer
It limits connection attempts to the retry interval, preventing a rapid blocking-like retry loop and allowing the rest of the sketch to keep running.