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 ESP8266. You can also use for example a radar module to detect the movement in the mailbox.

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):

AZDelivery ESP32 D1 Mini NodeMCU WiFi Modul + Bluetooth Internet Entwicklungsboard kompatibel mit Arduino
✔️Unser D1 ESP32 ist kompatibel mit der Arduino IDE, Lua und MicroPython und NodeMCU.
8,79 EUR
AZDelivery HC-SR501 PIR Bewegungssensor Bewegungsmelde Modul kompatibel mit Arduino inklusive E-Book!
✔️ PIR Modul zur Bewegungserkennung; ✔️ Betriebsspannung 5V. Ideal für...
4,49 EUR
Haobase 100x IN4007 DO-41 Gleichrichter Diode 1A 1000V
Dieser Gleichrichter Diode fuer fast alle Gleichrichterschaltkreise geeignet;...
4,90 EUR
Widerstand 1 K Ohm, 20 Stück, Metallschicht 0.6W 1% Metallfilm Widerstände
Bauform Axial 207; Toleranz +/- 1%; Abmessungen D: 2,5mm L: 6,8mm; 0,6W Belastbarkeit bei...
3,36 EUR
AZDelivery Breadboard Kit - 3x Jumper Wire m2m/f2m/f2f + 3er Set MB102 Breadbord inklusive E-Book!
✔️ Steckbrett für schnellen Aufbau elektronischer Schaltungen mit 830 Kontakten.;...
9,99 EUR

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 make your D1 Mini or ESP8266 available in your Arduino IDE to be able to program it from there.

Setting up the project

First assemble all parts on your breadboard as follows.

Set up on the breadboard
Set up on the breadboard

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 D0 by mistake, we use a diode to block current from this pin (look for the direction of the white ring). If you don’t have a diode at hand, the project should work with a simple jumper as a workaround.

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 without any time limit – that means it will only be woken up again by a signal from the motion sensor.

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 artificial intelligence on microcontrollers.

Letzte Aktualisierung am 2021-02-28 / Affiliate Links / Bilder von der Amazon Product Advertising API