Rounik Biswas
Published © GPL3+

Security System Using PIR Sensor

A security system using PIR Sensor and Bot Module which detects motion and send mail and telegram message

IntermediateFull instructions provided679
Security System Using PIR Sensor

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Schematics

Circuit_Diagram

Schematics of Circuit Diagram needed for this project

Circuit_diagram

Circuit diagram for the project

Code

alert.py

Python
This is the python code for alert.py
import requests                 # for making HTTP requests
import json                     # library for handling JSON data
import time                     # module for sleep operation
import smtplib
from boltiot import Bolt        # importing Bolt from boltiot module
import conf                     # config file

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

def get_sensor_value_from_pin(pin):
    try:
        response = mybolt.digitalRead(pin)
        data = json.loads(response)
        sensor_value = int(data["value"])
        if sensor_value == 1:
            return sensor_value
        return 0
    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


def send_mail(email, password, message):
    print("MESSAGE:  " + message)
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(email, password)
    print("Login Successful")
    server.sendmail(email, "abc@xyz.com", message) 		# replace abc@xyz.com with                                     the mail id where you want to send the mail.
    print("SENT")
    server.quit()
    
   
while True:
    
    sensor_value = get_sensor_value_from_pin("0")    
    print("The current sensor value is:", sensor_value)
    
    if sensor_value == -999:
        print("Something Went Wrong...")
        time.sleep(10)
        continue
    
        
    if sensor_value == 1:
	print("Sensor value has exceeded threshold")
	message = "Alert! Sensor Detected Someone "
	telegram_status = send_telegram_message(message)
	print("This is the Telegram status:", telegram_status)
		    
	email= "xxxxxxxxx"  		# Enter your mail id
	password= "xxxxxxxxx" 		# Enter password of your mail id
	send_mail(email, password, message)
        -
    time.sleep(10)

Credits

Rounik Biswas

Rounik Biswas

1 project • 1 follower

Comments