Send Photo of Burglar To IoT Telegram Bot Using Raspberry Pi

Send Photo of Burglar To IoT Telegram Bot Using Raspberry Pi

Introduction

Continue from the previous tutorial regarding Telegram and Raspberry Pi, this time I would like to share on how we can add a camera to capture a photo and send it to the Telegram. Then, by adding a magnetic contact switch sensor, we can make the process to become auto capture. When developing this tutorial, I’m thinking of burglar alert application. Maybe you can use it for other application too. Please read through following tutorial first:

Video

This video shows how to send photo to Telegram using Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is python 3 sample program to capture a photo and send it to the Telegram. Please insert your Telegram Bot token at line 44.

from gpiozero import Buzzer, DigitalInputDevice
import telepot
import picamera
sensor = DigitalInputDevice(17, pull_up=True)
buzzer = Buzzer(26)
def handle(msg):
global telegramText
global chat_id
global receiveTelegramMessage
chat_id = msg['chat']['id']
telegramText = msg['text']
print("Message received from " + str(chat_id))
if telegramText == "/start":
bot.sendMessage(chat_id, "Welcome to Idris Bot")
else:
buzzer.beep(0.1, 0.1, 1)
receiveTelegramMessage = True
def capture():
print("Capturing photo…")
camera = picamera.PiCamera()
camera.capture('./photo.jpg')
camera.close()
print("Sending photo to " + str(chat_id))
bot.sendPhoto(chat_id, photo = open('./photo.jpg', 'rb'))
def sensorTrigger():
global statusText
global sendTelegramMessage
global cameraEnable
global sendPhoto
statusText = "Sensor is triggered!"
sendTelegramMessage = True
if cameraEnable == True:
sendPhoto = True
buzzer.beep(0.1, 0.1, 1)
bot = telepot.Bot('PUT YOUR TELEGRAM BOT TOKEN HERE')
bot.message_loop(handle)
sensor.when_deactivated = sensorTrigger
receiveTelegramMessage = False
sendTelegramMessage = False
cameraEnable = False
sendPhoto = False
statusText = ""
print("Telegram bot is ready")
buzzer.beep(0.1, 0.1, 2)
try:
while True:
if receiveTelegramMessage == True:
receiveTelegramMessage = False
statusText = ""
if telegramText == "ENABLE":
cameraEnable = True
statusText = "Camera is enabled"
elif telegramText == "PHOTO":
sendPhoto = True
statusText = "Capturing photo…"
else:
statusText = "Command is not valid"
sendTelegramMessage = True
if sendTelegramMessage == True:
sendTelegramMessage = False
bot.sendMessage(chat_id, statusText)
if cameraEnable == True and sendPhoto == True:
cameraEnable = False
sendPhoto = False
capture()
except KeyboardInterrupt:
sys.exit(0)

view raw
TelegramBotPhoto.py
hosted with ❤ by GitHub

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Send Data to Firebase Using Arduino Uno WiFi Rev2
Simple Alarm System Using Maker Nano
Program Cucumber ESP32-S2 Using CircuitPython
Face Recognition Using OpenCV on Raspberry Pi 400
Turned Your Tablet as a Display for the Raspberry Pi 400
Scroll to Top