serveur-json.ino
The sketch becomes a JSON API: YunJSON builds the object (light, button, uptime) and YunServer serves it on localhost. Node/Python scripts on the Linux side consume hardware state like a microservice.
Hardware
An Arduino Yun (ATmega32U4 + Linino Linux on an Atheros AR9331, Wi-Fi and Ethernet, microSD slot). 1 button on D2 to GND + LDR/potentiometer on A0.
Wiring
Button between D2 and GND (internal resistor); analog sensor on A0 as usual.
How it works
YunServer receives the HTTP request relayed by the Linux, the sketch builds the JSON body by hand and writes it into the client. The Linux path /arduino/etat arrives as "etat".
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
Accept POSTs to actuate pins (parse incoming JSON), or push the JSON to an MQTT broker via the Linux.
serveur-json.ino
// YunServer + JSON : l'etat des entrees en API locale.
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer serveur;
void setup() {
Bridge.begin();
serveur.listenOnLocalhost();
serveur.begin();
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
YunClient client = serveur.accept();
if (!client) return;
String chemin = client.readStringUntil('/');
chemin.trim();
if (chemin == "etat") {
// JSON construit a la main (pas de dependance externe).
String json = "{";
json += ""lumiere":" + String(analogRead(A0)) + ",";
json += ""bouton":" + String(digitalRead(2) == LOW ? "true" : "false") + ",";
json += ""uptime_s":" + String((long)(millis() / 1000));
json += "}";
client.print("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n");
client.print(json);
} else {
client.print("HTTP/1.1 404 Not Found\r\n\r\n");
}
client.stop();
delay(50);
}
// Bouton sur la broche 2 vers GND (pull-up active).
// Re tester : curl http://<carte>/arduino/etat