🛠 Variables cloud et intégration HTTP

Particle Photon · intermediaire · Schéma : IMG-particle-photon-intermediaire-3-photon-variables-hook.svg — variables sensor et status exposées par le Photon, interrogées par un script Python via l'API REST

Objectives

Expose Photon data to the cloud using Particle.variable: a read-only integer (a sensor value) and a String writable from the cloud, then query and modify them from an HTTP script — the basis of an integration hook to any backend.

Hardware

Complete code

// photon-variables-hook.ino — variables cloud
int sensor = 0;
String status = "idle";   // les String sont inscriptibles depuis le cloud
unsigned long derniere = 0;

void setup {
  Particle.variable("sensor", sensor);  // lecture: GET .../sensor
  Particle.variable("status", status);  // ecriture: POST .../status
}

void loop {
  if (millis - derniere > 2000) {
    derniere = millis;
    sensor = analogRead(A0);            // 12 bits, 0..4095
  }
}

Expected verification

Read: curl -H "Authorization: Bearer $TOKEN" https://api.particle.io/v1/devices/<ID>/sensor returns the value. Write: curl -H "Authorization: Bearer $TOKEN" -d value=mode_nuit https://api.particle.io/v1/devices/<ID>/status modifies the String on the firmware side. In Python, requests.get followed by r.json does the same job.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials