Maker 101
Published © GPL3+

How to Make a $10 WiFi Security System at Home

If the PIR motion sensor detects any motion, it sends a notification to your mobile phone. No fees and works anywhere!

IntermediateFull instructions provided2,706
How to Make a $10 WiFi Security System at Home

Things used in this project

Hardware components

ESP8266 ESP01 WiFi Module
×1
FTDI USB To TTL Adapter
×1
Mini PIR Motion Sensor
×1
LD1117 Voltage Regulator
×1
1000uF Electrolytic Capacitor
×1
100nF Ceramic Capacitor
×1
On-Off Switch Button
×1
LED
×1
Mini Breadboard
×1
Jumper Wires
×1
Digital Multimeter
×1
Alligator USB Connector
×1

Software apps and online services

Maker service
IFTTT Maker service
https://ifttt.com
NodeMCU firmware
NodeMCU firmware
https://github.com/nodemcu/nodemcu-flasher
Arduino IDE
Arduino IDE
https://www.arduino.cc/en/Main/Software

Story

Read more

Code

pir-security-notification-esp-code

Arduino
Be sure to include the necessary libraries and enter the IFTTT keys.
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

#define SENSORPIN 2
#define LEDPIN 0

// Set web server port number to 80
WiFiServer server1(80);

// Variable to store the HTTP request
String header;

// Replace with your unique IFTTT URL resource
const char* resource = "/trigger/YOUR_EVENT_NAME/with/key/YOUR_KEY";

// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

volatile bool sensorActivatedFlag = false;

void setup() {
  Serial.begin(115200);
  pinMode(SENSORPIN, INPUT);
  pinMode(LEDPIN, OUTPUT);

  attachInterrupt(SENSORPIN, sensorActivated, RISING);
  
  // WiFiManager
  // Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  // fetches ssid and pass from eeprom and tries to connect
  // if it does not connect it starts an access point with the specified name
  // here  "AutoConnectAP"
  // and goes into a blocking loop awaiting configuration
  wifiManager.autoConnect();
  // or use this for auto generated name ESP + ChipID
  //wifiManager.autoConnect();

  // if you get here you have connected to the WiFi
  Serial.println("Connected.");
  
  server1.begin();
  initWifi();
}

// Establish a Wi-Fi connection with your router
void initWifi() {
  WiFiClient client = server1.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // Web Page Heading
            client.println("<body><h1>ESP8266 Web Server</h1>");
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

void sensorActivated() {
  Serial.println("sensorActivated");
  int pirSensor = digitalRead(SENSORPIN);
  if(pirSensor == HIGH)
  {
    sensorActivatedFlag = true;
  }
  return;
}

// Make an HTTP request to the IFTTT web service
void makeIFTTTRequest() {
  Serial.print("Connecting to "); 
  Serial.print(server);

  sensorActivatedFlag = false;
  
  WiFiClient client;
  int retries = 5;
  while(!!!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if(!!!client.connected()) {
     Serial.println("Failed to connect, going back to sleep");
  }
  
  Serial.print("Request resource: "); 
  Serial.println(resource);
  client.print(String("GET ") + resource + 
                  " HTTP/1.1\r\n" +
                  "Host: " + server + "\r\n" + 
                  "Connection: close\r\n\r\n");
                  
  int timeout = 5 * 10; // 5 seconds             
  while(!!!client.available() && (timeout-- > 0)){
    delay(100);
  }
  if(!!!client.available()) {
     Serial.println("No response, going back to sleep");
  }
  while(client.available()){
    Serial.write(client.read());
  }
  
  Serial.println("\nclosing connection");
  client.stop();
}

void ledblink() {
  digitalWrite(LEDPIN, HIGH);
  delay(500);
  digitalWrite(LEDPIN, LOW);
  delay(500);
  digitalWrite(LEDPIN, HIGH);
  delay(500);
  digitalWrite(LEDPIN, LOW);
  delay(500);
  digitalWrite(LEDPIN, HIGH);
  delay(500);
  digitalWrite(LEDPIN, LOW);
}

void loop() {
  if (sensorActivatedFlag) {
    makeIFTTTRequest();
    ledblink();
  }
}

Credits

Maker 101

Maker 101

42 projects • 172 followers
Maker 101; Beginner and intermediate level Maker projects!

Comments