🛠 Alerte email : bouton sur la Yún et envoi via sendmail
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
- 1 Arduino Yún connected to the Internet (outbound SMTP port 465/587 required — some ISPs block port 25)
- 1 push button between pin 2 and GND (internal pull-up)
- A testable SMTP account (yours, or a test account)
- PC to configure arduino.local > "Mail" tab
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
- SMTP blocked by the ISP: outbound port 25 is often blocked by the router — use 465 (SSL) or 587 (STARTTLS) depending on the provider.
- Gmail without an "app password": basic authentication has been refused since 2021; create a dedicated app password (or use another SMTP provider).
- Expecting synchronous sending: Mail.send queues the message; the sketch continues. To know if it was sent, you must check the Linux logs.
- Exposing credentials in the code: SMTP credentials live in the Linux config (passed via the web page), not in the sketch — never hardcode them in the sketch.
- Confusing /mail/send (REST route, body in the URL, length limited) and YunMail (clean multi-line body): prefer YunMail for any non-trivial content.
- The body passed in the URL must encode spaces (+) and avoid reserved characters — a source of mysteriously truncated emails.
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
- Envoyer un capteur analogique sur le réseau : potentiomètre et Bridge REST
- runShellCommandOnLinux : faire exécuter des commandes Linux depuis le sketch
- L'API REST du Bridge en détail : /arduino, /data et routes personnalisées
- 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