valeurs-partagees.ino

Arduino Yún · File : valeurs-partagees.ino · Language : Arduino · Use case : Échanger des valeurs entre le croquis et Linux (valuePut/Get)

The bridge value store is the shared MCU↔Linux variable: valuePut exposes a key readable by a plain local HTTP request, valueGet fetches a setpoint written from the Linux. Two lines for a full round trip.

Hardware

An Arduino Yun (ATmega32U4 + Linino Linux on an Atheros AR9331, Wi-Fi and Ethernet, microSD slot). 1 potentiometer or LDR on A0 for the demo.

Wiring

Wiper to A0, ends to 5V and GND.

How it works

Bridge.valuePut pushes the key to the bridge daemon on the Linux side, exposed under /data/<key>; valueGet re-reads a key the Linux wrote with a POST /data.

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 state keys (relay, mode) for a small automaton, or rotate the setpoint from a Linux cron.

valeurs-partagees.ino

// Echange de donnees par le magasin de cles du pont.
#include <Bridge.h>

const int CAPTEUR = A0;
unsigned long t0 = 0;

void setup() {
  Bridge.begin();
}

void loop() {
  if (millis() - t0 < 1000) return;
  t0 = millis();

  // 1) Publier la mesure : cote Linux -> GET /data/lumiere
  int brut = analogRead(CAPTEUR);
  Bridge.valuePut("lumiere", String(brut).c_str());

  // 2) Lire une consigne ecrite par le Linux (defaut 500).
  String consigne;
  if (!Bridge.valueGet("seuil", consigne) || consigne.length() == 0) {
    consigne = "500";
  }
  int seuil = consigne.toInt();

  if (brut < seuil) {
    Bridge.valuePut("alerte", "obscurite");
  } else {
    Bridge.valuePut("alerte", "ok");
  }
}

// Cote Linux (Yun) :
//   curl http://localhost/data/lumiere
//   curl -d "400" http://localhost/data/seuil

← Back to the board