🛠 Commander une LED à distance avec Particle.function

Particle Photon · intermediaire · Schéma : IMG-particle-photon-intermediaire-2-photon-fonction-distance.svg — appel REST POST vers api.particle.io déclenchant la fonction led du Photon qui allume D7

Objectives

Expose a firmware function to the Particle Cloud using Particle.function, then call it from the Internet via a simple HTTP request: the cloud calls your registered C++ function, which returns an integer.

Hardware

Complete Code

// photon-fonction-distance.ino — fonction appelée depuis le cloud
void setup {
  pinMode(D7, OUTPUT);
  Particle.function("led", ledCmd);   // enregistrement auprès du cloud
}

void loop { }

// Appelee par le cloud : POST /v1/devices/<id>/led?args=on
int ledCmd(String args) {
  if (args == "on")  { digitalWrite(D7, HIGH); return 1; }
  if (args == "off") { digitalWrite(D7, LOW);  return 0; }
  return -1;   // argument invalide
}

Expected Verification

From a terminal: curl -H "Authorization: Bearer $TOKEN" -d args=on https://api.particle.io/v1/devices/<Photon_ID>/led turns on LED D7; args=off turns it off; an unknown argument returns -1. The list of your devices (and the 24-character hexadecimal ID) is obtained with curl -H "Authorization: Bearer $TOKEN" https://api.particle.io/v1/devices.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials