IoT: E-mail alert PIR sensor – Zero Ohm Electronics

IoT: E-mail alert PIR sensor

Posted by Momen Odeh on



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


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: 

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


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


Then we will see this page:


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


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


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



Here we click “continue”.


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: 

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


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:


Here we press “Done”.

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


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



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


After that we will see this window:


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

After we sing in, we will see these window:


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:

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

For me will appear like this:

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:


 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:


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:



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.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:on("connection", function(conn, payload)

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


    print('Email Sent')


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




  • 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.  


  • 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.


Change “YOUR_API_KEY” to our key.



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


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

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


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


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:



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


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:



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

Share this post

Newer Post →

Leave a comment

Please note, comments must be approved before they are published.

Liquid error: Could not find asset snippets/bold-currency-converter.liquid