Pawan Kumar
Published © GPL3+

Intruder Alert System

It is an intruder alert system which sends alert through telegram

BeginnerShowcase (no instructions)6 hours5,073
Intruder Alert System

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1

Software apps and online services

Arduino IDE
Arduino IDE
Bolt Cloud
Bolt IoT Bolt Cloud
repl.it
Telegram

Story

Read more

Schematics

circuit diagram of Intruder alert system

This is the schematic diagram of Intruder alert system..

Code

Python Code

Python
This is The code used to send alerts to telegram once the value is received from Arduino
import requests                
import json                     
import time                     
from boltiot import Bolt        
import conf                     
mybolt = Bolt(conf.bolt_api_key, conf.device_id)
response = mybolt.serialBegin('9600')
def get_sensor_value_from_pin(pin):
    """Returns the sensor value. Returns -999 if requefist fails"""
    try:
        response = mybolt.serialRead('10')
        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(
            "GET",
            url,
            params=data
        )
        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 = get_sensor_value_from_pin("10")    
    print("The current sensor value is:", sensor_value)
    if sensor_value == -999:
        print("Request was unsuccessfull. Skipping.")
        time.sleep(1)
        continue
    if sensor_value <= conf.threshold:
        print("Sensor value has decreased threshold")
        message = "Alert! intruder " 
        ". The current sensor value is " + str(sensor_value)
        telegram_status = send_telegram_message(message)
        print("This is the Telegram status:", telegram_status)
    time.sleep(1)

Arduino Code

Arduino
This is the code for Arduino for interfacing BOLT and the sensors
#include <Servo.h>
#include <SoftwareSerial.h>  
SoftwareSerial mySerial(0,1);
const int pingPin = 9; 
const int echoPin = 10; 
Servo servo_test;  
int angle = 0;   

void setup() {
   Serial.begin(9600); 
     mySerial.begin(9600);
  Serial.setTimeout(10);
  { 
  servo_test.attach(11);      
} 
 
}
String data;
long microsecondsToCentimeters(long microseconds);
void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);

      cm = microsecondsToCentimeters(duration);
      Serial.println(cm);
   delay(10);
   if (mySerial.available() > 0) //IOT from here
   {
    delay(10);
    String readData = "";
    Serial.print("cm");
    while (mySerial.available() > 0 ) {
      readData = readData + (float)mySerial.read();
    }
    Serial.println(readData);
{
      mySerial.print(cm);
      Serial.println(cm);}}
        String data;
 
  for(int i=0;i<=180;i += 1)    
  {                                  
    servo_test.write(i);              
    delay(10);                       
  } 
 
  
  for(int i = 180; i>=0;i -= 1)   
  {                                
    servo_test.write(i);             
    delay(10);                       
  } 
  digitalWrite(pingPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH); 
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(echoPin, HIGH); 
  return duration;
  return data;
  
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}

Credits

Pawan Kumar

Pawan Kumar

2 projects • 0 followers

Comments