An Article about Home Security using Raspberry Pi + Web Cam + PIR Sensor and Telegram Bot

Author: Shazin Sadakath


I saw a post in Hackaday.com which was about using Telegram API to control LEDs attached to a Raspberry Pi. Telegram is a messaging service similar to Watsapp but it allows a unique bot API to support its non human machine to machine communication. 

Because of this feature it is hacker friendly because the possibilities are only limited by imagination. I had a PIR sensor and a USB web camera lying around and thought it would be cool to put together something related to Telegram bot API using those.

In Sri Lanka CCTV cameras selling like hot cake and office and home security surveillance has become a high priority in the last couple of years. Because of this I thought it would be cool to put a Home security service for me using the aforementioned components. 

Thus came up with the following schematic.

 

And attached the USB camera to the Raspberry Pi's USB port. 

 

 

Finally a little bit of coding 

########################
# Shazin Sadakath      #
#######################

import RPi.GPIO as GPIO
import time
import telegram
from subprocess import call

sensor = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

chat_id = -1
token = "token"

bot = telegram.Bot(token=token)

while True:
 time.sleep(5)
 previous_state = current_state
 current_state = GPIO.input(sensor)
 #print("Current State : %s" % current_state)
 if current_state != previous_state:
  new_state = "HIGH" if current_state else "LOW"
  print("GPIO pin %s is %s" % (sensor, new_state))
  if new_state == "HIGH":
   print("Detected Activity")
   call(["fswebcam", "image.jpg"])
   bot.sendMessage(chat_id=chat_id, text="Some activity detected for the past 5 seconds!")
   bot.sendPhoto(chat_id=chat_id, photo=open("/home/pi/image.jpg"))

After installing all important Telegram messenger and creating a bot by going through the Documentation of Telegram, finally I managed to get the whole thing working together. You can see the application in action in the following video.

 

 



References

  1. Telegram on Raspberry Pi
  2. Raspberry Pi + PIR
  3. Telegram Bot Python Library
  4. Telegram Bots Introduction
  5. Standard Web Cam usage in Raspberry Pi


Tags: RaspberryPi PIR Telegram
Views: 1324
Register for more exciting articles

Comments

Please login or register to post a comment.


There are currently no comments.