🛠 Exposer des Variables cloud avec Particle.variable
Objectives
While Particle.function triggers an action, Particle.variable exposes a live value from the firmware, read on demand by the cloud: each device becomes a small telemetry server. Reading is done via CLI (particle get) or a simple REST GET on api.particle.io/v1/devices/<id>/<name>?format=raw, which facilitates integration with Grafana, Node-RED, or a simple script. You will expose several types (int, boolean, string), read them locally and remotely, and understand the "pull" model (the cloud reads the variable; the device does not push it).
Hardware
- 1× Particle Boron connected to the cloud
- Authenticated Particle CLI (
particle token createfor an OAuth2 token) - Optional: a second Particle device to subscribe and compare publish/variable
Complete code
// Boron — Variables cloud : telemetry exposee en lecture
SYSTEM_THREAD(ENABLED);
int rssi = 0;
bool radioActive = true;
String etat = "demarrage";
float temperature = 0.0f;
void setup {
Serial.begin(9600);
Particle.variable("rssi", rssi); // INT
Particle.variable("radioActive", radioActive); // BOOL
Particle.variable("etat", etat); // STRING
Particle.variable("temperature", temperature); // DOUBLE
}
void loop {
if (Cellular.ready) {
rssi = Cellular.RSSI;
etat = "connectee";
} else {
etat = "hors-ligne";
}
// temperature = lire_capteur; // votre logique ici
Serial.printf("rssi=%d etat=%s\n", rssi, etat.c_str);
delay(5000);
}
Readings: particle get <device> rssi then via REST:
curl "https://api.particle.io/v1/devices/<device_id>/rssi?format=raw" \
-H "Authorization: Bearer $TOKEN"
Expected verification
particle get returns the name, type, and current value of each variable, updated between two readings. The REST GET with format=raw returns the raw value, which is convenient for scripts; without this parameter, you get JSON with name, result, and coreInfo. Disconnect the antenna or turn off the board: the read fails (device offline), which confirms the pull model — the last value is not served from a buffer.
Common errors
- Modifying the variable reference (reassigning the object instead of updating the value): depending on the Device OS version, the cloud link may be lost; update the value in place.
- Expecting history: a Variable is not a database, it is an instant read point; for history, publish to an integration service.
- Incompatible types: a variable declared as INT read with a string expectation breaks clients; document the types.
- Forgetting the
Authorization: Bearerheader on the API: 401 error; the token is obtained viaparticle token createor the OAuth2 flow. - Confusing with
Particle.publish: publish = pushed event with short TTL; variable = state pulled on demand.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-evenement.inoArduinofonction-distance.inoArduinocapteur-dht.inoArduinoruban-neopixel.inoArduinopwm-respiration.inoArduino