IoT Motion Sensor
In this project, we are going to learn how to make an Internet of things motion sensor which will give us alert on our phone when motion is detected at the place we installed the sensor. Everything works through the internet so there is no limitation of the distance between the sensor and the phone.
Video
Hardware Used
Components | AliExpress | Amazon.in | Amazon.com | Banggood | Utsource |
ESP8266 01 | |||||
PIR Motion sensor | |||||
ESP8266 serial adaptor |
Working Theory
- We have connected the PIR motion sensor to one of the GPIO pins of the ESP 8266 – 01.
- Whenever the PIR detects motion then the output sates of the PIR sensor will change which will be detected by our ESP 8266 – 01 controller.
- We have programmed the ESP to make an HTTP GET request to our IFTTT webhooks applet.
- Whenever the HTTP GET request has been made then it will trigger a notification alert on our mobile phone.
Pin Out Diagrams
Circuit
- Connect the GND of ESP 01 and PIR sensor to the negative of the power supply
- Connect the 3.3v of the power supply to the VCC of both ESP 01 and PIR sensor
- If you are not using 3.3v supported PIR Sensor then follow this tutorial to use 3.3v in cheap PIR modules.
- Connect the OUT of PIR sensor to the GPIO 2 pin of ESP
IFTTT Applet Creation
- Before creating Applet in IFTTT you need to create an account in the IFTTT web site. www.ifttt.com
- Also, download the IFTTT app for your Android or iPhone.
- Download the Android app from here.
- Download the iPhone app from here.
- After downloading the app login with your user credentials.
- Also, allow all the permissions which ask for.
Step 1:
- Click on your profile icon
- Then select the create option from the drop-down list.
Step 2:
- Now click on the +This button
Step 3:
- Now search for webhooks
- Then click on the Webhooks icon
Step 4:
- From choose trigger option, click on the “receive a web request”.
Step 5:
- Now give a name for the even
- I have written “iot_Motion_Sensor”.
- Note that there should not be any blank space between the words.
Step 6:
- Now clock on +That button
Step 7:
- Now from “choose action service”,
- Search for notification.
- And click on the notification icon.
- Note that you must be installed and logged in the IFTTT app on your phone for the notifications to work.
Step 8:
- Now from here choose any of the options you need
- I am going to choose simple notification instead of rich notification.
Step 9:
- Now in the message box write the notification which you needed to receive when the motion is detected.
- I am simply going to write “Motion Detected”
- If you have multiple sensors in place then you can write something like motion detected at garage or motion detected in closet etc..
- If you want you can give the time when the motion is detected.
Step 10:
- Now click finish
Step 11:
- That’s it now we have created the applet successfully.
Obtaining the HTTP GET request URL
Step 1:
- Log in to your IFTTT account.
- Click on your profile pic and choose My services
Step 2:
- Then select webhooks.
Step 3:
- Now click on Documentation
Step 4:
- Now replace {event} with our event name which we were given when we created our applet.
- In my case it was “Iot_Motion_Sensor”.
- There is a test button you can click on it to check whether your applet is working.
- Your secret key also will be on this page which you should not share with anyone.
- Don’t worry I have changed mine 😉
- You can click on the image to zoom.
Uploading and testing
- Now upload the code to your ESP8266 – 01 using an FTDI or use an Arduino as a USB to TTL converter and upload the code.
- Then connect the circuit and power it on.
- Wait for 30 seconds to calibrate the PIR sensor, when calibration is done the onboard LED will turn OFF.
- Now whenever motion is detected the onboard LED will blink and
- A push notification will arrive on your phone saying “Motion Detected”.
Code
// IoT motion sensor by www.diyusthad.com // www.youtube.com/c/diyusthad // www.hackser.io/najad #include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> const char* ssid = "*****"; //Your WiFI ssid const char* password = "*****"; //Your WiFi password boolean PIRstate ; //variable to store PIR state boolean lastPIRstate = HIGH; int PIR = 0; //PIR connected to GPIO 0 WiFiClient wifiClient; void setup () { WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); } pinMode(PIR, INPUT); //digitalWrite(PIR, LOW); pinMode(LED_BUILTIN, OUTPUT); delay(30000); } void loop() { PIRstate = digitalRead(PIR); //HIGH when motion detected, else LOW if (PIRstate != lastPIRstate) //Checking if there is any motion { digitalWrite(LED_BUILTIN, LOW); delay(100); digitalWrite(LED_BUILTIN, HIGH); if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status { HTTPClient http; //Declare an object of class HTTPClient http.begin(wifiClient,"paste the link from ifttt"); //Specify request destination http.GET(); //Send the request http.end(); //Close connection } lastPIRstate = PIRstate; } }
To buy electronic components order from UTSOURCE
This doesnt work if the IFFT request is in https format .. we need to use http only then the notifications work
Yes, you are right.
i,am used nodemcu and make all your step but not work can i know where a problem
Many are asking the same doubt, ill better do another article for it. Will update you by email when its ready
The waiting is over, Now make it with NodeMCU https://diyusthad.com/2020/05/iot-motion-sensor-nodemcu-esp8266-pir.html
hey, im having trouble uploading this code to my NODEMCU. The code is completely correct (very good job on the code!) though when i upload it to my NODEMCU nothing happens. I have tried all sorts of solutions online but none of them work. I would really appreciate it if you were to explain to me the problem and the solution, cheers!
Hey Dimo, this code will work for only ESP8266-01, not for NodeMCU. ill do necessary changes for NodeMCU and will share it with you in sha Allah.
The waiting is over, Now make it with NodeMCU https://diyusthad.com/2020/05/iot-motion-sensor-nodemcu-esp8266-pir.html
Can you give me the NodeMCU version as well thank you I need to finish this project in 24 hours
Hi Ted Lu, I’m working on it in sha Allah will be posting it by tomorrow.
The waiting is over, Now make it with NodeMCU https://diyusthad.com/2020/05/iot-motion-sensor-nodemcu-esp8266-pir.html
You are realy Usthad(master) like your nick name. Thanks
Hi, Enes thanks for your support.
Hello. I wonder if it is okay to connect the VCC to 3.3v as PIR sensor needs 5v to properly work. Correct me if I wrong. Nice tutorial anyway. Thank you so much.
Hi Huzniey, There are PIR sensors that support 3.3v. I have mentioned about it in the updated project with NodeMCU
Follow this tutorial to convert 5v PIR to 3.3v https://techgurka.blogspot.com/2013/05/cheap-pyroelectric-infrared-pir-motion.html
i cannot find the ESP8266HTTPClient.h
I want to use an IR sensor instead of PIR sensor. Can anyone help me with this?
pls give the library u included
I have done everything you said and inputed the info in to Aurdino as a script.
I am ending up with this error when verifing. How can I fix this?
// IoT motion sensor by http://www.diyusthad.com
// http://www.youtube.com/c/diyusthad
// http://www.hackser.io/najad
#include
#include
const char* ssid = “s****”; //Your WiFI ssid
const char* password = “sol*******”; //Your WiFi password
boolean PIRstate ; //variable to store PIR state
boolean lastPIRstate = HIGH;
int PIR = 4; //PIR connected to GPIO 4, NodeMCU pin D2
void setup () {
WiFi.begin(ssid, password);
Serial.begin(9600);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}
pinMode(PIR, INPUT); //digitalWrite(PIR, LOW);
pinMode(LED_BUILTIN, OUTPUT);
delay(30000);
}
void loop()
{
PIRstate = digitalRead(PIR); //HIGH when motion detected, else LOW
if (PIRstate != lastPIRstate) //Checking if there is any motion
{
Serial.println(“Motion Detected”);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
HTTPClient http; //Declare an object of class HTTPClient
http.begin(“https://maker.ifttt.com/trigger/iot_motion_sensor/with/key/c4XcivtEp2ES90z6Pq2fxWAtbFfUwqyLdhhGMLktTzR”); //Specify request destination
http.GET(); //Send the request
http.end(); //Close connection
}
lastPIRstate = PIRstate;
}
}
*Here is the error code
call to ‘HTTPClient::begin’ declared with atribute error: obsolete API, use ::begin(WiFiClient, url)
Compilation error: call to ‘HTTPClient::begin’ declared with attribute error: obsolete API, use ::begin(WiFiClient, url)
Step1:
Add to code at start
#include
WiFiClient wifiClient;
Step 2:
change in code line 43
http.begin(“Request link obtained from IFTTT”);
to
http.begin(wifiClient,”Request link obtained from IFTTT””);
Like i need to do this but i have esp32c so can you please help me???