Motion Detector Using Nodemcu Web Alert
by Navya_1030 in Circuits > Arduino
500 Views, 3 Favorites, 0 Comments
Motion Detector Using Nodemcu Web Alert
by Navya_1030
HC-SR501 Motion Sensor usually known as the PIR sensor isused in many kinds of Security Alarm Systems and MotionDetector 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.
We used 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:
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.
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 noti cations, SMS, etc.
ARDUINO CODE:
<p>#include <ESP8266></p><p>#include<wi client.h></p><p>#include<ESP8266webserver.h></p><p>const char* ssid = "******"; const char*</p><p>password = "************";</p><p>const char *host ="maker.ifttt.com";</p><p>const char *privateKey = </p><p>"Private key";</p><p>ESP8266WebServer server(80); </p><p>//Server on port 80</p><p>void send_event(const char </p><p>*event);</p><p>int buzzer = 14; //Buzzer alarm </p><p>connected to GPIO-14 or D5 of nodemcu</p><p>int PIRsensor = 5; //PIR sensor output </p><p>connected to GPIO-5 or D1 of nodemcu </p><p>String Message;</p><p>const char MAIN_page[] PROGMEM =R"=====(</p><p><!doctype html> </p><p><html> </p><p><head> </p><p><title>Date </p><p>Logger</title></p><p><h1 </p><p>style="text-align:center; color:red;">Iot Design Pro</h1></p><p><h3 </p><p>style="text-align:center;">Motion Detector</h3> canvas{ -moz-user-select: none; -webkit-user-select: none; ms-user-select: none;</p><p><style> </p><p>canvas{</p><p>-moz-user-select: none;</p><p>-webkit-user-select: none;</p><p>-ms-user-select: none;</p><p>}</p><p>/* Data Table Styling*/</p><p>#dataTable {</p><p>font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;</p><p>border-collapse: collapse;</p><p>width: 100%;</p><p>text-align: center;</p><p>}</p><p>#dataTable td, #dataTable th {</p><p>border: 1px solid #ddd;</p><p>padding: 8px;</p><p>}</p><p>#dataTable</p><p>tr:nth-child(even){background-color: #f2f2f2;}</p><p>#dataTable tr:hover </p><p>{background-color: #ddd;}</p><p>#dataTable th { </p><p>padding-top: 12px; </p><p>padding-bottom: 12px; </p><p>text-align: center; </p><p>background-color: #050505;</p><p>color: white;</p><p>}</p><p></style> </p><p></head> </p><p><body> </p><p><div> </p><p><table id="dataTable"></p><p><tr><th>Time</th><th>Activity</th></tr> </p><p></table> </p><p></div> </p><p><br> </p><p><br> <script> </p><p>var Avalues=[]; </p><p>//var timestamp=[];</p><p>var datastamp=[];</p><p>setlnterval(function(){</p><p>// Call a function repetatively with 5 Second interval</p><p>getData();</p><p>}, 3000); //5000mSeconds update rate</p><p>function getData() {</p><p>var xhttp = new XMLHttpRequest();</p><p>xhttp.onreadystatechange =function() {</p><p>if (this.readyState == 4 &&this.status == 200) { </p><p>//Push the data in array</p><p>// var time = new Date().toLocaleTimeString();</p><p>var date = new Date(); </p><p>var txt = this.responseText;</p><p>var obj = JSON.parse(txt);</p><p>Avalues.push(obj.Activity); // timeStamp.push(time);</p><p>dateStamp.push(date);</p><p> //Update DataTable</p><p>var table =document.getElementById("dataTable");</p><p>var row = table.insertRow(1);//Add after headings</p><p>var cell1 = row.insertCell(0);</p><p>var cell2 = row.insertCell(1); </p><p>cell1.innerHTML = date; </p><p>//cell2.innerHTML = time; </p><p>cell2.innerHTML = obj.Activity; </p><p>} }; </p><p>xhttp.open("GET",</p><p>"readData", true); //Handle</p><p>readData server on ESP8266 </p><p>xhttp.send(); </p><p>} </p><p>)====="; </p><p>void handleRoot() { </p><p>String s = MAIN_page; //Read HTML </p><p>contents</p><p>server.send(200,"text/html", s); //Send web page</p><p>} </p><p>void readData() {</p><p>int state = digitalRead(PIRsensor); //Continuously</p><p>check the state of PIR sensor</p><p>delay(500); //Check state of PIR after every half second Serial.print(state);</p><p>if(state == HIGH){</p><p>digitalWrite (buzzer, HIGH); //If intrusion detected ring the buzzer</p><p>delay(1000);</p><p>digitalWrite (buzzer, LOW);</p><p>Message = "Motion Deteced";</p><p>String data = "{\"Activity\":\""+ String(Message) +"\"}";</p><p>server.send(200, "text/plane", data); //Send ADC value, temperature and humidity JSON to client ajax request send_event("motion_event");</p><p>Serial.println("Motion detected!");</p><p>}</p><p>}</p><p>void setup() { </p><p>Serial.begin(9600); </p><p>Serial.print("Connecting to </p><p>Wi Network");</p><p>Serial.println(ssid); WiFi.begin(ssid, </p><p>password);</p><p>while (WiFi.status() !=WL_CONNECTED{</p><p>delay(500); </p><p>Serial.print("."); </p><p>} </p><p>Serial.println(""); </p><p>Serial.println("Successfully </p><p>connected to WiFi.");</p><p>Serial.println("IP address is : </p><p>"); Serial.println(WiFi.localIP());</p><p>server.on("/", </p><p>handleRoot); //Which routine to handle at root location. This is displaypage</p><p>server.on("/readData", </p><p>readData); //This page is called by java Script AJAX</p><p>server.begin(); //Start server </p><p>Serial.println("HTTP server started");</p><p>pinMode(PIRsensor, INPUT); // PIR </p><p>sensor as input</p><p>pinMode(buzzer, OUTPUT); // Buzzer </p><p>alaram as output</p><p>digitalWrite (buzzer, LOW);//Initially buzzer o</p><p>} </p><p>void loop(){</p><p>server.handleClient(); //Handle client requests</p><p>}</p><p>void send_event(const char *event)</p><p>{</p><p>Serial.print("Connecting to ");</p><p>Serial.println(host);</p><p>// Use WiFiClient class to create TCP connections </p><p>WiFiClient client;</p><p>const int httpPort = 80;</p><p>if (!client.connect(host, httpPort)) { Serial.println("Connection failed");</p><p>return;</p><p>}</p><p>// We now create a URI for the request<br>String url = "/trigger/";</p><p>url += event;</p><p>url += "/with/key/";</p><p>url += privateKey;</p><p>Serial.print("Requesting URL:");</p><p>Serial.println(url);</p><p>// This will send the request to the server</p><p>client.print(String("GET") + url + " HTTP/1.1\r\n" +</p><p>"Host: " + host + </p><p>"\r\n" + "Connection: close\r\n\r\n");</p><p>while(client.connected()) </p><p>{ </p><p>if(client.available()) </p><p>{ </p><p>String line = </p><p>client.readStringUntil('\r');</p><p>Serial.print(line); </p><p>} else { </p><p>// No data yet, wait a bit </p><p>delay(50); </p><p>}; </p><p>} Serial.println(); </p><p>Serial.println("closing </p><p>connection");</p><p>client.stop(); </p><p>} </p><p>canvas{ -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; canvas{ -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none;</p><p> </p><div> <br><br></div><p> </p><p> </p> <br>
Step5:youtube Link
https://youtu.be/PHXUSC7kYn