Controlling Electrical Appliances Through Telegram Message Using Raspberry Pi

Normally we send SMS to control, how about Telegram message?

INTRODUCTION

Besides using Telegram message to do display on dot matrix, we also can control things, such as relay modules, to on and off. To do that, we need to compare the message received and assign what task to do. In this video tutorial, we will share on how to control electrical appliances using Telegram messages and Raspberry Pi.

Before that, you may need to refer following tutorial first:

  1. Getting Started With Maker pHAT
  2. Displaying Telegram Message on Dot Matrix Using Raspberry Pi

VIDEO

This video will show you on how to control electrical appliances through Telegram message using Raspberry Pi.

Untuk versi bahasa Melayu, sila rujuk video dibawah.

HARDWARE PREPARATION

Item used in the video.

Wiring connection table.

Maker pHATRelay 2 ChannelsAC SocketAC Plug
5VVCC  
GNDGND  
13IN1  
19IN2  
 COM1 Live
 COM2 Live
 NO1Live1 
 NO2Live2 
  Neutral1Neutral
  Neutral2Neutral

Sample Code

This is the sample code used in the video. Anyway it will be updated from time to time.

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)
view raw TelegramBotRelay.py hosted with ❤ by GitHub

Thank You

References:

  1. Getting Started With Maker pHAT
  2. Displaying Telegram Message on Dot Matrix Using Raspberry Pi 

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

 

3 thoughts on “Controlling Electrical Appliances Through Telegram Message Using Raspberry Pi”

  1. NUR ZAWATIL ISQI BINTI ZAKARIA

    I world like to make this project with my students. May I know the total cost for this project? It’s more helpful to have a proper quotation send to my email. Where should I ask for it?

  2. thank you for this tutorial,
    i’m hoping in the future cytron can also make any tutorial in PDF format with picture or tutorial in summary with pdf format

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

Program micro:bit V2 Using MicroPython On Raspberry Pi
Basic Comparison Between MicroPython and Arduino
The Octolapse V0.4.1 With Raspberry Pi Camera
Getting Started With Node-RED Using Raspberry Pi
How Raspberry Pi Has Changed the World
Scroll to Top