reconnexion-auto.ino

Arduino MKR WiFi 1010 · File : reconnexion-auto.ino · Language : Arduino · Use case : Survivre aux coupures WiFi avec une machine de reprise

A connected object must survive the router rebooting: this WiFi watchdog for the MKR WiFi 1010 detects link loss, retries connection, and after one minute of failure hard-resets the WINC1500 module. The robustness pattern for hands-off installations.

Hardware

An Arduino MKR WiFi 1010 (SAMD21 microcontroller, WINC1500 Wi-Fi module, no Bluetooth, USB or LiPo power on J6). MKR WiFi 1010, real access point; testable by simply switching the router off.

Wiring

No wiring; WiFi.end() then begin() is enough to reload the WINC1500 firmware.

How it works

The loop compares the WiFi state every pass; the depuisKO timestamp paces the attempts. The end/begin cycle reloads the module firmware, the only cure for WINC1500 SPI lock-ups.

Running it

Install Arduino SAMD Boards and the WiFi101 library, select the board, upload. Wi-Fi runs on 2.4 GHz only: check that your access point is not 5 GHz only.

Variants

Add a reboot counter in EEPROM to alert after N failures, or fall back to a backup access point if the main one disappears.

reconnexion-auto.ino

// Tuteur WiFi MKR WiFi 1010 : detecte la coupure, retente, reinit.
#include <SPI.h>
#include <WiFi101.h>

const char SSID[] = "MonReseau";
const char PASS[] = "motdepassewifi";

unsigned long depuisKO = 0;

void connexion() {
  WiFi.disconnect();
  WiFi.begin(WPA_PASS_PHRASE, SSID, PASS);
  unsigned long t = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t < 20000) delay(100);
  Serial.println(WiFi.status() == WL_CONNECTED ?
                 WiFi.localIP() : F("connexion KO"));
}

void setup() {
  Serial.begin(9600);
  connexion();
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    depuisKO = 0;
    activite(); // tache reseau normale ici
    return;
  }
  if (depuisKO == 0) {
    depuisKO = millis();
    Serial.println(F("WiFi perdu, reprise..."));
  }
  if (millis() - depuisKO > 60000) { // reinit materielle du module
    WiFi.end();
    delay(1000);
    depuisKO = 0;
  }
  connexion();
}

void activite() {
  static unsigned long dernier = 0;
  if (millis() - dernier < 300000) return;
  dernier = millis();
  Serial.println(F("tache periodique OK"));
}

← Back to the board