- NIXIE STYLE LED DISPLAYPosted 4 days ago
- TOTEM: learning by experimentingPosted 1 week ago
- Google Assistant Voice Controlled Switch – NodeMCU IOT ProjePosted 1 month ago
- Water Softener Salt Level MonitorPosted 1 month ago
- Sparkly Air SensorPosted 1 month ago
- Ultra sonic distance finder with live statusPosted 1 month ago
- Windows interface to have total control over lampsPosted 1 month ago
- Smart AquariumPosted 1 month ago
- Wearable ProjectionPosted 1 month ago
- Write programs with the Arduino Web EditorPosted 1 month ago
Build a Simple Presence Alarm system with email notifications
[bctt tweet=”Send alarm email notifications through #Gmail with #ESP8266 ” username=”OpenElectronics”]
Based on our ESP03 demoboard, this device sends out email notifications through Gmail when the onboard P.I.R. detects movement.
The traditional alarm system which just makes a sound alarm go off and maybe sends out an SMS, forcing you to cash out for recharging the built-in Sim Card, is not enough for you? Have you decided to take advantage of a simple and intuitive system in order to receive access notification anywhere in the world, even your cell phone, over the Internet? Are you beginners of the Arduino and IOT word and you would like to start out with a good and exciting project that will show your friends what you can do? Well, you don’t need to worry, because we have developed an ideal project to satisfy all your needs, based on the ESP03 Wi-Fi module and operating in the security field, which we are going to describe in these pages. In fact, the circuit is an alarm system that takes advantage of the “ESP03 demoboard” for the ESP03 Wi-Fi module, described in the post: Let’s play with the ESP-03 Demoboard
The above-mentioned module, besides being versatile and cost-effective, it is well fitted for applications requiring wireless Internet access, as we already showed you in the past.
While browsing the net, we found a very interesting example of ESP03 usage, created by an enthusiast who developed a library capable of sending out emails formed module. By using his library, the author of this article, therefore, decided to implement the code for managing an alarm system, which is easy and cheap but at the same time powerful and customizable, and can be added to any house today, without breaking a wall or calling your electrician.
In this post, we are going to provide you a step-by-step explanation about how to send out email notifications, to the advantage of a Gmail account and accessing the web through the connection carried out by the ESP03 module that can be found on our ESP03 demoboard.
Given the necessity to manage sensors and outputs using the demoboard, we have developed and created an additional shield which is ready for a P.I.R. sensor (movement sensor), a 12 V sound alarm output, and hardware key input (to enable/disable the alarm system) and an output for the 5 V buzzer or LED indicator. The shield has been designed in order to be applied to the ESP03 demoboard; everything is powered by 12 V applied on the shield, which in turn powers the ESP03 demoboard. Having just one power source for everything make the system very compact, as you can see from the prototype photos in these pages.
However, let’s see in detail what we are going to need to create our alarm system with “infinite” potential; let’s start from the hardware, which includes:
- The ESP03 demoboard FT1191;
- A USBA-mini USB cable;
- A printed P.I.R. sensor;
- A key switch;
- A 12V sound alarm (optional);
- Shield FT1307 to apply to the ESP03 demoboard;
- 12Vcc/0,5A adapter;
On the software side, this is what we are going to need to create our alarm system:
- IDE Arduino installed on a computer with the third-party driver for boards based on the ESP8266 chip, loaded and configured;
- Project esp03_gmail.ino for Arduino.
You also need a Gmail account, of course, to send out email notifications.
Since we have to send emails and take advantage of Gmail’s potentials, we suggest – if you already have a Gmail account for everyday use – to create a new, dedicated account for sending out the alarm notifications, so you don’t mix the two things up…
We’re going to use the SMTP protocol to send out the emails; for those of you who don’t know how it works, let’s just say that the SMTP protocol (Simple Mail Transfer Protocol) is the standard protocol that allows transferring emails from one server to another using a point-to-point connection.
When we send out a message (email) using an email client or a webmail client, that message is taken by the outbound mail server (SMTP) which starts up a conversation with the receiver’s inbound mail server.
Every free mail provider (such as Gmail or Hotmail) includes an associated SMTP server.
Before getting to the heart of how to set up our alarm system, let’s take a look at the shield containing the required hardware that is better described by the circuit diagram you can see in these pages. The board is going to be applied to the ESP03 demoboard using pin-strips since it has rows of pads with the same step and pin-to-pin compatible with the demoboard.
This is a low cost, Arduino-compatible development board based on the ESP03 Wi-Fi module and equipped with USB 2.0 interface connected to the module. This board allows anyone to learn, experiment with IoT (Internet of things) devices or just web-connected devices.
The hardware is included in Arduino’s IDE as third-party and requires a dedicated driver.
In our case, we are applying a shield to it, and the onboard electronics contains both the voltage regulator to operate the demoboard (to this purpose we have to short-circuit the D1 diode on it, as shown in the figure) and the components to interface the boards I/Os with the external world. In particular, the regulator is a 7805, which voltage comes from the input contacts PWR 12V (typically powered at 12 V, filtered by C2) and outputs 5 V stable with a maximum current of 1 A, filtered by C1 and C3.
The 5 V voltage goes to 5V and GND pads on the ESP03 demoboard side (connection FT1191 of the circuit diagram). The demoboard then gets 3.3 V stable thanks to its regulator and provides it to the 3.3 V line’s shield, used to power the pull-up resistors of the SENS inputs to which we can connect the sensors with clean contact output or with open-collector transistor referred to the GND contact (-). The inputs are read through the contacts IO12 (in common with the SENS input, used to read clean contacts, and in common with the P.I.R. output, which must be powered by 5 V by U1’s out) and IO13 (CHIAVE input, used to connect the key switch); please note that the P.I.R. has an open collector output, therefore is not relevant unless powered by 5 V because its output provides – at logic level high – the voltage allowed by the pull-up resistor R1.
IO14 line of the ESP03 demoboard pilots the piezoelectric BZ1 buzzer through the BC547 transistor which functions as static a switch and is polarized based on the voltage divider formed by our R3 and R4; the jumper is used to enable or disable the buzzer on the shield. T1’s connector is brought to the contact – of the LED output, which positive terminal is brought to the input line; this output is used, besides piloting the LED lights (with the limiting resistor integrated) to also pilot other utilizers absorbing less than 450 mA (the BC547 output a max of 500 mA) and working at 11÷12 V. Finally, IO16 pilots the n-gate MOSFET labeled BS170, which is an enhancement MOSFET that we are using as low resistance static switch to pilot a possible 12 V sound alarm through contacts + and – SIRENA.
First, let’s forget about the ESP module and move to our PC, starting by configuring everything we need on it, and we are going to start from the Gmail access. If you have created an account or are using an existing account, you need to edit an essential parameter that will allow us to send emails from Google.
Now open a webpage and log in to Google. In the top right corner of the login screen, let’s click on Login and then insert the email address and the login password for the account.
Now we click on the account name so that the pop-up shows up, as you can see in the figure, and from there you are going to click on the Personal account button.
Once you do that, you will see another page opening up with all the information regarding our account, and on the left side of the page you have to click on access and safety; on the right side, on the page, you will now see what is shown in the figure. Among the various access and safety settings for Gmail, we can find apps and linked websites.
Scroll down until you see Allow unsafe apps: if it’s OFF then you will have to switch the flag ON. This operation is necessary because it allow us to take advantage of the Gmail account in order to send out emails, otherwise Google’s restrictions would block the connection out.
Very well, now you have to convert the email address and the corresponding login password in encode format base64, through the website https://www.base64encode.org/. Piece of cake!
If you want, you could also integrate conversion code, however, since you are going to use it just once, you can easily open the webpage web Encode to Base64 format and insert our data as shown in figure . In the box Simply use the form below you must insert the email address to login to Google and then click on the Encode button. The code you will have to copy will be generated in the second box; now let’s do the same thing for the password.
If you’re not sure you have copied or coded correctly, copy the generated code, select web Decode from Base64 format in the page and paste the code itself, as shown in figure.
You now have to click on Decode and, if everything worked out fine, you will see appearing the name you had previously inserted.
Write down the two generated codes in a text editor (the email address and password to access Google). After that, we are finished, at least for what concerns the email configuration.
Remember that the generated codes will be used to allow the esp8266 to send out emails as if it were a user acting on a PC keyboard, however it will do that automatically (acting like a robot) thanks to the identification codes that will give access to the SMTP server.
Now we can focus on the Arduino’s IDE, we’re going to open it and load the simple code. We remind you that there are four files to be downloaded:
Let’s open the Gsender.h library and edit the following lines, with the corresponding codes you have generated from the website like so:
const char * EMAILBASE64_LOGIN = "bm9tZUBnbWFpbC5jb20 ="; // Enter the authentication email address to google // generated by encode64 encoding const char * EMAILBASE64_PASSWORD = "cGFzc3dvcmQ ="; // Enter the authentication password to google // generated by encode64 encoding
Still in the Gsender.h library, added the FROM line inserting your email address:
const char * FROM = "email@example.com"; // enter the name of the e-mail box // of google you will use to send messages // unencrypted and without encode64 encoding. The name of the The mail will then be mailed by the person receiving the mails.
You have thus set the authentication parameters for the Google server; all you have to do now is connect to your Wi-Fi home network (or where you are going to install the alarm system) and try to send a test email.
Now, save your Gsender.h module and open the esp03_gmail.ino file.
Configure the connection between the Wi-Fi module in the home network, inserts in the SSID name (“nome rete wifi”) and password:
const char * ssid = "Name of existing WiFi network"; // Enter the name of the wi-fi network we want // connect with our form (eg "alice0123456) const char * password = "WiFi network password"; // Enter the name of the Wi-Fi password we want // connect with our form (eg "pippopippo")
Now, all you have to do is decide who you want to send your notification emails to from your demoboard based on the ESP03 module.
Then, go to the send email line (you can find it after the comments, still within the file esp03_gmail.ino) and edit it using a valid email address, which is the email address you are going to send your emails to.
Now, you’re almost ready to run your first test; you just have to get the hardware ready.
Once the group “alarm system” demoboard + shield is complete you can go on and connect the sensors. On the board and you will find three pads labeled P.I.R. which you will have to connect the passive infrared radar like so:
- solder the red wire leading to the positive power terminal of the sensor to the pad +;
- solder the green wire it into the P.I.R. output to the central pad;
- solder the black wire leading to the negative power terminal of the sensor to the pad -.
On the terminal labeled “chiave” connect the two wires (no polarity) and solder each of them to a terminal of the key switch or another device with clean contact acting as a key. If you do not want to keep it, short-circuit the CHIAVE terminals of the shield.
As for the possible sound alarm, you have a 12 V output available; if you want to use it, connect the positive terminal of your siren to the “SIRENA +” terminal and the -12 “SIRENA -”.
If you do not wish to use the siren, the shield allows to get the sound notification from the piezo buzzer that you can use by closing the JBZ1 bridge, which you can easily create by soldering a section of spare resistance or capacitor between the same pads.
Now, connect the power adapter to the PWR12V terminal and respect polarity, the positive terminal of your siren must be connected to the “+12” e terminal and the -12 to “-12”. However, if you have any doubts you can refer to the wiring scheme shown in figure.
If you don’t connect the sound alarm you can also do without using the 12 V power adapter and power everything through a USB plug (in this case you don’t have to short-circuit D1).
Now, connect the USBA/miniUSB cabled to the ESP03 demoboard and download the firmware (please remember to select generic esp8266 board and the correct USB port, with a communication speed of 115.200 bps) by pressing and holding the prog button without releasing it, press reset for a second, finally, when you see the transfer has started, release the PROG button.
Now, open the serial (Tools/Serial Monitor) and press the reset button on the demoboard;
If nothing is wrong, you should see the program starting in the serial terminal (Arduino ide/ Tools/Serial Monitor); if this is not the case, press reset once more and wait 5-6 seconds for the program screen to appear on the serial Monitor.
Well, now we are going to briefly explain the program developed to create the alarm system: the key switch enables or disables the alarm, therefore we have inserted and on-time and off-time that can be modified by the user through this define:
#define TEMPO_ABILITA_ALLARME 30
//user-defined seconds amount
#define TEMPO_DISABILITA_ALLARME 30
// user-defined seconds amount
On-time is decreased only when we turn the key in the right direction and the words “unarmed enabled” appear on the monitor.
From the moment we see the words on the monitor we have a certain timeframe available (in this case 30 seconds
TEMPO_ABILITA_ALLARME) before the software enables the P.I.R. and goes in “motion detection” mode.
In case of movement or if you want to disable the pir module we have a certain timeframe available (in this case 30 seconds TEMPO_DISABILITA_ALLARME) before the software sends out the “motion detector to” notification via email.
If the module is off (not powered) and will leave the key switched on “unarmed enabled” we must turn it OFF and then ON again to reactivate the alarm system; this procedure is necessary because in case of a reset or a power outage, we might not know in which state our alarm system is, therefore risking to send out erroneous notifications.
The ON-time allows us to have all the necessary time to get out of the house and close the door without it detecting any movement, since the passive infrared (P.I.R.) sensor won’t be enabled yet.
Besides, we remind you that:
- When ON-time is out, the alarm system is active;
- The OFF-time is what we have from the moment movement is detected to disable the alarm system.
With that said, we must analyze the notification and the possible addition of the sound alarm.
For the email notification we have implemented a system which automatically connects to the Wi-Fi network, if available, and tries to send out the notification to the email address we had set earlier.
We also wanted to implement the option to save verification in the EEPROM’s internal memory, also called (E²PROM) which keeps its content even when power is out, therefore it can be used to save configuration info that must be present when the device (the ESP03 demoboard) is turned on again.
If the modules fails to connect to the Wi-Fi network, for instance because there is no network available, the software will save notification on the EEPROM and will reset the module. At launch, the program controls is the notification is saved in the memory and tries to send it out once again, by making your connection. When our alarm system manages to sends out the notification successfully, it finally deletes the notification saved on the EEPROM memory and goes in “reactivates alarm system” state. By turning the key OFF and then ON we can reactivate the alarm system.
If anything goes wrong, try to check the previous passages and don’t forget to insert a valid email address.
As you can see in this pages, our system represents a practical and monitor alarm system; you can however go wild and customize the code, for instance by adding Telegram to configure it the module in remote or implementing the activation of the external sound alarm, or you can also create and install several systems and have them talk to each other in order to monitor more places, in case the house you live in has multiple floors.
We hope we have enticed your curiosity, and enjoy your project!