🛠 Commander la Boron à distance avec Particle.function

Particle Boron · intermediaire · Schéma : IMG-particle-boron-intermediaire-2-boron-particle-function.svg — ordinateur portable et smartphone envoyant une requête HTTP vers le cloud Particle qui appelle une fonction sur la Boron pilotant un relais.

Objectives

Particle.function exposes a function from your firmware to the cloud: any client (CLI, curl, Python, web app) can call it via the REST API, and Device OS handles TLS authentication and device identity. Each device is a REST resource on https://api.particle.io — you POST a function call just as you would send a request to a micro-server. You will control an output (turning a load on/off via a relay) from the terminal, then understand the constraints: maximum 63-character argument, short function name, integer return value.

Hardware

Complete Code

// Boron — commande cloud via Particle.function
const uint16_t RELAIS_PIN = D5;    // broche sortie — verifier le pinout
bool chargeActive = false;

void setup {
    pinMode(RELAIS_PIN, OUTPUT);
    digitalWrite(RELAIS_PIN, LOW);
    Particle.function("setCharge", setChargeHandler);
}

int setChargeHandler(String args) {
    // args vient du champ POST "arg" (63 caracteres max)
    if (args == "on") {
        digitalWrite(RELAIS_PIN, HIGH);
        chargeActive = true;
        return 1;
    }
    if (args == "off") {
        digitalWrite(RELAIS_PIN, LOW);
        chargeActive = false;
        return 0;
    }
    return -1;                     // argument inconnu
}

void loop {
    delay(1000);
}

Client-side calls: particle call <device> setCharge on then particle call <device> setCharge off. In curl:

curl https://api.particle.io/v1/devices/<device_id>/setCharge \
  -d arg="on" -H "Authorization: Bearer $TOKEN"
# -> {"id":"...","return_value":1,"connected":true}

Expected Verification

Each CLI or REST call returns JSON with return_value equal to the value returned by your handler (1, 0, or -1), and the physical output toggles at the same time (milliseconds if the device is connected). Also test an invalid argument: you should receive -1, proving that the handler is executing correctly.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials