Motion Detecting IoT Device
by The_Tech_Lab in Circuits > Arduino
705 Views, 2 Favorites, 0 Comments
Motion Detecting IoT Device
A simple motion detecting device that uses IFTTT to send a notifiication to user when it detects a motion.
Things Used in This Project
Hardware components
Below is the given list of all the hardware components that are going to be required for this project:
- Wemos D1 Mini x1
- HC-SR501 PIR Motion Sensor x1
- Lithium Battery x1
- TP4056 Battery Charger x1
- Breadboard x1
- Jumper wires(generic) x1
Software apps and online services
The following have been used for the completion of the project:
- Arduino IDE
IFTTT Maker service
Story
Ethan began to slow his motions as he approached the locker. Any slight movement and he would give himself away. The entire museum was empty but there was one invisible obstacle preventing him from taking the gem-the MOTION SENSOR. Motion Sensor is used both in movies and in real life. You can also make yourself one at home. Want to know? Read below for the detailed explanation.
How Wemos D1 Mini works/what it is?
The Wemos D1 Mini is an Arduino compatible microcontroller board with WiFi that packs a lot of features like low power mode, bluetooth, I2C, SPI, UART etc in a really compact package. We can program it just like an Arduino and it costs about the same as an Arduino UNO. But we get a lot more functionalities than a traditional Arduino UNO with some sacrifices like lesser pins. The reason it’s great for this project is because it's really small and has ultra low power mode where it basically draws very little or no power at all making it a great for small battery powered projects like ours.
How PIR works?
Passive Infrared Sensor or PIR in short is a sensor that detects electromagnetic radiation that is generated from the body of humans which is used as a trigger to sound off an alarm. So basically a PIR contains a particular lens which is called the “Fresnel Lens” that acts as the receiver for the PIR. In order to get a clear view of what a PIR sensor looks like, a picture of it has been given below. The white lens which is mounted on the right side is the fresnel lens which was being talked about.
How IFTTT Works
IFTTT derives its name from the programming conditional statement “if this, then that”. It is a platform that allows to connect applications, devices as well as services from various developers for the purpose of triggering one or multiple automations involving the use of the applications, devices and services. For the case of this motion sensor, IFTTT works in this manner. If this (the trigger is being activated while someone stays in front of the motion sensor), then that part (a notification gets sent to the mobile of the user) indicates that there is a person located up ahead at that very moment. So, this will allow the user to take actions accordingly.For our project, we use the webhook service of IFTTT to trigger an event, which then sends a message to our mobile phone. You can create the service by going to Sign up for a free account, use webhooks service in the “This” term and create a web request service. We name our event “motion_triggered”
In the “That” field, choose Android SMS → Send an SMS → Choose your SMS format. Note that sending SMS, your mobile phone carrier will charge money from your phone.
Now, go to the webhooks page: here and open the “Settings” tab. There should be an API key code that we will need later in the code. It’s good to keep it secret since it's a code unique to your IFTTT account and service.
Circuitry
The circuit for this project is actually really simple and you only need to assemble it on a breadboard using some jumper wires to test it. It mainly consists of a PIR sensor connected to our main processor, the Wemos D1 Mini. We also added a switch to manually turn the device on and off. The whole thing can be powered using a lithium battery as you see in the circuit diagram. However, for testing purposes, we can just power it from the USB port. We also add a TP4056 based battery charger module for easy charging of the device. It has battery protection features built in so our battery is safe from any sort of electrical damage.
Code Snippet
The code is actually pretty simple for the hardware that we are using, thanks to the amazing libraries. We commented the code with explanations of every section, so you can understand without having to Google around. Make sure to add your WiFi SSID, password, IFTTT event name and API key that we talked about above in the IFTTT step. The snippet of the code can be found below:
// Type your WiFi name (SSID) and password here: const char* ssid = "your_ssid_here"; const char* password = "your_password_here"; const int httpsPort = 443; const char* host = "maker.ifttt.com"; // Replace with your eventname and api key - open the documentation: https://ifttt.com/maker_webhooks String event_name = "motion_triggered"; String api_key = "your_api_key_here"; String url = "/trigger/" + event_name + "/with/key/" + api_key;
Usage and Applications
The applications of this motion sensor are far and wide. It can be used at any point starting from fitting it in front of the main house gate to placing it in front of the stores at the malls. The use of the motion sensor can be even used to track down if an infant is staying inside the baby cot. This can easily be done by just tweaking the triggering system of the motion sensor. It is a versatile tool which can be used any place that requires monitoring.
Closing
The process is easier than you imagined, right? Now, you can also give Ethan a hard time. Tell us what you think in the comments below. Any questions email us at: For more articles like this check The Tech Lab
Website: www.thetechlab.webite
Our Facebook: https://www.facebook.com/thetechlab
YouTube channel: https://www.youtube.com/channel/UCjVpWA4bj87jjc26RGSX13Q
Schematics
This is the schematic for the project.
Coding for the Whole Project
The amount of coding that is required to pull off this project is very small and is extremely easy to do so.
/* Motion Trigger */ #include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include <WiFiClient.h> // Type your WiFi name (SSID) and password here: const char* ssid = "your_ssid_here"; const char* password = "your_password_here"; const int httpsPort = 443; const char* host = "maker.ifttt.com"; // Replace with your eventname and api key - open the documentation: https://ifttt.com/maker_webhooks String event_name = "motion_triggered"; String api_key = "your_api_key_here"; String url = "/trigger/" + event_name + "/with/key/" + api_key; // Main setup - this part of the code runs only once on startup void setup() { // Begin serial connection, mostly for debugging Serial.begin(9600); // Initialize the on-board LED pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); // turn off the LED (HIGH is OFF for the on board LED) // Initialize WiFi and print on serial monitor (for debugging) WiFi.begin(ssid, password); Serial.println("Connecting"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); makeHTTPRequest(); // Sleep until a PIR motion is detected ESP.deepSleep(0); } void loop() { } void makeHTTPRequest() { //Send an HTTP POST request when PIR is triggered if (WiFi.status() == WL_CONNECTED) { // check if WiFi is connected digitalWrite(LED_BUILTIN, LOW); //turn on the LED Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); WiFiClientSecure client; // create a new HTTP client Serial.print("connecting to "); Serial.println(host); if (!client.connect(host, httpsPort)) { //connect to HTTP client, show error if failed to connect. Serial.println("Error: Connection failed."); return; } Serial.print("requesting URL: "); Serial.println(url); client.print(String("GET ") + url + " HTTP/1.1\r\n" + // request to HTTP client, in order to trigger IFTTT webhooks service. "Host: " + host + "\r\n" + "User-Agent: BuildFailureDetectorESP32\r\n" + "Connection: close\r\n\r\n"); Serial.println("request sent"); // get the reply from HTTP and print on serial monitor while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { Serial.println("headers received"); break; } } String line = client.readStringUntil('\n'); Serial.println("reply was:"); Serial.println("=========="); Serial.println(line); Serial.println("=========="); Serial.println("closing connection"); Serial.println(""); delay(10000); // long delay to avoid too many triggers at once digitalWrite(LED_BUILTIN, HIGH); //turn the LED off as there's no more motion } else { // If the device fails to connect to WiFi, print an error Serial.println("Error: WiFi not connected."); } }