IoT Based Motion Detector

IoT Based Motion Detector

HC-SR501 Motion Sensor usually known as the PIR sensor is used in many kinds of Security Alarm Systems and Motion Detector systems. Instead of emitting infrared signals, it absorbs IR signals and that’s why it is called PIR (Passive Infrared) Sensor. Every object emits heat in the form of IR rays, so whenever the PIR sensor detects any change in heat, its output pin becomes HIGH. The key components of the PIR sensor are the Pyroelectric sensor and Motion Detector IC BISS0001. BISS0001 IC takes the data from the sensor and measures the output pin HIGH or LOW accordingly.

Previously we used the PIR Sensor with Raspberry Pi to build a security system. Today we will use a PIR sensor with NodeMCU to build an IoT Motion Detector System which not only sends an email whenever it detects some motion but also shows it on a webpage with time and date.

 

Components Required

  • NodeMCU ESP8266
  • Motion Sensor HC-SR501 (PIR Sensor)
  • Buzzer
  • Jumper Wires

 

Circuit Diagram

The circuit diagram of the IoT Motion Sensor Circuit is shown below:

IoT Motion Sensor Circuit Diagram

 

The circuit is very simple as we are only connecting the PIR sensor and buzzer with NodeMCU. VCC and GND pins of PIR Sensor is connected to VIN and GND of NodeMCU, while the Data pin is connected to D1 (GPIO 5) pin of NodeMCU. The positive and GND pins of the buzzer are connected to D5 (GPIO 14) and GND of NodeMCU respectively.

IoT Motion Detector

 

IFTTT Setup for IoT Motion Detector

IFTTT (If This Then That) is a web-based service by which we can create chains of conditional statements, called applets. Using these applets, we can send Emails, Twitter, Facebook notifications, SMS, etc. We have used IFTTT in many IoT based projects, you can find all of them by following the link.

To use the IFTTT, sign in to your IFTTT account if you already have one or create one.

Now search for ‘Webhooks’ and click on the Webhooks in Services section.

IFTTT Webhooks

 

Now in the Webhooks window, click on ‘Documentation’ in the upper right corner to get the private key.

Copy this key. It will be used in the program.

IFTTT Key

 

After getting the private key, now create an applet using Webhooks and Email services. To create an applet, click on your profile and then click on ‘Create.’

IFTTT Applet

 

 Now in the next window, click on the ‘This’ icon.

IFTTT

 

Now search for Webhooks in the search section and click on ‘Webhooks.

IFTTT Webhooks Service

 

Now choose ‘Receive a Web Request’ trigger and in the next window, enter the event name as motion_event and then click on create trigger.

IFTTT Trigger Field

 

After this, click on ‘Then That’ and then click on Email.

Choose Action For IFTTT

 

Now in email, click on ‘send me an email’ and enter the email subject and body and then click on create action.

IFTTT Configuration

In the last step, click on ‘Finish’ to complete the Applet setup.

 

Programming NodeMCU for IoT Motion Sensor

Complete code for the Motion Detector system can be found at the end of the page. The code can be divided into two parts. The first part is used to read the motion sensor data and performing other tasks like sending an email, and the second part is the HTML code that is used to create web server and visualizing the data. Here we are explaining the complete code line by line.

 

So, start your code by including all the required libraries. The ESP8266 libraries are pre-installed in Arduino IDE. 

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

 

After that, make instances for Wi-Fi name, Wi-Fi password, IFTTT host name, and private key. Enter the IFTTT private key that you copied from the IFTTT.

const char* ssid = "Wi-Fi Name"; 
const char* password = "Password";
const char *host = "maker.ifttt.com";
const char *privateKey = "Private key";

 

After that, define the pins where you connected the PIR sensor and Buzzer.

int buzzer = 14;  
int PIRsensor = 5; 

 

The handleRoot function is executed when we open the Webpage in our browser using the NodeMCU IP address.

void handleRoot() 
{
String s = MAIN_page; 
server.send(200, "text/html", s);
}

 

The next function that is readData() is used to read the data from the PIR sensor and send it to the Webpage. In this loop, whenever the PIR sensor goes high, it stores the message in a string and sends this to the Webpage whenever requested.

int state = digitalRead(PIRsensor); 
    delay(500);                         
  if(state == HIGH){ 
     Message = "Motion Deteced";
     String data = "{\"Activity\":\""+ String(Message) +"\"}";
     server.send(200, "text/plane", data);   
     send_event("motion_event");               
     digitalWrite (buzzer, HIGH);    
     delay(1000);
     digitalWrite (buzzer, LOW);

 

Inside the void setup() function, we initialized the baud rate and webpage using the server.begin() function and then connect the module with the Wi-Fi using the Wi-Fi name and password. Also, define the PIR sensor pin as input and Buzzer pin as an output.

Serial.begin(115200);
WiFi.begin(ssid, password);
server.begin();
server.on("/", handleRoot);     
server.on("/readData", readData); 
pinMode(PIRsensor, INPUT); 
pinMode(buzzer, OUTPUT);

 

The void loop() function continuously listen for HTTP requests from clients.

void loop
{
server.handleClient();          
}

 

HTML Code Explanation

As mentioned above, the HTML code is used to create a webpage to show the time and date of Motion detection. The important parts of the code are explained below. 

The <!DOCTYPE html> tag is used to tell the web browser that which version of html we are using to write the html code. This tag is written at the top. Everything in this code can be written after that.

<!doctype html>

The code written between the <html></html> tags will be read by the browser. The <head></head> tag is used to define the title, header line and style of the web page. The data written in the <title></title> is the name of the tab in the browser. The <style></style> tag is used to style the table and the header lines.

<html>
<head>
<title>Data Logger</title>
<h1 style="text-align:center; color:red;">Iot Design Pro</h1>
<style>
  canvas{
    -moz-user-select: none;
……………………..
……………………..
……………………..
</style>
</head>

 

The <script></script> tag is used to include the jQuery. The jQuery is the JavaScript library. The getData() function inside the <script> tag is used to get the data from NodeMCU and update the data table.

function getData()
{
…………..
…………...

 

The XMLHttpRequest object is used to request data from the webserver. All browsers have a built-in XMLHttpRequest object to request data from a server. Using the XMLHttpRequest, we can update a web page without reloading the page. Here we are using this object to get the Date and Activity data from the NodeMCU and update the data table without refreshing the web page.

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var date = new Date();
var txt = this.responseText;
var obj = JSON.parse(txt); 
Avalues.push(obj.Activity);
dateStamp.push(date);
……………………….
…………….................
……………………….

 

The open() and send() methods of the XMLHttpRequest object are used to send a request to a server. The syntax for xhttp.open() is given below:

xhttp.open(method, url, async)

Where:

method is the type of request (GET or POST) 

URL is the server location

async is used to define asynchronous or synchronous, where true is asynchronous and false is synchronous.

xhttp.open("GET", "readData", true); 
xhttp.send();

 

Testing the Setup

Once your hardware and code are ready, connect the NodeMCU to your laptop and upload the code using Arduino IDE. After uploading the program in NodeMCU, open serial monitor with 115200 baud rate and get the IP address of NodeMCU. Open it in the web browser, and you will see all the data with time and date when motion is recorded.

IoT Motion Detector Working

A working video and complete code for this Simple IoT Motion Detector is given below.

Code

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "Wi-Fi Name"; 
const char* password = "Password";
const char *host = "maker.ifttt.com";
const char *privateKey = "Private Key";
ESP8266WebServer server(80); //Server on port 80
void send_event(const char *event);
int buzzer = 14;   //Buzzer  alarm  connected to GPIO-14 or D5 of nodemcu
int PIRsensor = 5; //PIR sensor output connected to GPIO-5 or D1 of nodemcu 
String Message;
const char MAIN_page[] PROGMEM = R"=====(
<!doctype html>
<html>
<head>
  <title>Data Logger</title>
  <h1 style="text-align:center; color:red;">Iot Design Pro</h1>
  <h3 style="text-align:center;">Motion Detector</h3>
  <style>
  canvas{
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
  }
  /* Data Table Styling*/ 
  #dataTable {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    border-collapse: collapse;
    width: 100%;
    text-align: center;
  }
  #dataTable td, #dataTable th {
    border: 1px solid #ddd;
    padding: 8px;
  }
  #dataTable tr:nth-child(even){background-color: #f2f2f2;}
  #dataTable tr:hover {background-color: #ddd;}
  #dataTable th {
    padding-top: 12px;
    padding-bottom: 12px;
    text-align: center;
    background-color: #050505;
    color: white;
  }
  </style>
</head>
<body>   
<div>
  <table id="dataTable">
    <tr><th>Time</th><th>Activity</th></tr>
  </table>
</div>
<br>
<br>  
<script>
var Avalues = [];
//var timeStamp = [];
var dateStamp = [];
setInterval(function() {
  // Call a function repetatively with 5 Second interval
  getData();
}, 3000); //5000mSeconds update rate
function getData() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     //Push the data in array
 // var time = new Date().toLocaleTimeString();
  var date = new Date();
  var txt = this.responseText;
  var obj = JSON.parse(txt); 
      Avalues.push(obj.Activity);
     // timeStamp.push(time);
      dateStamp.push(date);
  //Update Data Table
    var table = document.getElementById("dataTable");
    var row = table.insertRow(1); //Add after headings
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = date;
    //cell2.innerHTML = time;
    cell2.innerHTML = obj.Activity;
    }
  };
  xhttp.open("GET", "readData", true); //Handle readData server on ESP8266
  xhttp.send();
}    
</script>
</body>
</html>

)====="; 
void handleRoot() {
 String s = MAIN_page; //Read HTML contents
 server.send(200, "text/html", s); //Send web page
}
void readData() {
  int state = digitalRead(PIRsensor); //Continuously check the state of PIR sensor
  delay(500);                         //Check state of PIR after every half second
  Serial.print(state);
    if(state == HIGH){ 
    digitalWrite (buzzer, HIGH);    //If intrusion detected ring the buzzer
    delay(1000);
    digitalWrite (buzzer, LOW);
    Message = "Motion Deteced";
    String data = "{\"Activity\":\""+ String(Message) +"\"}";
    server.send(200, "text/plane", data); //Send ADC value, temperature and humidity JSON to client ajax request
    send_event("motion_event");               
    Serial.println("Motion detected!");
    }
}
void setup() {
 Serial.begin(9600);
 Serial.print("Connecting to Wifi Network");
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("Successfully connected to WiFi.");
 Serial.println("IP address is : ");
 Serial.println(WiFi.localIP());
 server.on("/", handleRoot);      //Which routine to handle at root location. This is display page
 server.on("/readData", readData); //This page is called by java Script AJAX
 server.begin();                  //Start server
 Serial.println("HTTP server started");
 pinMode(PIRsensor, INPUT); // PIR sensor as input  
 pinMode(buzzer, OUTPUT);   // Buzzer alaram as output
 digitalWrite (buzzer, LOW);// Initially buzzer off
}
void loop(){
  server.handleClient();          //Handle client requests 
}
void send_event(const char *event)
{
  Serial.print("Connecting to "); 
  Serial.println(host); 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("Connection failed");
    return;
  } 
  // We now create a URI for the request
  String url = "/trigger/";
  url += event;
  url += "/with/key/";
  url += privateKey; 
  Serial.print("Requesting URL: ");
  Serial.println(url);  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  while(client.connected())
  {
    if(client.available())
    {
      String line = client.readStringUntil('\r');
      Serial.print(line);
    } else {
      // No data yet, wait a bit
      delay(50);
    };
  }  
  Serial.println();
  Serial.println("closing connection");
  client.stop();
}

Video

25 Comments

I and also my friends came reading the good information and facts from your site and so all of the sudden I had an awful feeling I had not expressed respect to you for those techniques. Those ladies ended up as a result glad to read through them and have certainly been loving them. Thanks for turning out to be very helpful and for figuring out this kind of cool themes most people are really wanting to know about. My honest regret for not expressing appreciation to you sooner.

I would like to point out my affection for your kind-heartedness in support of persons who really need help on in this concept. Your real dedication to getting the solution all-around was remarkably powerful and has regularly helped people just like me to attain their targets. Your entire helpful guide signifies a lot to me and extremely more to my office colleagues. Many thanks; from each one of us.

I am writing to let you understand what a nice experience my cousin's girl obtained using the blog. She learned such a lot of details, which included what it's like to possess a very effective helping spirit to get men and women with no trouble know just exactly certain grueling subject areas. You really exceeded our own expectations. Many thanks for distributing those important, trustworthy, edifying and also fun guidance on this topic to Kate.

Today, while I was at work, my cousin stole my iphone and tested to see if it can survive a 40 foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is completely off topic but I had to share it with someone!|

I precisely desired to appreciate you yet again. I am not sure the things I would have implemented without the actual concepts shared by you directly on such area. It actually was the troublesome dilemma in my circumstances, nevertheless viewing a new skilled mode you managed the issue made me to leap over joy. I am grateful for your service and then believe you recognize what a great job you have been carrying out training men and women all through your website. I know that you have never encountered any of us.

I happen to be commenting to make you be aware of what a extraordinary experience my friend's princess had visiting your blog. She discovered lots of details, not to mention what it's like to possess an amazing helping mood to get other people quite simply learn about certain extremely tough topics. You really did more than people's expected results. Many thanks for producing the important, safe, explanatory as well as fun thoughts on this topic to Sandra.

Thanks for all of the hard work on this site. Kim delights in conducting investigations and it is easy to see why. My spouse and i learn all regarding the lively mode you produce functional suggestions via this web blog and as well as recommend participation from others on this content then our girl is in fact becoming educated a lot. Enjoy the rest of the year. You're carrying out a good job.

I followed every single step sir but while checking it in web server taking the ip address from serial monitor am not getting the result that u got

I simply wanted to compose a simple message in order to appreciate you for the nice suggestions you are posting here. My considerable internet investigation has at the end of the day been compensated with good quality facts and strategies to talk about with my family members. I 'd suppose that many of us visitors actually are definitely endowed to exist in a perfect place with many brilliant individuals with valuable tricks. I feel rather lucky to have used the webpages and look forward to many more excellent minutes reading here. Thanks a lot again for a lot of things.

I definitely wanted to post a quick comment so as to say thanks to you for those wonderful ideas you are posting at this site. My extended internet look up has at the end been compensated with reputable concept to write about with my visitors. I would assert that many of us readers actually are really fortunate to exist in a notable community with so many perfect individuals with useful suggestions. I feel pretty grateful to have used your entire web site and look forward to so many more entertaining moments reading here. Thank you once again for a lot of things.

I in addition to my buddies were found to be reading through the great things found on your site and so suddenly came up with an awful feeling I never expressed respect to the web blog owner for those tips. Those boys appeared to be certainly glad to see all of them and have in reality been taking pleasure in them. Appreciate your truly being very accommodating and also for figuring out such very good guides millions of individuals are really desirous to understand about. My personal honest regret for not expressing appreciation to you sooner.

My spouse and i ended up being joyful when Emmanuel could conclude his preliminary research because of the precious recommendations he grabbed out of the web site. It is now and again perplexing to just choose to be making a gift of thoughts which usually some other people could have been trying to sell. Therefore we recognize we now have the blog owner to be grateful to for this. The most important explanations you made, the straightforward blog menu, the relationships you can make it possible to engender - it's mostly wonderful, and it's helping our son in addition to our family recognize that that content is pleasurable, and that is quite mandatory. Thank you for the whole thing!

I would like to show appreciation to you just for bailing me out of this type of trouble. As a result of looking through the world wide web and seeing suggestions that were not pleasant, I assumed my life was well over. Existing without the approaches to the problems you've solved all through your good guideline is a serious case, and the ones that would have in a wrong way affected my entire career if I had not discovered your blog post. Your understanding and kindness in dealing with the whole lot was important. I am not sure what I would've done if I hadn't come across such a subject like this. I am able to now relish my future. Thank you very much for this skilled and sensible guide. I won't be reluctant to recommend your site to any individual who needs to have direction on this area.

Needed to create you that bit of observation to be able to say thanks again on your pleasing solutions you have discussed at this time. This has been simply strangely open-handed with people like you giving easily all that many of us would have marketed as an electronic book in order to make some dough on their own, chiefly since you might well have done it if you decided. Those techniques also worked to become a fantastic way to understand that the rest have the same desire the same as my very own to learn much more pertaining to this matter. I'm certain there are several more fun opportunities ahead for individuals that go through your site.

My wife and i have been absolutely delighted Michael could deal with his web research using the precious recommendations he had while using the web pages. It is now and again perplexing just to find yourself offering secrets which usually other people might have been trying to sell. And we all remember we've got the blog owner to give thanks to because of that. The specific illustrations you've made, the simple site menu, the relationships you give support to instill - it is mostly amazing, and it's really making our son in addition to us believe that the subject is awesome, and that's extremely indispensable. Many thanks for the whole thing!

I am glad for commenting to make you understand what a incredible experience my girl went through using your blog. She noticed such a lot of issues, which included how it is like to have an incredible helping style to have many others easily know precisely certain hard to do subject matter. You undoubtedly surpassed our own expectations. I appreciate you for rendering these interesting, trusted, informative and as well as fun tips on this topic to Lizeth.

Thanks so much for providing individuals with a very spectacular chance to read articles and blog posts from this web site. It is usually very pleasing plus stuffed with a lot of fun for me personally and my office acquaintances to search your blog at the least 3 times a week to read through the newest stuff you will have. Of course, I'm so actually amazed with all the dazzling knowledge you serve. Some two facts in this post are rather the best we've had.

I want to get across my passion for your kindness giving support to those people who really need help on this one idea. Your special dedication to passing the message all through ended up being extraordinarily practical and has without exception helped people much like me to get to their goals. This invaluable suggestions entails so much to me and substantially more to my office workers. With thanks; from all of us.

I as well as my guys appeared to be going through the good techniques on your site while quickly developed an awful feeling I had not expressed respect to the blog owner for those strategies. Those boys appeared to be so warmed to see them and have in effect actually been tapping into these things. Thanks for turning out to be indeed kind and for deciding on some high-quality areas most people are really eager to understand about. My personal sincere regret for not expressing appreciation to earlier.

My husband and i have been absolutely peaceful Edward could round up his investigations from your ideas he got when using the site. It's not at all simplistic to just possibly be freely giving information and facts men and women have been selling. We fully grasp we now have you to appreciate for that. The explanations you have made, the simple website menu, the relationships you can make it possible to foster - it's everything terrific, and it's really aiding our son in addition to the family do think this topic is interesting, which is certainly incredibly mandatory. Thank you for all!

I would like to voice my passion for your generosity giving support to folks who absolutely need guidance on that idea. Your real dedication to getting the solution all around came to be certainly interesting and has constantly made regular people like me to attain their ambitions. This useful guideline implies so much a person like me and further more to my office workers. Thanks a ton; from each one of us.

My spouse and i have been so excited Edward managed to round up his analysis through your precious recommendations he made from your own blog. It's not at all simplistic to just always be giving freely helpful hints which usually the others might have been selling. And now we see we have the website owner to thank because of that. The most important explanations you made, the simple web site menu, the relationships you will help instill - it's got mostly wonderful, and it's really facilitating our son and us consider that this topic is fun, which is truly essential. Thank you for everything!

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.