Sharvayu Chavan
Published

Home Security System with ESP8266

Know what's going on at home from anywhere in the world with this IOT based security system!

BeginnerShowcase (no instructions)4,512
Home Security System with ESP8266

Things used in this project

Hardware components

PIR Sensor, 7 m
PIR Sensor, 7 m
×1
Reed Switch, SPST-NO
Reed Switch, SPST-NO
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Breadboard (generic)
Breadboard (generic)
×1
SparkFun Breadboard Power Supply 5V/3.3V
SparkFun Breadboard Power Supply 5V/3.3V
×1
9V battery (generic)
9V battery (generic)
×1
9V Battery Clip
9V Battery Clip
×1

Software apps and online services

Arduino IDE
Arduino IDE
Adafruit Dashboard

Hand tools and fabrication machines

Premium Female/Male Extension Jumper Wires, 40 x 6" (150mm)
Premium Female/Male Extension Jumper Wires, 40 x 6" (150mm)
Magnets, Button
Magnets, Button

Story

Read more

Schematics

Home Security System Schematic

Code

Home_Security_System_Final.ino

C/C++
You can use this code if you plan on making your own security system!
//Necessary libraries for Adafruit Dashboard and ESP8266
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

//SSID and Password for Wifi
#define WLAN_SSID       "******"
#define WLAN_PASS       "*********"

//Adafruit Dashboard information(can be found under the "My Key" section of your dasboard)
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  ****                   // use 8883 for SSL
#define AIO_USERNAME    "******" //Found on dashboard
#define AIO_KEY         "******" //Found on dashboard

WiFiClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

//These two lines are necessary to send data to Adafruit Dashboard
Adafruit_MQTT_Publish PIR = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/PIR Sensor");//PIR will be used for PIR Sensor(motion)
Adafruit_MQTT_Publish Reed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Reed Switch");//Reed will be used for Reed Switch(door)

//Pin numbers for PIR Sensor(Sensor), Reed Switch(REED_PIN), and built-in led(LED_PIN)
int Sensor = 13;
const int REED_PIN = 12;
const int LED_PIN = 16; 

void MQTT_connect();

void setup() {
  // put your setup code here, to run once. I have defined all of my inputs and outputs here, and have also started my serial monitor along with WiFi Connectivity
  
  pinMode(Sensor, INPUT);
  pinMode(REED_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);

  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());

}

uint32_t x = 0;

void loop() {
  //This loop contains the main part of our program - it will be run over and over again.
  MQTT_connect();
  Serial.print(F("\nSending MS data "));
  Serial.print(x);
  Serial.print("...");

  int result = digitalRead(Sensor);// Read the state of the PIR Sensor
  int sts = digitalRead(REED_PIN); // Read the state of the reed switch
  if (sts == LOW) // If the pin reads low, the switch is closed and thus the door is closed as well.
  {
    Serial.println("Door Closed");
    digitalWrite(LED_PIN, LOW); 

  }
  else //If the pin is not low, it is high, which means the switch and door are open
  {
    Serial.println("Door Open");
    Reed.publish("Door Open"); //Publish data to Adafruit Dashboard
    digitalWrite(LED_PIN, HIGH);
  }


  Serial.println(result);
  if (result == HIGH) { //If result it high, it means motion has been detected
    Serial.println("Motion detected"); 
    PIR.publish("Motion Detected");//Publish data to Adafruit Dashboard
  }
  else { //if result is not high, no motion is being detected
    Serial.println("No Motion Detected");
    delay(1000);
  }
}

//Function for necessary MQTT Connections
void MQTT_connect() {
  int8_t ret;


  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { 
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000); 
    retries--;
    if (retries == 0) {
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
}

Credits

Sharvayu Chavan

Sharvayu Chavan

0 projects • 3 followers
I am a high school student currently attending the International Academy. I am interested in the STEM and Artificial Intelligence fields.

Comments