🛠 L'API REST du Bridge en détail : /arduino, /data et routes personnalisées

Arduino Yún · intermediaire · IMG-yun-intermediaire-5-yun-api-rest-complete.svg — Yún au centre, autour d'elle quatre bulles HTTP : GET /arduino/digital/13/1, GET /data/get/temp, POST /data/send, GET /mail/send ; flèches navigateur, curl et smartphone.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials