Modifications

Sauter à la navigation Sauter à la recherche
1 975 octets ajoutés ,  7 janvier 2015 à 07:32
Page créée avec « {{Spark.IO-Could-API-NAV}} {{traduction}} == Introduction == {{bloc-etroit|text=To control a Core, you must first define and expose functions in the Core firmware. You then... »
{{Spark.IO-Could-API-NAV}}

{{traduction}}

== Introduction ==
{{bloc-etroit|text=To control a Core, you must first define and expose functions in the Core firmware. You then call these functions remotely using the Spark Cloud API.

Note: If you have declared a function name longer than 12 characters it {{underline|will be truncated}} to 12 characters.

Example: Spark.function("someFunction1", ...); exposes a function called '''someFunction''' and not '''someFunction1'''}}

== Sur le Core ==
Let's say, as an example, you create a Spark-powered coffeemaker. Within the firmware, we might expect to see something like this brew function.

<nowiki>/* FIRMWARE */
int brew(String args)
{
// parse brew temperature and duration from args
// ...

activate_heating_element(temperature);
start_water_pump(duration_seconds);

// int status_code = ...
return status_code;
}

/* FIRMWARE */
void setup()
{
Spark.function("brew", brew);
}</nowiki>


In a normal coffeemaker, '''brew''' might be called when a button on the front of the coffeemaker is pressed.

To make this function available through the Spark Cloud, simply add a '''Spark.function''' call to your '''setup()'''.

This exposes the brew function so that it can be called through the API. When this code is present in the firmware, you can make this API call.

== Appel d'API ===
The API request will be routed to the Spark Core and will run your '''brew''' function.

The response will have a '''return_value''' key containing the integer returned by '''brew'''.

POST /v1/devices/{DEVICE_ID}/{FUNCTION}

<nowiki>
# EXAMPLE REQUEST
curl https://api.spark.io/v1/devices/0123456789abcdef01234567/brew \
-d access_token=1234123412341234123412341234123412341234 \
-d "args=202,230"</nowiki>

Exemple de réponse

<nowiki>// EXAMPLE RESPONSE
{
"id": "0123456789abcdef01234567",
"name": "prototype99",
"connected": true,
"return_value": 42
}</nowiki>

{{Spark.IO-Could-API-TRAILER}}
29 923

modifications

Menu de navigation