🛠 Envoyer un capteur analogique sur le réseau : potentiomètre et Bridge REST
Objectives
Take the first step from sensor to network: read a potentiometer on the ATmega32U4's analog input (10-bit ADC, 12 analog inputs on the Yún) and expose the value to the network via the Bridge REST API. You will understand the complete path: pin → 32U4 → internal serial port (pins 0/1) → AR9331 → HTTP → browser.
Hardware
- 1 Arduino Yún + power supply (WiFi consumes more than a Blink: prefer a stable power supply, a PC's micro-USB is sufficient)
- 1 10 kΩ potentiometer (pin 1 to 5 V, pin 3 to GND, wiper to A0)
- 1 breadboard + wires
- PC on the same network
Complete code
// Le Bridge expose les lectures analogiques via /arduino/analog/<pin>.
// Il suffit de faire vivre le pont : le service REST lit les broches en direct.
#include <Bridge.h>
void setup {
Bridge.begin; // attend que Linino OS soit prêt
Console.begin;
Console.println(F("Potentiometre pral sur A0"));
}
void loop {
// Optionnel : publier nous-mêmes une valeur dans le « data store » du Bridge,
// lisible via http://arduino.local/data/get/pot
static uint32_t derniere = 0;
if (millis - derniere >= 200) {
derniere = millis;
int v = analogRead(A0); // 0..1023 (ADC du 32U4)
Bridge.put("pot", String(v)); // route /data/get/pot
Console.print(F("pot=")); Console.println(v);
}
}
Expected verification
Three URLs, three views of the same reality: http://arduino.local/arduino/analog/0 returns the raw reading 0–1023; http://arduino.local/data/get/pot returns the value published by Bridge.put; the Serial Monitor displays the rate at 5 Hz. Turn the potentiometer: all three follow in near real-time. Also test from a smartphone: this is your first telemetry server.
Common errors
- Expecting a value between 0 and 255: the 32U4's ADC is 10-bit (0–1023), just like on the UNO — do not copy an 8-bit tutorial without adapting map.
- Writing Bridge.put on every loop iteration: this floods the internal serial port (115200 baud between the two processors) and saturates Linux; pace it (here 200 ms).
- Confusing /arduino/analog/0 (direct reading controlled by Linux) and /data/get/pot (the value YOU publish): the first reads the pin, the second reads a cache.
- The potentiometer wiper on the wrong pin: the value stops changing or jumps — check with the Serial Monitor before troubleshooting the network side.
- Bridge.begin without patience on first startup: if Linux hasn't finished booting, the Bridge retries; do not unplug after 5 seconds.
- Weak USB power supply: radio dropouts in the middle of a request; if WiFi restarts on its own, change the USB port/cable.
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
- runShellCommandOnLinux : faire exécuter des commandes Linux depuis le sketch
- L'API REST du Bridge en détail : /arduino, /data et routes personnalisées
- 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