🛠 Exposer des Variables cloud avec Particle.variable

Particle Boron · intermediaire · Schéma : IMG-particle-boron-intermediaire-3-boron-variables-cloud.svg — Boron publiant des jauges (RSSI, batterie, température) vers le cloud Particle, consultées par une requête HTTP GET depuis un serveur.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials