I am currently working with an IR sensor for paper shelves. When the sensor detects no paper, it will turn the LED light on. at the same time, it will send an alert message through Telegram for the admin to add the paper.

The problem is, I can't place the Telegram code correctly. Can someone help me?

Below is my code:

import RPi.GPIO as GPIO
import time, datetime
import telepot
#from telepot.loop import MessageLoop

now = datetime.datetime.now()

GPIO.setup(14,GPIO.OUT) #GPIO 14 -> Red 1 LED as output
GPIO.setup(15,GPIO.OUT) #GPIO 15 -> Red 2 LED as output
GPIO.setup(18,GPIO.OUT) #GPIO 18 -> Red 3 LED as output
GPIO.setup(23,GPIO.OUT) #GPIO 23 -> Red 4 LED as output
GPIO.setup(24,GPIO.OUT) #GPIO 24 -> Red 5 LED as output
GPIO.setup(5,GPIO.IN) #GPIO 5 -> IR sensor 1 as input
GPIO.setup(6,GPIO.IN) #GPIO 6 -> IR sensor 2 as input
GPIO.setup(13,GPIO.IN) #GPIO 13 -> IR sensor 3 as input
GPIO.setup(19,GPIO.IN) #GPIO 19 -> IR sensor 4 as input
GPIO.setup(26,GPIO.IN) #GPIO 26 -> IR sensor 5 as input

while 1 :

        if(GPIO.input(5)==True): #object is far away
        GPIO.output(14,True) #Red 1 led ON
        message = "Sila buat penambahan pada Borang Faks" 
        if(GPIO.input(5)==False): #object is near
        GPIO.output(14,False) # Red 1 led OFF

        if(GPIO.input(6)==True): #object is far away
        GPIO.output(15,True) #Red led 2 ON
        message = "Sila buat penambahan pada Borang Sijil Cuti Sakit"
        if(GPIO.input(6)==False): #object is near
        GPIO.output(15,False) # Red led 2 OFF

        if(GPIO.input(13)==True): #object is far away
        GPIO.output(18,True) #Red led 3 ON
        message = "Sila buat penambahan pada Senarai Kehadiran Mesyuarat"
        if(GPIO.input(13)==False): #object is near
        GPIO.output(18,False) # Red led 3 OFF

        if(GPIO.input(19)==True): #object is far away
        GPIO.output(23,True) #Red led 4 ON
        message = "Sila buat penambahan pada Borang Pemilihan WP"                    #telegram_bot.receivedMessage (chat_id, message)  
        if(GPIO.input(19)==False): #object is near
        GPIO.output(23,False) # Red led 4 OFF

        if(GPIO.input(26)==True): #object is far away
        GPIO.output(24,True) #Red 5 led ON
        message = "Sila buat penambahan pada Borang Cuti Gantian / Tanpa Rekod"
        if(GPIO.input(26)==False): #object is near
        GPIO.output(24,False) # Red 5 led OFF

#telegram_bot.receivedMessage (chat_id, message)

def action(msg):
    chat_id = msg['chat']['id']
    command = msg['text']

    print 'Send: %s' % command

#telegram_bot = telepot.Bot('832979874:AAEhuS0vJbJCruLzu8_g1P7QCPu0g-bGq-E')
#print (telegram_bot.getMe())

#MessageLoop(telegram_bot, action).run_as_thread()
print 'Up and Running....'
  • You don't appear to be indenting your if statements correctly. All of the things you want to have performed when the if statement is true needs to have at least one more leading space character than the actual if statement. Is your code properly formatted in your question? – HeatfanJohn Mar 29 at 5:23
  • Hi heatfanJohn. Okay i will correct my indenting. Other than that, is my telegram codes correct? – iznurul ahmad radzali Mar 29 at 6:32
  • 1
    Why don't you start by just getting the telegram part working? Take out all the paper-sensing and LED code, and just do the telegram part. When you have that working, combine them. – Mark Smith Mar 29 at 6:36
  • @iznurulahmadradzali Where is telepot usage on your code?! Your code remains in your indefinite loop (while 1:) and not running anything after that. – Benyamin Jafari Mar 29 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.