Do you go to the mailbox every now and then in vain? That’s over now! This project sends you a Telegram message to your smartphone as soon as the letter carrier has delivered the mail.
For this we use a Wemos D1 Mini and a PIR motion detector. But of course you can also use another
Since we don’t have a power socket in the mailbox, we use a powerbank for the power supply and put the microcontroller into deep sleep most of the time. However, there is one requirement: The D1 Mini or ESP8266 must be able to connect to your Wi-Fi – this should reach your mailbox.
Advanced
1 – 2 hours
ca. $15
For this project you need (quantities see description):
Alternatively you can use a NodeMCU ESP8266 and/or a radar module:
Preparations
You might have to do a few things beforehand – you can find out how in these tutorials: First, set up a Telegram bot to receive messages from the D1 Mini on your smartphone. You also need to
Setting up the project
First assemble all parts on your breadboard as follows.
Let’s first look at the connection of pins D0 and RST: This allows you to wake up your microcontroller when a signal is sent from D0 to RST. In this project, however, this signal should come from the motion sensor. So to prevent it from being sent from pin
One more note:As long as the two pins are connected, you cannot upload a sketch. For this you have to disconnect the link first.
The alarm signal for the D1 Mini
So you wake up your microcontroller via a HIGH at the reset pin. But for this you need a clear change from LOW to HIGH. If you connect your PIR sensor directly to the reset pin, you will not get a correct signal, which can lead to several resets in a row – or simply does not work, as in our tests.
Following this tutorial, we install a NPN transistor (BC547), which helps us to transmit a definite signal from the PIR sensor to the reset pin of the D1 Mini. If this still leads to unwanted restarts (i.e. waking up), the linked tutorial also describes a setup with a capacitor that could help you.
In this tutorial you will learn more about the motion sensor HC-SR501.
The sketch
So let’s get to the code. First of all, you need a few libraries, which you include at the beginning of the sketch. If you haven’t already done so, install the latest version in the Library Manager.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
Then there are some definitions and constants for your Wi-Fi and Telegram bot credentials. As a reminder, you can learn how to get the required data for Telegram in the tutorial above.
//Your WiFi Credentials
const char* ssid = "NETWORK";
const char* password = "PASSWORD";
//Your Bot-Token
#define botToken "BOT-TOKEN"
//Your User ID
#define userID "USER-ID"
In the last step before the setup function, you create an instance of WiFiClientSecure and use it to initialize the connection to the Telegram bot.
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
The setup function
In the sketch for this project, you only need the setup function that runs after you start the Wemos D1 Mini. Here you start the internet connection and send the message that the postal mail is there to your smartphone (i.e. to your Telegram bot). Finally, you put your microcontroller back to sleep.
As soon as it receives a new signal from your motion detector, it wakes up again and runs through the setup again. So as you can see, you don’t need a loop.
First you configure the client you created above, start the Serial Monitor and output the message that your D1 Mini has been woken up:
client.setInsecure();
Serial.begin(115200);
Serial.println("Awake");
Then connect to your Wi-Fi network:
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("Connected!");
Finally, send a message to your Telegram bot using the bot.sendMessage() function – wait 2 seconds and put the D1 Mini back to sleep:
bot.sendMessage(userID, "Your mail has arrived!", "");
delay(2000);
Serial.println("Going to sleep");
ESP.deepSleep(0);
For the deep sleep you use the function ESP.deepSleep() with the parameter 0. This zero ensures that your microcontroller is put to sleep
And that was it – actually quite simple. Below you can copy the whole sketch.
//Libraries
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
//WiFi Credentials
const char* ssid = "NETWORK";
const char* password = "PASSWORD";
//Telegram Bot Credentials
#define botToken "BOT-TOKEN"
#define userID "USER-ID"
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
void setup() {
client.setInsecure();
Serial.begin(115200);
Serial.println("Awake");
//Connecting to WiFi
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("Connected!");
bot.sendMessage(userID, "Your mail has arrived!", "");
delay(2000);
Serial.println("Going to sleep");
ESP.deepSleep(0);
}
void loop() {
}
What’s next?
You now have a mail detector with the HC-SR501 motion sensor. Depending on the type of mailbox you have, a radar module may be a better choice.
But maybe you can think of completely different solutions to detect that your mailbox has been opened. A more nerdy and ambitious solution would be one that uses AI. At Pollux Labs, you can learn the basics of
Letzte Aktualisierung am 2021-02-28 / Affiliate Links / Bilder von der Amazon Product Advertising API