In this tutorial, I’ll share with you on how to build a home notification using Telegram and Raspberry Pi. The notification will be sent to the telegram bot when a human or animal passes by. It’s quite easy to do it. Now, let’s do it!
HARDWARE PREPARATION
This tutorial use :
You can make the hardware connection based on the diagram and table below.
Connection | |
---|---|
PIR Sensor : VCC | Raspberry Pi (Maker pHAT) : 5V |
PIR Sensor : GND | Raspberry Pi (Maker pHAT) : GND |
PIR Sensor : Signal | Raspberry Pi (Maker pHAT) : GPIO 17 |

Video Reference
Please refer to this video to setup the telgram bot.
References :
- Raspberry Pi with PIR Sensor
- Displaying Telegram Message on Dot Matrix Using Raspberry Pi
- Controlling Electrical Appliance Through Telegram Using Raspberry Pi
Sample Code
This is the sample code used for this tutorial. Try it!
import telepot | |
import RPi.GPIO as GPIO | |
import time | |
import datetime | |
from telepot.loop import MessageLoop | |
PIR = 17 | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(PIR, GPIO.IN) | |
motion = 0 | |
motionNew = 0 | |
def handle(msg): | |
global telegramText | |
global chat_id | |
chat_id = msg['chat']['id'] | |
telegramText = msg['text'] | |
print('Message received from ' + str(chat_id)) | |
if telegramText == '/start': | |
bot.sendMessage(chat_id, 'Welcome to House Notification') | |
while True: | |
main() | |
bot = telepot.Bot('Your Token Here') | |
bot.message_loop(handle) | |
def main(): | |
global chat_id | |
global motion | |
global motionNew | |
if GPIO.input(PIR) == 1: | |
print("Motion detected") | |
motion = 1 | |
if motionNew != motion: | |
motionNew = motion | |
sendNotification(motion) | |
elif GPIO.input(PIR) == 0: | |
print("No motion detected") | |
motion = 0 | |
if motionNew != motion: | |
sendNotification(motion) | |
motionNew = motion | |
def sendNotification(motion): | |
global chat_id | |
if motion == 1: | |
bot.sendMessage(chat_id, 'Someone is at your front door') | |
bot.sendMessage(chat_id, str(datetime.datetime.now())) | |
elif motion == 0: | |
bot.sendMessage(chat_id, 'Nobody is at your front door') | |
while 1: | |
time.sleep(10) |
Thank you
Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiry, please post at Cytron Technical Forum.
7 thoughts on “Home Notification using Telegram and Raspberry Pi”
hi, can u make a tutorial related to LDR sensing LEDs turning on and off with the similar concept of this home notification?
Hai, can you make some tutorial using pir and solenoid door lock ?
where is the sample code?
Hi Tega, sorry we never used AMG8833 sensor. Thanks.
how does one configure the amg8833 sensor instead of the PIR
sensor
Hi, you need to install the telepot library first. For more technical inquiry, please post at our Technical Forum. Thanks.
CODE ERROR
Traceback (most recent call last):
File “/home/pi/Documents/homenotification.py”, line 1, in
import telepot
File “/usr/lib/python3/dist-packages/thonny/backend.py”, line 305, in _custom_import
module = self._original_import(*args, **kw)
ImportError: No module named ‘telepot’