fonction-distance.ino
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 Boron module (nRF52840, Wi-Fi + Bluetooth LE, cellular variant where applicable) with on-board antenna. None (D7 LED built in); a Particle account and an API token.
Wiring
Nothing to wire. Power: USB, LiPo or 3.6-5.5 V on VIN.
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
Install the Particle CLI (particle setup), flash with particle flash --serial or OTA (particle flash <name> firmware.bin). The Device Cloud app or REST API complete the deployment.
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.
}