clignotant-rest.ino

Arduino Yún · File : clignotant-rest.ino · Language : Arduino · Use case : Piloter la LED par REST depuis le côté Linux ou le réseau

The REST bridge is the Yun/Tian signature: the sketch becomes a local HTTP server. A curl on the Linux side (or a browser) toggles the LED — the instant demo of the MCU + Linux dual personality.

Hardware

An Arduino Yun (ATmega32U4 + Linino Linux on an Atheros AR9331, Wi-Fi and Ethernet, microSD slot). None: user LED built in.

Wiring

Nothing to wire. Board connected to the network (cable or Wi-Fi) for remote access.

How it works

Bridge.begin() opens the link to the Linux; YunServer listens on localhost and URLs under /arduino/... reach the sketch. The command read (on/off/toggle) drives the pin, the reply goes back over HTTP.

Running it

Arduino IDE: select the serial port to upload, then the Arduino Yun board for the monitor. Linux side: ssh root@arduino.local (distribution password). The Yunwizard app configures the network.

Variants

Add an /analog path returning A0 as JSON, or wire the URL to a button on a page served by the Linux.

clignotant-rest.ino

// Le croquis expose un mini-serveur REST : le cote Linux (ou un
// navigateur via http://arduino.local/) commande la LED utilisateur.
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer serveur;
const int LED = LED_BUILTIN;
bool etat = false;

void setup() {
  pinMode(LED, OUTPUT);
  Bridge.begin();          // attend le lien avec le Linux
  serveur.listenOnLocalhost();
  serveur.begin();
}

void loop() {
  YunClient client = serveur.accept();
  if (!client) return;     // pas de connexion : on revient vite

  String commande = client.readStringUntil('/');
  commande.trim();

  if (commande == "led") {
    String action = client.readStringUntil('/');
    action.trim();
    if (action == "on") etat = true;
    else if (action == "off") etat = false;
    else if (action == "toggle") etat = !etat;
    digitalWrite(LED, etat);
    client.print(etat ? "on" : "off");
  } else {
    client.print("led/on|off|toggle");
  }
  client.stop();
  delay(50);               // laisse le temps de fermer la connexion
}

← Back to the board