🛠 Wi-Fi résilient, broche OFF et veille profonde
Objectives
Make a WiFi project autonomous and robust on the GIGA R1 WiFi: a reconnection state machine with exponential backoff, saving the wake-up context, and using the two power pins documented by Arduino — OFF (hardware power-off, hard shutdown) and VRTC (battery-backed RTC). You will also see the honest limitations regarding power consumption on this board, which is designed as a performance station rather than a battery-powered device.
Hardware
- Arduino GIGA R1 WiFi board + u.FL antenna, powered via USB-C or VIN 6–24 V (predecessors stopped at 12 V)
- Optional: 3 V coin cell battery on VRTC to preserve RTC time during power outages
- Know the OFF pin location on the edge connector (see the official board schematic)
Complete code
// GIGA R1 WiFi — reconnexion robuste + arret materiel via la broche OFF
#include <WiFi.h>
const char SSID[] = "your-ssid";
const char PASS[] = "your-pass";
const int PIN_OFF = 7; // broche utilisee pour commander le circuit d'arret
// (comportement exact a valider avec le schema officiel)
enum Etat { HORS_LIGNE, CONNECTE };
Etat etat = HORS_LIGNE;
unsigned long dernierEssai = 0;
unsigned long backoff = 1000; // 1 s, double a chaque echec, plafonne
const unsigned long BACKOFF_MAX = 60000; // 60 s
int echecsConsecutifs = 0;
const int SEUIL_ARRET = 10; // 10 echecs de suite -> extinction
void setup {
Serial.begin(115200);
pinMode(PIN_OFF, OUTPUT);
digitalWrite(PIN_OFF, LOW); // reste sous tension
WiFi.begin(SSID, PASS);
}
void loop {
switch (etat) {
case CONNECTE:
if (WiFi.status != WL_CONNECTED) {
etat = HORS_LIGNE; backoff = 1000; echecsConsecutifs = 0;
Serial.println("Liaison perdue, reconnexion...");
WiFi.disconnect; WiFi.begin(SSID, PASS);
}
// ... tache applicative ici (lecture capteur, publication MQTT...) ...
delay(1000);
break;
case HORS_LIGNE:
if (millis - dernierEssai < backoff) { delay(100); break; }
dernierEssai = millis;
WiFi.disconnect; WiFi.begin(SSID, PASS);
unsigned long t0 = millis;
while (WiFi.status != WL_CONNECTED && millis - t0 < 10000) delay(100);
if (WiFi.status == WL_CONNECTED) {
etat = CONNECTE; backoff = 1000; echecsConsecutifs = 0;
Serial.print("Retabli — IP "); Serial.println(WiFi.localIP);
} else {
echecsConsecutifs++;
backoff = min(backoff * 2, BACKOFF_MAX); // backoff exponentiel
Serial.print("Echec "); Serial.print(echecsConsecutifs);
Serial.print(", prochain essai dans "); Serial.print(backoff / 1000); Serial.println(" s");
if (echecsConsecutifs >= SEUIL_ARRET) {
Serial.println("Trop d'echecs : arret materiel (broche OFF).");
delay(50); // laisse partir les octets serie
digitalWrite(PIN_OFF, HIGH); // hard shutdown : coupe l'alim
while (true); // ne doit plus jamais revenir ici
}
}
break;
}
}
Expected verification
Cut the WiFi connection from your router: the board detects the loss, enters backoff mode (1 s, 2 s, 4 s... capped at 60 s), and reconnects cleanly when the network returns, displaying the IP address. After 10 consecutive failures without network, the OFF pin is triggered and the board powers down (power LED goes off) — only a reset press or re-powering will turn it back on. With a battery on VRTC, the time is preserved despite the power cut.
Common errors
- Confusing power-off with deep-sleep: the OFF pin is a hard shutdown (power cut), not a sleep mode with resume; the program starts from scratch. `>
- Backoff without a cap: without
BACKOFF_MAX, an unstable network can generate a reconnection storm that saturates the access point. WiFi.reconnectin a loop without state: putting a reconnect every 100 ms inloopis the best way to crash the router; the state machine above is the bare minimum.- Forgetting power consumption: the official datasheet does not publish typical consumption or temperature ranges; for a battery-powered device, other boards (Portenta with power management, or ultra-low-power MCUs) are more suitable.
- VRTC without a battery: without a coin cell battery, the RTC resets to a default date every time power is applied — problematic for TLS (see the HTTPS tutorial) and data timestamping.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton.inoArduinorespiration.inoArduinoanalogique.inoArduinodac-onde.inoArduinooled-i2c.inoArduino