Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@suadanwar
Last active November 30, 2021 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save suadanwar/377a8dcdd921c83e51e99ed2abde4a88 to your computer and use it in GitHub Desktop.
Save suadanwar/377a8dcdd921c83e51e99ed2abde4a88 to your computer and use it in GitHub Desktop.
This sample code is for home notification using Telegram bot and Raspberry Pi.
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)
@m7iqbal
Copy link

m7iqbal commented Nov 30, 2021

Hi, may I know how to use MQ2 instead of PIR in this project? I tried but there is too many error even I did changed the coding for MQ2. Hope you can enlighten me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment