KORNER HOME SECURITY SYSTEM

DIY Korner home security system What is Korner security system?

This product is meant for home security. It comes with a korner tag (korner stick) and several korner tags. The one to the right is the korner tag and the one to the left are the tags. The way this works is the korner stick module is attached to a router with the rj45 ethernet connector using which it accesses the internet. The korner tags are then attached to the doors, windows, closets etc which you want to protect and want an alert when they are disturbed. The korner stick module sounds a loud alarm when any of the tags are disturbed and generates an alert message to the mobile app provided. Let’s get MAKING! Hardware components required For Korner Stick you’ll require, • ESP8266 12e board Circuit diagram Korner stick: Korner tag: Concept used for making this project the concept I used for making korner stick is, first of all this stick need to provide an access point (in simple language, creating an hotspot) to all the tags to get connected to it and in parallel the stick needs to get connected with our router so that the stick can get internet access to send notifications to laptop or mobile phones via Adafruit MQTT Server. So I programmed this ESP board into Mode 3(Both Station and Access Point). After the tags get connected to the stick, the stick will be waiting for particular links to get requested from the tags. If that particular link is recognized by the stick, it will first send signal to MQTT broker and start buzzing the buzzer 10 times. This is the task done by the ESP board acting as a stick. And for the korner tags, I have programmed the ESP board to work in Station mode as it just needs to connect to our korner stick. After that, the values of the accelerometer get calibrated according to the surface it is attached on. So, whenever there is any movement of the door, the values of accelerometer gets fluctuated and the ESP board acting as a tag will request one link and that link depends upon the IP address assign to the korner tag. I have explained this concept in very much detail in the project video so watch out to know more. So this is how I made my own Korner Stick and Korner tag. In the codes below change the SSID and password of the NodeMCU to your router ssid and password also put in the correct Adafruit credentials. Code for Korner stick

This is the arduino code to be uploaded onto the Korner stick </p>

#include 
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include 
#include 
#define buzzer D8 // Buzzer is attached on D8 pin
// Details of your router.
#define WLAN_SSID "SSID"
#define WLAN_PASS "PASSWORD"
// Details for making ESP act as a router.
const char *ssid = "ESPap";
const char *password = "thereisnospoon";
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "USERNAME"
#define AIO_KEY "AIO_KEY"
ESP8266WebServer server(80);
WiFiClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login
details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME,
AIO_KEY);
/****************************** Feeds
***************************************/
// Setup feeds each for different korner tags.
// Notice MQTT paths for AIO follow the form: /feeds/
Adafruit_MQTT_Publish sensor_2 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/sens2");
Adafruit_MQTT_Publish sensor_3 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/sens3");
Adafruit_MQTT_Publish sensor_4 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/sens4");
Adafruit_MQTT_Publish sensor_5 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/sens5");
Adafruit_MQTT_Publish sensor_6 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/sens6");
Adafruit_MQTT_Publish sensor_7 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/sens7");
Adafruit_MQTT_Publish sensor_8 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/sens8");
Adafruit_MQTT_Publish sensor_9 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/sens9");
Adafruit_MQTT_Publish sensor_10 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/sens10");
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void handleRoot()
{
 server.send(200, "text/html", "

You are connected

"); } uint32_t x = 0; void sensor2() { Serial.println("Link Requested from one of the device 2"); if (!sensor_2.publish(1)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } for (int i = 0; i < 10; i++) { digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(50); digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(500); } server.send(200, "text/html", "

Your Request Serviced

"); if (!sensor_2.publish(0)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } } void sensor3() { Serial.println("Link Requested from one of the device 3"); if (! sensor_3.publish(1)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } for (int i = 0; i < 10; i++) { digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(50); digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(500); } server.send(200, "text/html", "

Your Request Serviced

"); if (!sensor_3.publish(0)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } } void sensor4() { Serial.println("Link Requested from one of the device 4"); if (! sensor_4.publish(1)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } for (int i = 0; i < 10; i++) { digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(50); digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(500); } server.send(200, "text/html", "

Your Request Serviced

"); if (!sensor_4.publish(0)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } } void sensor5() { Serial.println("Link Requested from one of the device 5"); if (! sensor_5.publish(1)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } for (int i = 0; i < 10; i++) { digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(50); digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(500); } server.send(200, "text/html", "

Your Request Serviced

"); if (!sensor_5.publish(0)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } } void sensor6() { Serial.println("Link Requested from one of the device 6"); if (! sensor_6.publish(1)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } for (int i = 0; i < 10; i++) { digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(50); digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(500); } server.send(200, "text/html", "

Your Request Serviced

"); if (!sensor_6.publish(0)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } } void sensor7() { Serial.println("Link Requested from one of the device 7"); if (! sensor_7.publish(1)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } for (int i = 0; i < 10; i++) { digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(50); digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(500); } server.send(200, "text/html", "

Your Request Serviced

"); if (!sensor_7.publish(0)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } } void sensor8() { Serial.println("Link Requested from one of the device 8"); if (! sensor_8.publish(1)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } for (int i = 0; i < 10; i++) { digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(50); digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(500); } server.send(200, "text/html", "

Your Request Serviced

"); if (!sensor_8.publish(0)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } } void sensor9() { Serial.println("Link Requested from one of the device 9"); if (! sensor_9.publish(1)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } for (int i = 0; i < 10; i++) { digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(50); digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(500); } server.send(200, "text/html", "

Your Request Serviced

"); if (!sensor_9.publish(0)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } } void sensor10() { Serial.println("Link Requested from one of the device 10"); if (! sensor_10.publish(1)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } for (int i = 0; i < 10; i++) { digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(50); digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(500); } server.send(200, "text/html", "

Your Request Serviced

"); if (!sensor_10.publish(0)) { Serial.println(F("Published")); } else { Serial.println(F("OK!")); } } void setup() { WiFi.mode(WIFI_AP_STA);// Selecting mode 3 delay(1000); Serial.begin(115200); Serial.println(); Serial.print("Configuring access point..."); /* You can remove the password parameter if you want the AP to be open. */ WiFi.softAP(ssid, password); pinMode(buzzer, OUTPUT); IPAddress myIP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(myIP); server.on("/", handleRoot); server.on("/sens2", sensor2); //192.168.4.1/sens2 server.on("/sens3", sensor3); server.on("/sens4", sensor4); server.on("/sens5", sensor5); server.on("/sens6", sensor6); server.on("/sens7", sensor7); server.on("/sens8", sensor8); server.on("/sens9", sensor9); server.on("/sens10", sensor10); server.begin(); Serial.println("HTTP server started"); Serial.println(F("Adafruit MQTT demo")); // Connect to WiFi access point. Serial.println(); Serial.println(); 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()); } void loop() { MQTT_connect(); //Establishing MQTT connection Adafruit_MQTT_Subscribe *subscription; Serial.println("Waiting for the link"); digitalWrite(buzzer, LOW); // by default the buzzer will be low server.handleClient(); // waiting for link to be requested from tags } void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); } This is the arduino code to uploaded into the Korner tags: #include #include #define sensitivity 20 // 10 - highest sensitivity 100 - lowest sensitivity #define sens A0 //Z-axis of accelerometer is attached to A0 pin. // Details of router created by our Korner stick const char* ssid = "ESPap"; const char* password = "thereisnospoon"; int num = 0; long int total; int avg; bool triggered = 1; void setup() { Serial.begin(115200); Serial.println(); Serial.println(); Serial.println(); for (uint8_t t = 4; t > 0; t--) { Serial.printf("[SETUP] WAIT %d...\n", t); Serial.flush(); delay(1000); } WiFi.mode(WIFI_STA);// turning the mode to station mode. WiFi.disconnect(true);// disconnect if already connected WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); pinMode(sens, INPUT); delay(15000); } //------------ Getting the local IP address String ip = WiFi.localIP().toString(); Serial.println(ip); Serial.println("Connected"); //------------- Filtering the 4th part of our IP address // 192.168.4.1 int count = 0; while (ip.charAt(count) != '.') count++; count++; while (ip.charAt(count) != '.') count++; count++; while (ip.charAt(count) != '.') count++; count++; //-------------- Converting the 4th part of IP address to Int String number = ip.substring(count); num = number.toInt(); Serial.println(number); } void loop() { // wait for WiFi connection if ((WiFi.status() == WL_CONNECTED)) { HTTPClient http; //---------------- Calbrating the surface to 0. if(triggered) { for(int i =0 ; i<100; i++) { int temp = analogRead(sens); total = total + temp; delay(20); } avg = total / 100; triggered = 0; total = 0; Serial.print("AVERAGE ");Serial.println(avg); } Serial.print("ANALOG - AVG = ");Serial.println(analogRead(sens) - avg); Serial.print("AVG - ANALOG = ");Serial.println(avg - analogRead(sens)); if (analogRead(sens) - avg > sensitivity || avg - analogRead(sens) > sensitivity)// if the module is deviated from its calibrated value than it will request a link based on its IP address { if (num == 1) { Serial.print("[HTTP] begin...1\n"); // requesting required link http.begin("http://192.168.4.1/sens1"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else if (num == 2) { Serial.print("[HTTP] begin...2\n"); // requesting required link http.begin("http://192.168.4.1/sens2"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else if (num == 3) { Serial.print("[HTTP] begin...3\n"); // requesting required link http.begin("http://192.168.4.1/sens3"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else if (num == 4) { Serial.print("[HTTP] begin...4\n"); // requesting required link http.begin("http://192.168.4.1/sens4"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else if (num == 5) { Serial.print("[HTTP] begin...5\n"); // requesting required link http.begin("http://192.168.4.1/sens5"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else if (num == 6) { Serial.print("[HTTP] begin...6\n"); // requesting required link http.begin("http://192.168.4.1/sens6"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else if (num == 7) { Serial.print("[HTTP] begin...7\n"); // requesting required link http.begin("http://192.168.4.1/sens7"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else if (num == 8) { Serial.print("[HTTP] begin...8\n"); // requesting required link http.begin("http://192.168.4.1/sens8"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else if (num == 9) { Serial.print("[HTTP] begin...9\n"); // requesting required link http.begin("http://192.168.4.1/sens9"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else if (num == 10) { Serial.print("[HTTP] begin...10\n"); // requesting required link http.begin("http://192.168.4.1/sens10"); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); http.end(); triggered = 1; } else { Serial.println("NOTHING TO DO"); } } } }
Downloads