Shreyas M. Karkada
Published © GPL3+

Security System with Telegram Alert

The System sends a message through Telegram whenever a Motion is detected when the system is Armed.

IntermediateFull instructions provided2 hours3,074
Security System with Telegram Alert

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Breadboard (generic)
Breadboard (generic)
×1
PIR Sensor, 7 m
PIR Sensor, 7 m
×1
4x3 Matrix Membrane Keypad
×1
Buzzer
Buzzer
×1
LED (generic)
LED (generic)
×3
Resistor 220 ohm
Resistor 220 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Bolt IoT Android App
Bolt IoT Android App
Bolt Cloud
Bolt IoT Bolt Cloud
Arduino IDE
Arduino IDE
Linux based System

Story

Read more

Schematics

Security System

Code

Security_System_with_Telegram_Alert.ino

Arduino
#include <Keypad.h>                // library for keyboard
#include <Password.h>              // library for password

Password password = Password( "1234" );  // password

const byte rows = 4;                     // four rows       
const byte cols = 3;                     // three columns
char keys[rows][cols] = {                // keys on keypad

{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}

};

byte rowPins[rows] = {9,8,7,6};
byte colPins[cols] = {5,4,3};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);



#define sensorz A3      // pin for PIR sensor data
//#define contact 0     // pin for on/off alarm
#define alrm 12      // pin for siren, buzzer
#define redLed A2        //  pin for red led
#define greenLed A0        // pin for green led
#define yellowLed A1    // pin for blue led
#define bolt 2    //Intruder Alert! Siren Activated
int contact = 10;       //used to immediately on/off alarm
int val;
int ledBlink;

int sensorzData;
unsigned long ceas, timpmemorat;

int intarziereactivare = 20;    // To delay for standby to armed
int intarzieredezactivare = 10; // To delay for triggered to alarm activated
int timpurlat = 10;             // Time of alarm is on

// This is the variable for states "0"
char caz = 0;

int sistem = 0;      // system is 0 for off and 1 for on


/*
States for 

  0. - off
  1. - stand-by
  2. - waitting
  3. - countdown
  4. - alarm
  
*/

void setup()
  {
  keypad.addEventListener(keypadEvent); // an object is created for tracking keystrokes
  
  Serial.begin(9600);  //Used for troubleshooting
  pinMode(alrm, OUTPUT);
  pinMode(sensorz, INPUT);
  pinMode(contact, INPUT);
  pinMode(redLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(bolt, OUTPUT);
  digitalRead(contact);
  //Serial.println("System startup"); //Used for troubleshooting
  //Serial.println("Alarm button status:"); //used for troubleshooting
  //Serial.println(digitalRead(contact)); //used for troubleshooting
  }

void loop()
  
  {
     
  ceas = millis();    // read the internal clock
  val = digitalRead(contact);
  
keypad.getKey();
 
     
    if (sistem%2 == 0)
    {
    // alarm is off
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, LOW);
    digitalWrite(yellowLed, HIGH);
    //Serial.println(contact); //Used for troubleshooting
    digitalWrite(alrm, LOW);
    digitalWrite(bolt, LOW);

    caz = 0;   
   //Serial.println("System is OFF !"); // Used for troubleshooting
    }

  else
    {
    // alarm is on
    
    if(caz == 0) 
     {
     caz = 1;
     timpmemorat = ceas;
     digitalWrite(yellowLed, HIGH);
     }

    if(caz == 1)              // system waiting
      {
      if ((ceas%1000)<500) digitalWrite(greenLed, HIGH);
      else digitalWrite(greenLed, LOW);
 keypad.getKey();
      if(ceas >= timpmemorat + intarziereactivare * 1000) {caz = 2;}
      //Serial.println("System is arming !"); // Used for troubleshooting
      }
      
    if(caz == 2)              // system is armed
      {
      digitalWrite(greenLed, HIGH); 
         
 keypad.getKey();
   
      sensorzData = digitalRead(sensorz);  
      //Serial.print("sensorzdData = "); //Used for troubleshooting
      //Serial.println(sensorzData); //Used for troubleshooting
   
  //    if(sensorzData > 600) {caz = 3; timpmemorat = ceas;}
     if(sensorzData == HIGH)
       {
       caz = 3;
       timpmemorat = ceas;
       digitalWrite(greenLed, LOW);
       }
        
      //Serial.println("System is armed !"); // Used for Troubleshooting
      }

    if(caz == 3)              // system is triggered and countdown
      {             

      if ((ceas%500)<100) digitalWrite(redLed, HIGH);
      else digitalWrite(redLed, LOW);
 keypad.getKey();
      if(ceas >= timpmemorat + intarzieredezactivare * 10) {caz = 4; timpmemorat = ceas;}
      //Serial.println("System is triggered and is countdown !"); //Used for troubleshooting
      digitalWrite(bolt, HIGH);
      }

    if(caz == 4)              // siren (buzzer) is active
      {
      //digitalWrite(alrm, HIGH);
      digitalWrite(redLed, HIGH);
      //Serial.println("Siren is active !"); //Used for troubleshooting    

// For siren

    //tone( 10, 10000, 100);  // Simple Alarm Tone
    for(double x = 0; x < 0.92; x += 0.01){  // Elegant Alarm Tone
        tone(10, sinh(x+8.294), 10);
        delay(1);
        }   
    
      
 keypad.getKey();      
      if(ceas >= timpmemorat + timpurlat * 1000) {caz = 2; digitalWrite(bolt, LOW); digitalWrite(alrm, LOW); digitalWrite(redLed, LOW);}
      }
    }
  }
  
  //take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
  //Serial.print("Pressed: ");
  //Serial.println(eKey);
  switch (eKey){
    case '*': checkPassword(); break;
    case '#': password.reset(); break;
    default: password.append(eKey);
     }
  }
}

  
  void checkPassword(){
  if (password.evaluate()){
    //Serial.println("Success"); //Used for troubleshooting
  sistem++;
  password.reset();
    //Serial.println("Disarmed");//Add code to run if it works
  }else{
    //Serial.println("Wrong"); //Used for troubleshooting
    //add code to run if it did not work
    ledBlink = 0;
    while (ledBlink <= 5){
    digitalWrite(redLed, HIGH);
    delay(100);
    digitalWrite(redLed, LOW);
    delay(100);
    ledBlink++;
    }
    password.reset();
  }
}

Security System

Python
import requests                 # for making HTTP requests
import json                     # library for handling JSON data
import time                     # module for sleep operation

from boltiot import Bolt        # importing Bolt from boltiot module
import conf                     # config file

mybolt = Bolt(conf.bolt_api_key, conf.device_id)

def value_of_pin(pin):
    """Returns the sensor value. Returns -999 if request fails"""
    try:
        response = mybolt.digitalRead(pin)
        data = json.loads(response)
        if data["success"] != 1:
            print("Request not successfull")
            print("This is the response->", data)
            return -999
        sensor_value = int(data["value"])
        return sensor_value
    except Exception as e:
        print("Something went wrong when returning the sensor value")
        print(e)
        return -999


def send_telegram_message(message):
    """Sends message via Telegram"""
    url = "https://api.telegram.org/" + conf.telegram_bot_id + "/sendMessage"
    data = {
        "chat_id": conf.telegram_chat_id,
        "text": message
    }
    try:
        response = requests.request(
            "POST",
            url,
            params=data
        )
        print("This is the Telegram URL")
        print(url)
        print("This is the Telegram response")
        print(response.text)
        telegram_data = json.loads(response.text)
        return telegram_data["ok"]
    except Exception as e:
        print("An error occurred in sending the alert message via Telegram")
        print(e)
        return False


while True:
    # Step 1
    sensor_value = value_of_pin("0")    
  
    # Step 2
    if sensor_value == -999:
        print("Request was unsuccessfull. Skipping.")
        time.sleep(10)
        continue
    
    # Step 3
    if sensor_value == 1:
        print("Sensor has been Triggered")
        message = "Intruder Alert! Siren Activated"
        telegram_status = send_telegram_message(message)
        print("This is the Telegram status:", telegram_status)

    # Step 4
    time.sleep(10)

conf

Python
"""Configurations for security_system.py"""
bolt_api_key = "XXXX"               # This is your Bolt Cloud API Key
device_id = "BOLTXXXX"                  # This is the device ID and will be similar to BOLTXXXX where XXXX is some numbers
telegram_chat_id = "@XXXX"          # This is the channel ID of the created Telegram channel. Paste after @
telegram_bot_id = "botXXXXX"         # This is the bot ID of the created Telegram Bot. Paste after bot

Credits

Shreyas M. Karkada
1 project • 0 followers
Contact
Thanks to Bolt IoT and thehack904.

Comments

Please log in or sign up to comment.