Want to learn 3D Printing, hurry up and register for our upcoming Workshop that starts Dec.16th

IoT: E-mail alert PIR sensor

Posted by Momen 06/12/2017 0 Comment(s)

 

Introduction:

In this tutorial we will use E-mail alert idea depending on Passive infrared sensor to know if anybody or anything are Pass in front of the PIR sensor , by using NodeMCU chip, then we will explain programming commands.

Before we start this tutorial, we will take a look on NodeMCU chip before we start a these project of Internet of Things (IoT).

The NodeMCU Mini is an ESP8266 WiFi micro-controller that runs at 80MHz processing speed and at 3.3V logic voltage. This micro-controller contains a Tensilica chip core as well as a full WiFi stack. You can program the micro-controller using the Arduino IDE software for an easy-to-run Internet of Things (IoT) core or using the Lua script language.

Now we will start our project:

Hardware Components and important links:

  • NodeMCU board.
  • ESPlorer program.
  • Bootstrap studio program.
  • Breadboard.
  • Plate NodeMCU board.
  • PIR: Passive infrared sensor.
  • Website (IFTTT): https://ifttt.com/ 

 

Create an account on IFTTT website (If This Then That):

This website provides many intelligence controlling tools that called (Recipe) and Notification tool is one of this tools that we will use on these tutorial.

Firstly we will go to the website:

https://ifttt.com/ 

Then we will press sing up to register on the website as we see on the image.

 esp8266 e-mail-alert-1

Then we will write the e-mail address and password, then we press on create account.

esp8266 e-mail-alert-2 

Then we will see this page:

esp8266 e-mail-alert-3 

Now click “this” to go for the next step.

esp8266 e-mail-alert-4  

Here we will click “that” to go for the next step.

esp8266 e-mail-alert-5 

Also we click “continue” to go for the next step.

 

esp8266 e-mail-alert-6 

Here we click “continue”.

esp8266 e-mail-alert-7 

On this page we will select our channels or websites that we love to following. Then we press continue.

After that we will select (Recipe) tool that we will use on our tutorial by going to this link:

https://ifttt.com/applets/315426p-esp8266-email-notifier 

After we sing in, we will see as these image:

esp8266 e-mail-alert-8 

From this page we will click connect “Marker channel” and connect “Gmail channel”.

*Important note:

In order to use this tool we should have a Gmail email account. We cannot use another account email.

………………….

 

After we click connect “Marker channel” we will see this window:

esp8266 e-mail-alert-9 

Here we press “Done”.

Also when we press connect “Gmail channel” we will see tow windows:

esp8266 e-mail-alert-10 

We click Allow access. Then done on the next window as in these image:

esp8266 e-mail-alert-11 

 

Now we will write these sentence (motion_detected) on the first box. Then we write our e-mail address. Then we click “Add”.

esp8266 e-mail-alert-12 

After that we will see this window:

esp8266 e-mail-alert-13 

We Click “Done”. Then we will go to this link:

https://ifttt.com/maker_webhooks 

After we sing in, we will see these window:

esp8266 e-mail-alert-14 

For me my secret key as we show in the image. We will copy it to use later.

*Now we will test if the tool is activated or not.

We will Copy this link to the browser:

 https://maker.ifttt.com/trigger/motion_detected/with/key/YOUR_API_KEY

Then we change “YOUR_API_KEY” to our secret key that obtained previously.

For me will appear like this:

 https://maker.ifttt.com/trigger/motion_detected/with/key/cnaIpXPkSACc_ZSUincyfQ

After we copy the link and change “YOUR_API_KEY” to our key and click “Enter” from keyboard. Should be see same as these result on the browser:

esp8266 e-mail-alert-15 

 We will note this sentence:

“Congratulations! You’ve fired the motion_detected event”

After this massage we will go to the e-mail, and we will note the massage that told us motion was detected as in these image:

esp8266 e-mail-alert-16 

Receiving notification of the email assures us that the tool works well.

*note if we are not find the notification of the email on “Inbox”, we check the “Junk mail”.

Hardware Design:

 

 
 esp8266 e-mail-alert

Now let’s have a look at the hardware overview of the entire circuit shown in the image. Connect them as shown:

 

We also connect the output to pin (D4) at the NodeMCU board.

 

Software Design:

The Code and explanation:

Copy these code to your ESPlorer software and save it with "ESPlorer_RGB_LED" name or any other name (no problem at this point), then upload the code.

wifi.setmode(wifi.STATION)

wifi.sta.config("your ssid","your pass")

pin = 4   

gpio.mode(pin, gpio.INT)

function onChange ()

    print('Motion Detected')

    conn = nil

    conn=net.createConnection(net.TCP, 0)

    conn:on("receive", function(conn, payload) end)

    conn:connect(80,"maker.ifttt.com")

    conn:on("connection", function(conn, payload)

        conn:send("POST /trigger/motion_detected/with/key/YOUR_API_KEY HTTP/1.1\r\nHost: maker.ifttt.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end)

    conn:close()

    print('Email Sent')

end

gpio.trig(pin, 'up', onChange)

 

 

esp8266 e-mail-alert-17 

  • Set up the ESP8266 chip to receive Wi-Fi. That is mean we will runs the ESP8266 as a client.
  • In the second line we have the function that have tow arguments :

"ssid" We will write the name of the network we want to connect to.

"pass" We will write the secret password of the network we want to connect to.

 

  • In line 3 we define a variable (pin), that is refers to the PIR: Passive infrared sensor.
  •  Line 4: we configured the input (D4) that connected to PIR sensor as (interrupt).

*(interrupt): a function used to interrupt the main code program to start another program. These interrupting working depend on the (Triggering).

* Triggering: it is a changing value (High, Low) that occurs on the (pin).depending on the situation.  


esp8266 e-mail-alert-18

  • Line 6: we create a function { onChange() } .
  • Line 8: print sentence “Motion Detected”
  • Lines 9-12: Port 80 used to make a connection between a server and client.
  • Lines 13-14: here will go to the link that it will active the notification tool and send a message to the e-mail that an object move front of the PIR sensor.

*note:

Change “YOUR_API_KEY” to our key.

 

esp8266 e-mail-alert-19 

  • Line 15: closing the connection.
  • Line 16: printing the sentence “Email sent”.
  • Line 17: end of the function { onChange() } .
  • Line 18: here they will test if they has a trig or not (it is testing the changing of value if it’s High or not, to execute{ onChange() } function if the value equal high.)

The command { gpio.trigاﺍ( pin, 'up', onChange ) } contain three argument values:

1) pin: refers to pin (D4) that using as interrupt.

2)  Up: refers to the changing situation from low to high.

3)  {onChange()}: name of the function that will be executed when Triggering operation is done.

 

  • Now we will upload the program on the NodeMCU chip and starting our experiment:

After finishing the code we will connect the NodeMCU chip to the computer. Then we select the port that’s connected to the NodeMCU chip. Also we will chose 9600 as the speed of data transfer as in these image:

esp8266 e-mail-alert-20 

If we are do not find the port, we should press on the refresh button.

Then we click on the activate button connection               esp8266 e-mail-alert-21         and the communication process will start as in these image:

esp8266 e-mail-alert-22

  • Then we will upload the program by click on ( save to ESP ) button. Then we will save the code at (init.lua) name.
  • After saving the code at (init.lua) name, then the code will upload as in these image:

 esp8266 e-mail-alert-24

Now how to get the IP address:

After uploading the program on NodeMCU chip the IP will appear in the below like in these image:

esp8266 e-mail-alert-25 

 

But if not appear, then we will write these command { =wifi.sta.getip() } in the transmission box as in these image :

esp8266 e-mail-alert-26

Then we press send, then the IP address will appear.

 

  • Few notes on the name of code file:

To run the program that was saved at ESP8266 properly, it is preferable to name all the files with (.init.lua) and save each code file at a new folder with same name of our project.

Let's have a look at Image below which explains the steps:

file-name

Finally:

We hope that you have enjoyed and benefited from this tutorial. We say goodbye until the next tutorial.

Leave a Comment