fonction-distance.ino

Particle Photon · File : fonction-distance.ino · Language : Arduino · Use case : Exposer une commande actionnable à distance (Particle.function)

Particle.function turns a plain handler into a remote API: the cloud calls piloter() with an argument string and the LED obeys. It is the base brick for driving the board from a web button, IFTTT or Home Assistant.

Hardware

A Particle Photon module (2.4 GHz Wi-Fi, STM32 microcontroller, metal shielding) powered by USB or VIN. None (D7 LED built in); a Particle account and an API token.

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.function registers the handler; on a POST /functions the cloud stack runs piloter(args) and returns its integer result to the caller.

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 motor/relay commands, or validate a shared token inside the argument string for mini access control.

fonction-distance.ino

// Particle.function : une commande appelee par le cloud ou curl.
// curl -X POST https://api.particle.io/v3/devices/<id>/functions \
//   -d "name=piloter&args=led:on&access_token=<TOKEN>"

int piloter(String args) {
  if (args == "led:on")  { digitalWrite(D7, HIGH); return 1; }
  if (args == "led:off") { digitalWrite(D7, LOW);  return 0; }
  if (args == "led:toggle") {
    static bool etat = false;
    etat = !etat;
    digitalWrite(D7, etat);
    return etat ? 1 : 0;
  }
  return -1; // commande inconnue
}

void setup() {
  pinMode(D7, OUTPUT);
  Particle.function("piloter", piloter);
  Serial.begin(9600);
  Serial.println("En attente de commandes cloud");
}

void loop() {
  // Le handler est appele par la pile cloud quand une requete arrive.
  // Attention : rester court (< 1 s) pour ne pas bloquer le lien.
}

← Back to the board