Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Last active February 19, 2019 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IdrisCytron/d9306158907b1513d4fa3db63bc9a5bb to your computer and use it in GitHub Desktop.
Save IdrisCytron/d9306158907b1513d4fa3db63bc9a5bb to your computer and use it in GitHub Desktop.
Controlling 2 relays using Telegram and Raspberry Pi.
from gpiozero import LED, Buzzer, Button, OutputDevice
from time import time, sleep, strftime
from datetime import datetime
import telepot
led1 = LED(17)
led2 = LED(18)
led3 = LED(27)
led4 = LED(22)
led5 = LED(25)
led6 = LED(12)
sw1 = Button(21)
sw2 = Button(16)
sw3 = Button(20)
buzzer = Buzzer(26)
RELAY_IN1 = 13
RELAY_IN2 = 19
relay1 = OutputDevice(RELAY_IN1, active_high=False, initial_value=False)
relay2 = OutputDevice(RELAY_IN2, active_high=False, initial_value=False)
def handle(msg):
global telegramText
global chat_id
global telegramMessage
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, 2)
telegramMessage = True
def sw1Pressed():
relay1.toggle()
def sw2Pressed():
relay2.toggle()
bot = telepot.Bot('PUT YOUR TELEGRAM BOT TOKEN HERE')
bot.message_loop(handle)
print("Telegram bot is ready")
sw1.when_pressed = sw1Pressed
sw2.when_pressed = sw2Pressed
telegramMessage = False
count = 0
acStatusText = ''
relay1Status = False
relay2Status = False
buzzer.beep(0.1, 0.1, 1)
try:
while True:
if telegramMessage == True:
telegramMessage = False
if telegramText == 'AC1 ON':
print("Setting relay 1: ON")
relay1Status = True
relay1.on()
elif telegramText == 'AC1 OFF':
print("Setting relay 1: OFF")
relay1Status = False
relay1.off()
elif telegramText == 'AC2 ON':
print("Setting relay 2: OFF")
relay2Status = True
relay2.on()
elif telegramText == 'AC2 OFF':
print("Setting relay 2: OFF")
relay2Status = False
relay2.off()
acStatusText = 'Status: '
if relay1Status == True:
acStatusText = acStatusText + 'AC1 ON, '
elif relay1Status == False:
acStatusText = acStatusText + 'AC1 OFF, '
if relay2Status == True:
acStatusText = acStatusText + 'AC2 ON.'
elif relay2Status == False:
acStatusText = acStatusText + 'AC2 OFF.'
bot.sendMessage(chat_id, acStatusText)
except KeyboardInterrupt:
relay1.off()
relay2.off()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment