🛠 L'API REST du Bridge en détail : /arduino, /data et routes personnalisées
Objectives
Document and seriously leverage the REST API embedded in Linino OS: it allows a browser, a smartphone, or a remote server to communicate with the pins and the Bridge without writing any web server. Intermediate level: we move beyond magic URLs to understand route families, verbs, and how to graft your own routes on the Linux side.
Hardware
- 1 Arduino Yún online on your network (http://arduino.local)
- PC with curl (or PowerShell Invoke-WebRequest)
- The BridgeYunServer sketch (IDE examples) uploaded, or any sketch with Bridge.begin
Essential routes
# --- Famille /arduino : accès direct aux broches ---
curl "http://arduino.local/arduino/digital/13/1" # digitalWrite(13, HIGH)
curl "http://arduino.local/arduino/digital/13" # lit la broche (si en entrée)
curl "http://arduino.local/arduino/analog/0" # analogRead(0) -> 0..1023
curl "http://arduino.local/arduino/analogWrite/9/128" # analogWrite(9, 128)
curl "http://arduino.local/arduino/mode/7/input" # pinMode(7, INPUT)
# --- Famille /data : le data store partagé entre les deux mondes ---
curl "http://arduino.local/data/send/temp:225/hum:60" # publie temp=225, hum=60
curl "http://arduino.local/data/get/temp" # lit une clé
curl "http://arduino.local/data/get" # tout le contenu
# --- Famille /mail : envoi d'email via sendmail côté Linux ---
curl "http://arduino.local/mail/send/to@x.com/sujet/mon+corps"
On the sketch side, the exact mirror exists: Bridge.put feeds /data, and the REST service drives the pins just as your code would. On the Linux side, these routes are served by a Lua daemon (uhttpd + luci-rpc); you can add your own routes by dropping a Lua file into /usr/lib/lua/ — the advanced web server tutorial details this.
Sketch code (32U4 side)
#include <Bridge.h>
void setup {
Bridge.begin;
Console.begin;
pinMode(13, OUTPUT);
}
void loop {
// Le sketch lit ce que le réseau publie, et publie en retour.
String ordre;
if (Bridge.get("ordre", ordre)) {
if (ordre == "on") digitalWrite(13, HIGH);
if (ordre == "off") digitalWrite(13, LOW);
}
Bridge.put("etat", digitalRead(13) ? "on" : "off");
delay(200);
}
Expected verification
curl http://arduino.local/data/send/ordre:on turns on the LED (the sketch reacts to the key); curl http://arduino.local/data/get/etat returns "on". Each family responds in plain text, integrable into a simple JavaScript fetch. Test a non-existent route: 404 response — the API is your attack surface, protect it (root password, no card exposed to the Internet).
Common errors
- Exposing the Yún directly to the Internet (port forwarding 80/8080): the REST API has NO authentication on the /arduino and /data routes — anyone can turn on your pins. SSH tunnel or VPN only.
- Believing /arduino/analog/0 is limited to 255: it is indeed 0–1023 (10-bit ADC of the 32U4).
- Forgetting the live sketch: without Bridge.begin in a sketch, the /arduino/* routes do not respond correctly — the bridge on the 32U4 side must be running.
- Strings with spaces or accents in /mail/send: encode them (spaces as +, @ sometimes problematic depending on versions) — test with curl before integrating.
- Non-persistent data store: /data lives in RAM on the Linux side; a restart clears the keys. For durability, write to the microSD (advanced tutorial).
Other tutorials for this board
- Premiers pas sur la Yún : Bridge, Console et LED
- Connecter la Yún au WiFi et piloter une broche depuis le navigateur
- Envoyer un capteur analogique sur le réseau : potentiomètre et Bridge REST
- runShellCommandOnLinux : faire exécuter des commandes Linux depuis le sketch
- Alerte email : bouton sur la Yún et envoi via sendmail
- Serveur web embarqué : YunServer, YunClient et pages dynamiques
Code examples for this board
clignotant-rest.inoArduinoconsole-bridge.inoArduinovaleurs-partagees.inoArduinoprocess-shell.inoArduinoserveur-json.inoArduinolecture-fichier.inoArduino