IOT PUSH NOTIFICATION WITH E-MAIL
Jan 22nd, 2020
| by: ELECTRONOOBS
Please see the past ESP8266 tutorial in order to learn more. You have a lot more details about this in that tutorial. We detect the door open with a hall sensor and a magnet. The Arduino will send the data to a database that we have on our website and we do that with the ESP8266. The php file on the website, will compare the received sensor value with a threshold value and if the received is higher, then we send a mail to our own account so we could know that somebody opened the door from anywhere in the world. This is a push notification made with ESP8266.
PART 1 - What we need
Part list is very short. We need the Arduino and the ESP8266. If you supply the Arduino at 3.7V you won't need the 1K and 2.2K resistors as a voltage divider. The ESP usually won't work at 5V. See the schematic below for more. We need a hall sensor with a 10K pullup and a magnet to palce on the door.
PART 2 - Schematic
Remember that we supply the arduino to the Vcc pin with 3.7V to 4.2V. More than that might damage the ESP8266. Don't worry, the Arduino can perfectly work with 3.7V. Mount everything as below. The D12 pin is connected to the Reset pin of the ESP8266 in order to take it out of Deep Sleep mode manually.
PART 3.1 - Create the DATABASE
Ok, now that we have the schematic and everything connected, we first need to prepare our database. The database is where the values are stored and then we show those values to the website interface, so we can see those from anywhere in the world. Step 1 is to create a domain and get a hosting. Hosting is where our website data will be. As in the past tutorial, you can use a free domain and hosting service from 000webhost. See past tutorial on how to get a domain and hosting.
Once you have your domain, go to your "hosting" part on your C-panel (1). Then enter the database menu by clicking manage (2). There create a new database. Click add new and give it a name. In my case I've named my database noobix_v1 (3). Finally, open the database details (4) in order to get the details such as database name, username, host and the password will be the one you've placed when created your dataabse. Now we have a database, but is empty.
PART 3.2 - Add new table
Ok, now open the created database, in my case the "noobix_v1" in php my admin. You will see that the database is empty, it has no tables. We meed to create a table where we will store the values for the id of the noobix, a password, the sensor value we receive, the threshold and the alarm status. As you can see on step (1), the database is empty. So, on the side on step (2), click add new table and give it a name. I give the name NOOBIX_V1_TABLE and I select 5 rows for all the values we need. Then click next. In the next step (3), you will have to insert the names of the rows as I did and the longitude value as well. The names must be the same, otherwise it won't work or you will have to change the php code. So, click save and we will have a table with rows for id, password, sensor value, threshold and alarm status.
PART 3.3 - Add new user
Ok, as you can see now, the table is empty. That means it has no user for a NOOBIX device. So, we have to add at least one id and a password. For that click the insert button (1). On the enxt step (2), add the id and password that you want but with a maximum of 5 characters. In my case I add id= "1" and password= "12345". Then click save, so now we have a user for a noobix device. So as you can see on step (3), the table now has one row with the id, passowrd and the rest, but the rest are empty because we haven't reveieced data yet.
PART 4.1 - Prepare the PHP files, Change SQL.php
Ok, go below and downlaod a zip file.Extract it. Inside you will have a folder called NOOBIX_V1 with 3 php files inside. You have to open each of these php files with your own desired software. I use Dreamweaver. In each php file you mush cange the database data according to your own database. Also the table name. I've placed comments in the php files so you should know where to change the data. First open Change_SQU.php file and change the database server, user name, password that you've used and the database name we've crearted before. Get those values form step (4) from Part 3.1 above. Then change the table namme from NOOBIX_V1_TABLE to the table name you have used.
FILES TO DOWNLOAD FOR THIS STEP
PART 4.2 - Index.php and RX.php
Now open index.php and change the database info and the table name as well. Save this file and then open the RX.php file. In the RX.php change all the data taht is between "/////////" lines because you will have to change the database info and table name a few times. Also, change the email address where you want the mail to be sent.
PART 4.3 - Upload the files
Save the files and uplaod the entire NOOBIX_V1 folder to your website, in my case I uplaod it to electronoobs.com. So, now if I go to electronoobs.com/NOOBIX_V1, I should get the interface and start using it. Now let's see the Arduino code.
PART 5.1 - The Arduino code
Now go below and download the Arduino code. Extract the zip file and open the folder. This code is for deep sleep of the Arduino and the ESP8266. Later you will have a different code that will send the data each 2 seconds without going to sleep. Open the NOOBIX_V1_ISR.ino file and change the data in the red square as below. Change the wifi name and password with the names you have. Then change the host from electronoobs.com to your own domain without using the "www". Do the same in the ESP8266_CONNECT but this time with the "www". Add the id and password of your noobix. In my case I've made that id 1 and password 12345. Save the file adn upload it to the arduino.
FILES TO DOWNLOAD FOR THIS STEP
PART 5.2 - Serial software error
If you compile the code at this moment, you will get an error. That’s because the software serial library is not compatible with the sleep library. For that search the software serial library on your computer. Usually is in :\Program Files (x86)\Arduino\hardware\arduino\avr\libraries adn there you find the SoftwareSerial folder. First make a backup of the software serial folder, in case that you mess it up. Open that and open the src folder. Then, open the softerserial.cpp file with administrator rights. Now search and delete the lines below. These vectors are in conflict with the sleep library. Save the file. Compile again and you will see no more errors. So, compile and upload it to the Arduino. Read the Arduino code step by step to understand more. Any way, upload, go to the website, move the magnet from the sensor and see how you will receive the error.
PART 5.3 - Second code
Go below and download the second Arduino code. Extract the zip file and open the folder. This code will constantly send the analog value from analog Read A0 of the Arduino each 2 seconds. Upload it and test.
FILES TO DOWNLOAD FOR THIS STEP
PART 6 - Demo video
PART 7 - Tutorial video