From now on I will receive an email when the flap of my mailbox has been opened. For this I use the inexpensive ESP-01S module, which can dial into the WLAN and send e-mails. The battery-operated module is simply stuck in the appropriate position in the mailbox.
Info: A german version of this article (named Briefkastenwächter) is also available at paulwilhelm.de! 🙂
Download (software, schematic, layout)The Arduino sketch, the circuit diagram and the layout (images and EAGLE files) of the mailbox guard are also available in the GitHub repository!
Here's the link for setting up the SMTP2GO account needed for sending e-mails.
#include <ESP8266WiFi.h>
const char *wifi_ssid = "YOUR-WIFI-NAME";
const char *wifi_pass = "YOUR-WIFI-PASSWORD";
const char *mail_server = "mail.smtp2go.com";
const int mail_port = 2525;
const char *mail_user_base64 = "YOUR-BASE64-SMTP2GO-USERNAME";
const char *mail_pass_base64 = "YOUR-BASE64-SMTP2GO-PASSWORD";
const char *mail_sender = "YOUR-MAIL-SENDER"; // E-mail address that's shown as sender
const char *mail_recipients[] = { "YOUR-MAIL-RECIPIENT-1", "YOUR-MAIL-RECIPIENT-2" }; // Array of one or more e-mail recipients
const char *mail_subject = "IoT mailbox guard";
const char *mail_body = "The mailbox was opened.";
WiFiClient espClient;
void setup() {
Serial.end();
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
Serial.println();
blink();
Serial.println();
Serial.print("Connecting to WiFi: ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println();
Serial.println("IP: ");
Serial.println(WiFi.localIP());
for (int i = 0; i < sizeof(mail_recipients) / sizeof(const char *); i++) {
if (sendEmail(mail_recipients[i])) {
Serial.println();
Serial.println("E-Mail sent successfully.");
} else {
Serial.println();
Serial.println("Error!");
}
}
Serial.println();
Serial.println("Pulling RX LOW.");
Serial.println("Application done.");
blink();
// LED ON
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
// RX LOW
pinMode(3, FUNCTION_3);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
}
void loop() {
}
bool sendEmail(const char *recipient) {
Serial.println();
Serial.println("Connecting to mail server");
if (espClient.connect(mail_server, mail_port) != 1) return false;
if (!emailResp()) return false;
Serial.println();
Serial.println("Sending EHLO");
espClient.println("EHLO www.example.com");
if (!emailResp()) return false;
/*
Serial.println();
Serial.println("Sending TTLS");
espClient.println("STARTTLS");
if (!emailResp()) return false;
*/
Serial.println();
Serial.println("Sending AUTH LOGIN");
espClient.println("AUTH LOGIN");
if (!emailResp()) return false;
Serial.println();
Serial.println("Sending username");
espClient.println(mail_user_base64);
if (!emailResp()) return false;
Serial.println();
Serial.println("Sending password");
espClient.println(mail_pass_base64);
if (!emailResp()) return false;
Serial.println();
Serial.println(F("Sending sender"));
espClient.print("MAIL From: ");
espClient.println(mail_sender);
if (!emailResp()) return false;
Serial.println();
Serial.println("Sending recipient");
espClient.print("RCPT To: ");
espClient.println(recipient);
if (!emailResp()) return false;
Serial.println();
Serial.println("Sending data");
espClient.println("DATA");
if (!emailResp()) return false;
espClient.print("To: ");
espClient.println(recipient);
espClient.print("From: ");
espClient.println(mail_sender);
espClient.print("Subject: ");
espClient.println(mail_subject);
espClient.println("Content-Transfer-Encoding: 8bit");
espClient.println("Content-Type: text/html; charset=\"UTF-8\"");
espClient.println();
espClient.println(mail_body);
espClient.println(".");
if (!emailResp()) return false;
Serial.println();
Serial.println("Ending session");
espClient.println("QUIT");
if (!emailResp()) return false;
espClient.stop();
return true;
}
bool emailResp() {
byte responseCode;
byte readByte;
int loopCount = 0;
while (!espClient.available()) {
delay(1);
loopCount++;
if (loopCount > 20000) {
espClient.stop();
Serial.println("Timeout!");
return false;
}
}
responseCode = espClient.peek();
while (espClient.available()) {
readByte = espClient.read();
Serial.write(readByte);
}
return responseCode < '4';
}
void blink() {
// LED ON
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
delay(25);
// LED OFF
digitalWrite(2, HIGH);
pinMode(2, INPUT);
delay(25);
}
Update: The first 9 V block has now held out for 5 months. Only when the battery voltage reached 7 V was it finally over.
The circuit is supplied via a 9 V block and a downstream DC / DC step-down converter (3.3 V) as soon as the microswitch has been activated via the actuating pin on the letter box flap (the pin pops out).
A soft latch circuit allows the ESP-01S to switch off the entire circuit, including itself, after the work is done, so that almost no power is consumed in the idle state - even if the flap is clogged. I still have to determine the exact service life of a 9 V block in this configuration; the ESP-01S draws approx. 70 mA at 3.3 V. It takes about 10 seconds to send an email. (In the event of a disturbed WLAN reception, a timeout occurs after 20 seconds and the circuit is deactivated again.)
The actuating pin is a piece of carbon rod which is passed through a brass tube and secured against slipping out by a small glued-on end piece. With a drop of sewing machine oil and the use of a microswitch with a long lever, actuation requires relatively little force, so that the letterbox flap operates the switch safely in the “flap closed” state. A small additional circuit only generates a short current pulse when the switch is released, which activates the soft latch circuit.
External antennaOf course, a metal mailbox resembles a Faraday cage, so that the small printed antenna on the ESP-01S does not achieve a good range - especially not to the next floor. Fortunately, I still had an aging Linksys WRT54GL router flying around, which willingly sacrificed one of its two antennas:
For this I cut off the conductor of the printed antenna and instead soldered the coaxial cable of the external antenna and fixed it with some epoxy resin. This actually increased the range for sending e-mails sufficiently. (In contrast to the prototype, where I only soldered two short pieces of enamelled copper wire as a temporary external antenna.)
PrototypeI cooked the first version together on a breadboard, whereas the circuit board of the version shown above was of course milled on my DIY CNC! 🙂
An aluminum block serves as a spacer between the board and the mailbox wall. Double-sided adhesive tape fixes everything in the right position so that the letterbox flap pushes the carbon rod in completely when it is closed.
Features of the ESP-01SSince this is my first project with the ESP-01S, I first had to familiarize myself with the peculiarities of this module. I use the RX pin to turn off the soft latch circuit as I needed the TX pin for debug purposes and weird things are going on on both the GPIO0 pin and GPIO2 pin at startup:
GPIO0 apparently outputs a square wave signal with approx. 675 Hz...
... and GPIO2 seems to be sending some data. This behavior is of course unacceptable for the clean switching of the power supply.
Since the state of pins GPIO0 and GPIO2 determines the start mode (booting or flashing) of the module, I pulled both pins to HIGH with a pull-up resistor each to be on the safe side. I also switched the ENABLE and RESET pins HIGH.
Take a look at Code snippet #1 way below to see how to use the RX pin as a normal GPIO.
Resources usedGeneral information about the ESP modules:
- Overview of the ESP module family (Wikipedia)
- ESP8266 and ESP8285 module instructions (overview and pinout)
- Circuit diagram of the ESP-01S module (for those interested)
Sources of supply (no affiliate links):
- ESP-01S module on Ebay (shipping from Germany)
- ESP programmer on Ebay (shipping from Germany)
- Microswitch SCH48 on Ebay (shipping from Germany)
- Mini 360 DC / DC buck converter on Ebay (shipped from Germany)
Programming:
- Programming the ESP-01S WLAN module with the ESP programmer (modification of the ESP programmer)
- Program ESP-01 (ESP8266) with adapter - This is how it works (setup of the Arduino IDE)
Email and WiFi:
- How to Send an Email using ESP8266 WiFi Module? (Code for sending emails)
- ESP8266 Antenna Mod - extend WiFi range (soldering an external antenna)
Circuits:
- EEVblog # 262 - World's Simplest Soft Latching Power Switch Circuit (Soft Latch Circuit)
Comments