🛠 Alerte email : bouton sur la Yún et envoi via sendmail

Arduino Yún · intermediaire · IMG-yun-intermediaire-6-yun-bouton-alerte-mail.svg — Yún avec bouton poussoir sur la pin 2, flèche vers une envelople email sortante et un pictogramme serveur SMTP ; en médaillon, la config SMTP dans la page arduino.local.

Objectives

Build a physical → email alert: a button on a 32U4 pin triggers an email sent from Linino OS. Two approaches are compared: the REST route /mail/send (zero dependencies) and the YunMail library (clean API on the sketch side). You will configure an SMTP account (your provider or Gmail with an app password) in the board's administration page.

Hardware

SMTP Configuration

In the administration page (http://arduino.local, password root), fill in: SMTP server, port, user, password, sender. Linino OS uses a lightweight sendmail; SMTP logs viewable via SSH (logread) help with debugging.

Complete Code

// Alerte email sur appui, avec anti-répetition de 30 s.
#include <Bridge.h>
#include <YunMail.h>

const uint8_t BTN = 2;
const uint32_t PAUSE = 30000;
uint32_t dernierEnvoi = 0;
bool dernierEtat = HIGH;

void setup {
  pinMode(BTN, INPUT_PULLUP);
  Bridge.begin;
  Console.begin;
  Mail.begin;   // YunMail : pilote la file sendmail côté Linux
  Mail.from("alerte@exemple.fr");
  Mail.to("moi@exemple.fr", "Moi");
  Mail.subject("Yun : bouton presse");
  Console.println(F("pret"));
}

void loop {
  bool etat = digitalRead(BTN);
  bool front = (etat == LOW && dernierEtat == HIGH);
  dernierEtat = etat;

  if (front && millis - dernierEnvoi >= PAUSE) {
    dernierEnvoi = millis;
    Mail.println(F("Quelqu'un a appuye sur le bouton."));
    Mail.println(F("Cordialement, votre Yuniste."));
    int id = Mail.send;     // met en file ; l'envoi part côté Linux
    Console.print(F("mail mis en file, id=")); Console.println(id);
  }
  // L'alternative REST, sans YunMail :
  // Process p; p.runShellCommandOnLinux(
  //   "curl -s 'http://localhost/mail/send/moi@example.fr/Alerte/Bouton'");
}

Expected Verification

Press → a few seconds later, the email arrives (sending is asynchronous: the queue is processed via sendmail on the Linux side). The Monitor displays the queue ID. Test failure: wrong SMTP password → nothing arrives; then check logread via SSH on the Linux side, authentication errors are logged there. The 30-second anti-repeat prevents spam in case of multiple presses.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials