🛠 Wi-Fi résilient, broche OFF et veille profonde

Arduino GIGA R1 WiFi · avance · Schéma : IMG-giga-r1-wifi-avance-8-connexion-robuste-off-pin.svg — Machine à états de reconnexion WiFi de la GIGA R1 WiFi, broches OFF (extinction matérielle) et VRTC (RTC sauvegardé) zoomées sur le bord de carte

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials