alerte-obscurite.ino

Arduino Yún · File : alerte-obscurite.ino · Language : Arduino · Use case : Détecter l'obscurité côté MCU, notifier côté Linux

Detection stays on the MCU (fast, real time), notification on the Linux (logger, mail, MQTT). This split of roles is the Yun/Tian philosophy: each processor does what it does best.

Hardware

An Arduino Yun (ATmega32U4 + Linino Linux on an Atheros AR9331, Wi-Fi and Ethernet, microSD slot). 1 LDR + 10 kΩ resistor as a divider on A0.

Wiring

LDR between 5 V and A0, 10 kΩ resistor between A0 and GND.

How it works

The threshold with hysteresis avoids oscillation at dusk; logger writes into the Linux syslog, readable via journalctl or relayed by a system script.

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

Replace logger with a curl to a notification API, or switch on a lamp driven by the Linux via GPIO/relay.

alerte-obscurite.ino

// LDR sur A0 : seuil franchi -> notification cote Linux (log + LED).
#include <Bridge.h>
#include <Process.h>

const int LDR = A0;
const int SEUIL = 300;          // a calibrer
const unsigned long HYSTERESIS = 30000;

bool nuit = false;
unsigned long dernierEtat = 0;

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

void notifier(const char *message) {
  Process p;
  p.runShellCommand(String("logger 'capteur: ") + message + "'");
  while (p.running());
  Serial.println(message);
}

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

  int valeur = analogRead(LDR);
  if (!nuit && valeur < SEUIL) {
    nuit = true;
    notifier("obscurite detectee");
  } else if (nuit && valeur > SEUIL + 80) {   // hysteresis 80
    nuit = false;
    notifier("retour a la lumiere");
  }
}

// Cote Linux : journalctl -f (ou logread) voit passer les alertes,
// et un script logrotate/cron peut les pousser par mail.

← Back to the board