The World's Smallest WiFi Motion Alarm
by Evan Breeze in Circuits > Sensors
2850 Views, 54 Favorites, 0 Comments
The World's Smallest WiFi Motion Alarm
This is the world's smallest motion alarm that has WiFi capabilities, built using an ESP-01. When someone moves within the range of the sensor, it detects the motion and sends a web request to Webhooks. From there, IFTTT can do anything you like - send text messages, call your number, send you a notification, email, etc.
Theft and break-ins are becoming more common these days. To counter this, motion sensors are used, but they are often quite bulky. The intruders can easily see those and avoid them. What if they can't see the sensor? They would be robbing the house thinking there are no sensors, but in reality, you will know everything that's going on! This is what this project is aimed at.
Supplies
The Circuit Diagram
This is the circuit diagram for the project. It is very simple, with only 5 connections. The pins with a black border around them are the pins of the PIR sensor, and the pins on the right side are the pins of the ESP-01. The sensor is on the opposite side of the proto-board (the side without the voltage regulator and ESP-01).
Solder the PIR Sensor
Solder the PIR sensor onto the proto-board, with the pins in C13, D14, E13, and D12. Make sure to point the projection on the PIR sensor in the direction marked in the diagram. The PIR sensor should be on the side opposite the voltage regulator and ESP-01.
Solder the LD1117 Voltage Regulator
Solder the voltage regulator on pads B12, B13, and B14. Solder the PAD pin (the big pin on the top of the regulator) to the oval pads on the edge of the proto-board, as this will help with heat dissipation. When soldering the pins, be careful not to apply heat for too long, as the regulator body gets hot very fast.
Make the LD1117 and PIR Connections
Connect a wire from the OUT pin (middle pin) of the LD1117 regulator to the 3.3V pin on the PIR sensor, and extend the same wire to pad G14. Then, strip the end of the wire (about 5mm), and put the copper part of the wire through the G14 hole.
Take a piece of wire (about 4 cm), and remove the insulation completely. Then, twist the wire, so that the wire stays more in shape and doesn't touch other pads. Solder one end of the wire to the GND pin on the voltage regulator, then guide the wire along the GND and ONTIME pins of the PIR sensor and solder the wire to the pins. Now, you should have one end of the wire connected to the LD1117 and 2 points on the wire connected to the PIR sensor GND and ONTIME pins. Extend the same wire to pad F11, and put the rest of the wire through the pad.
Then, connect a wire from the signal pin of the PIR sensor, and extend it to pad F12. Do the same process as the 3.3V wire, and put the end of the wire through pad F12.
Solder the ESP-01 Header Pins
Insert the 8 header pins in pads F11-F14 and G11-G14. Put the headers through the pins, jamming the wires in pads F11, F12 and G14 with the headers. In pads F11, F12 and G14, there will be a wire and a header pin. Then, solder the headers to the proto-board, with the holes of the header pins on the same side as the voltage regulator.
Connect the USB-c Breakout to the Sensor Unit
Finally, with wires, connect the V pin of the USB-C breakout to the INPUT pin of the LD1117, and the G pin of the USB-C breakout to the GND pin of the LD1117. And with that, the tricky part of the build is done!
Final View of the Connections
After all connections, the board should look something like this.
If you don't like soldering wires and would like to get a cleaner design, you can order a PCB for the project. The PCB files are attached below (sorry the website didn't let me attach the zip file). You can download all the files and move them to a zip folder, and use the zip folder to order the PCB.
Downloads
Configure Webhooks and IFTTT
First, go to ifttt.com and sign in. Create a new applet.
- As the trigger, select Webhooks and connect Webhooks to your google account.
- Select "Receive a web request" as the trigger
- Give the event a name (this is the IFTTT event name that you will send to the ESP-01)
- As the action, select Notifications and connect it to your google account.
- Select "Send notification from the IFTTT app" as the action item.
- Set a message to be sent as the notification. (Example : "Motion detected on {OccuredAt}")
- Create action ---> Continue ---> Finish
- Make a second applet the exact same way, but for this applet, set the IFTTT event name as "device_active" and the content of the notification as "Device Active on {OccuredAt}".
For the notification to be sent, you will have to install the IFTTT app on your device. You can select other services as the action, if you'd like.
Install Necessary Libraries and Files
Copy and paste https://arduino.esp8266.com/stable/package_esp8266com_index.json under the additional board URLs in Preferences. Install the ESP8266 board by going to the boards manager, searching "esp8266" and installing the first one that shows up. If the EEPROM library is not already installed, simply search "EEPROM" in the library manager and download the appropriate library. The ESP8266 board package will come with all the needed libraries for the ESP8266. Download the file attached below.
Downloads
Program the ESP-01
#include <ESP8266WiFi.h>
#include "AnotherIFTTTWebhook.h"
#include <ESP8266WebServer.h>
#include <EEPROM.h>
const char* WIFI_SSID = "";
const char* WIFI_PASS = "";
char* IFTTT_Event = "";
char* IFTTT_Key = "";
const char* apSSID = "ESP-01"; // Change this to the name of your access point
const char* apPassword = "12345678"; // Change this to the password of your access point
ESP8266WebServer server(80);
void setup() {
pinMode(2, INPUT);
Serial.begin(115200);
EEPROM.begin(512);
IPAddress apIP(192, 168, 1, 1); // set the IP address of the access point
IPAddress gateway(192, 168, 1, 1); // set the gateway address
IPAddress subnet(255, 255, 255, 0); // set the subnet mask
WiFi.softAPConfig(apIP, gateway, subnet);
WiFi.softAP(apSSID, apPassword);
server.on("/", handleRoot);
server.on("/status", showStatus);
server.begin();
WIFI_SSID = EEPROMreadString(0, 20);
WIFI_PASS = EEPROMreadString(21, 40);
IFTTT_Key = EEPROMreadString(41, 70);
IFTTT_Event = EEPROMreadString(71, 100);
if (WIFI_SSID != "" && WIFI_PASS != "")
{
WiFi.begin(WIFI_SSID, WIFI_PASS);
}
}
char* EEPROMreadString(int id, int endid)
{
char buffer[endid - id + 2]; // +2 to account for null terminator and 0-based indexing
int i = 0;
for (; id <= endid; id++, i++)
{
char c = EEPROM.read(id);
if (c == '\0')
{
break;
}
buffer[i] = c;
}
buffer[i] = '\0'; // Add null terminator to the end of the string
char* output = new char[strlen(buffer) + 1]; // Allocate memory for the output string
strcpy(output, buffer); // Copy the string from buffer to output
return output;
}
void EEPROMwrite(int idstart, const char* receivedString)
{
int idend = idstart + strlen(receivedString) + 1;
int index = 0;
for ( int i = idstart; i < idend; i++, index++)
{
EEPROM.write(i, receivedString[index]);
}
EEPROM.commit();
}
void showStatus()
{
String data = "SSID: " + String(WIFI_SSID) + " Pass: " + String(WIFI_PASS) + " IFTTT Key: " + String(IFTTT_Key) + " IFTTT Event: " + String(IFTTT_Event);
const char* dataPtr = data.c_str();
server.send(200, "text/html", dataPtr);
}
void handleRoot()
{
server.send(200, "text/html", "Parameters received.");
WIFI_SSID = server.arg("ssid").c_str();
WIFI_PASS = server.arg("pass").c_str();
IFTTT_Key = (char*)server.arg("iftttkey").c_str();
IFTTT_Event = (char*)server.arg("iftttevent").c_str();
EEPROMwrite(0, WIFI_SSID);
EEPROMwrite(21, WIFI_PASS);
EEPROMwrite(41, IFTTT_Key);
EEPROMwrite(71, IFTTT_Event);
delay(500);
ESP.restart();
}
send_webhook("device_active", IFTTT_Key, "Device Active", "", "");
void loop() {
server.handleClient();
int x = digitalRead(2);
if (x == 1)
{
send_webhook(IFTTT_Event, IFTTT_Key, "Motion Detected", "", "");
delay(10000);
}
}
Working of the code :
- On startup, start an access point. Read the SSID, password, IFTTT Key and IFTTT Event name from the flash memory.
- If the parameters are blank, nothing works.
- When a web request is received to http://192.168.1.1/staus, show the SSID, password, IFTTT Key and IFTTT Event name on the webpage (this is NOT safe, so delete the showstatus() function and the server.on("/status") line if necessary).
- When a web request is received to http://192.168.1.1/ssid=YourWiFiSSID&pass=YourWiFiPassword&iftttkey=YourIFTTTKey&iftttevent=YourIFTTTEventName, extract the parameters from the URL and save it to the EEPROM.
- Restart the ESP after saving.
- On startup again, read the parameters from the EEPROM.
- Send a web request to Webhooks saying the device is active.
- Go to void loop (), read the PIR output and when the output is HIGH, trigger an IFTTT Event. Wait for 10 seconds as a cooldown time.
Save this code somewhere and close the Arduino IDE. Move the file downloaded in Step 9 to the same folder as the code and reopen this sketch in the Arduino IDE. If everything works, there should be a tab next to the main sketch that says "AnotherIFTTTWebhook.h". Connect the ESP to the programmer as shown in the picture. While holding the button on the side of the programmer, connect the programmer to the laptop. Select the correct board and COM port, and upload the code.
Configuration and Test
First, connect the ESP to a USB power source (USE ONLY USB-A TO USB-C CABLE). Connect a device to the ESP access point. To get your IFTTT_Key, go to https://ifttt.com/maker_webhooks ---> documentation (sign in if not already signed in). The key will be shown there. Open a browser, and go to the url in the form : http://192.168.1.1/?ssid=YourWiFiSSID&pass=YourWiFiPassword&iftttkey=YourIFTTTKey&iftttevent=YourIFTTTEventName. (Example : http://192.168.1.1./?ssid=HomeWIFI&pass=qwertyuiop&iftttkey=jsdbvjbaifahs&iftttevent=motion_detected)
A message will be shown saying "Parameters received". Then, go to http://192.168.1.1/status . If the parameters were saved, they should be shown on the webpage. And, with that, we are done!
Now, when someone steps within the range of the sensor, you should receive a notification from IFTTT.
Final Touches
Cut the proto-board near the edge of the ESP header pins. The board should be about the size of the ESP-01 after cutting. Then, with double-sided tape or glue (superglue not recommended), stick the USB-C port to the smooth side of the ESP-01. Connect the ESP-01 to the headers, and.....voila! You have the world's smallest WiFi motion alarm!!!
The Final Test
Now, connect the sensor unit to power (again, USE ONLY USB-A to USB-C CABLE). Wait for about 30s, and then move in front of the sensor. A notification will be sent to your phone. If the project works for you, please drop an I Made It! Thank you so much for checking out this project! Good luck!