veille-cloud.ino

Particle Photon · File : veille-cloud.ino · Language : Arduino · Use case : Surveiller la connexion réseau et recharger le lien cloud en cas de panne

SEMI_AUTOMATIC mode takes back control of the cloud link: the script blinks fast when the cloud is lost, slow when fine, and retries every minute. The deployment reflex for a battery board or moody Wi-Fi.

Hardware

A Particle Photon module (2.4 GHz Wi-Fi, STM32 microcontroller, metal shielding) powered by USB or VIN. None (D7 LED built in).

Wiring

Nothing to wire. Power: USB or 3.6-5.5 V on VIN (3.3 V max on the pins).

How it works

Particle.connected() reflects the link state; Particle.process() feeds the cloud state machine without blocking. After a long outage, WiFi.connect + Particle.connect restart the session.

Running it

With the Particle CLI: particle flash --serial (DFU mode, double reset) or OTA flash once the board is online. Safe Mode (reset + WiFi) covers troubleshooting.

Variants

Add a hardware watchdog (System.reset) if the link does not return after N minutes, or publish an alert event on disconnect.

veille-cloud.ino

// Indicateur de sante reseau : LED + Particle.process() manuel.
// Utile quand le module doit survivre a des coupures Wi-Fi.

unsigned long derniereVerif = 0;
bool etaitConnecte = false;

void setup() {
  pinMode(D7, OUTPUT);
  Serial.begin(9600);
  // Mode SEMI_AUTOMATIC : a nous de maintenir le lien.
  Particle.process();
}

void loop() {
  bool connecte = Particle.connected();

  if (connecte != etaitConnecte) {
    etaitConnecte = connecte;
    Serial.println(connecte ? "CLOUD: connecte" : "CLOUD: perdu");
  }

  // Feux de circulation : rapide si nuage perdu, fixe sinon.
  bool clignote = (millis() / (connecte ? 1000 : 150)) % 2;
  digitalWrite(D7, clignote);

  // Battee du lien cloud (non bloquant, a appeler souvent).
  Particle.process();

  if (millis() - derniereVerif >= 60000) {
    derniereVerif = millis();
    if (!connecte) {
      Serial.println("Tentative de reconnexion...");
      WiFi.connect();
      Particle.connect();
    }
  }
}

// En AUTOMATIC (defaut), le framework fait deja tout seul :
// ce montage sert aux builds SEMI/MANUAL ou tres occupes.

← Back to the board