In this Article we will see how we can use Telegram for Home Automation. Telegram is an extremely popular multi-platform messaging service. Just like any other messaging platform, it is used to send messages and exchange photos, videos, stickers, audio, and files of any type. Their user base is also enormous with around 400 million active users per month. However, unlike many widely used messaging services, Telegram has support for bots that can serve various functions. Some notable Telegram bots are storebot, deloreanbot, Wikipedia search, and stickerbot. Telegram also allows us to create our own bots. In this article, we will be creating our own bot in python and Telegram for Home Automation.
Hardware Setup
The hardware setup is fairly simple for this project. We will be using 5 different LEDs to represent the different appliances at our homes. You will have to make the connections as shown below.
- Connect the positive terminals of each of the LEDs to pins 31,33,36,38 and 40.
- Connect the negative terminals of all the LEDs to the ground rail of the breadboard.
Setting Up The Bot
- For this step, you need to open the Telegram app
- In the Search bar type
BotFather
- Once you open the chat you will have to follow something like this:
Code Explanation
If you would like to clone this code from GitHub you can find it here.
- First let’s import all the required packages.
import logging
from telegram.ext import Updater, CommandHandler
import RPi.GPIO as GPIO
- Next, we will have to setup our logger. This logger is required to view any errors that take place during runtime.
- Now let’s create a device dictionary that matches the name of the device to the pins that they are connected to.
device_dict = {"livingroom":31, "bedroom":36, "kitchen":33, "bathroom":38 ,"attic":40}
- Now we can define our first, command 1:
/start
. This command will just send out the following textHi!, run /help to view the commands
.
def start(update, context):
update.message.reply_text('Hi!, run /help to view the commands')
- command 2:
/listdevices
. This is the command that lists out all the devices (from device_dict) for the user to know what devices are connected.
def list_devices(update, context):
out_str = ""
for a in device_dict:
out_str+=a+'\n'
update.message.reply_text("Your devices are :" +'\n'+out_str)
- command 3:
/help
. This command will list out all the available commands.
def help(update, context):
update.message.reply_text('''commands:
/switchon your_device : start the switch on process
/switchoff your_device : start the switch off process
/listdevices : list out all available devices
''')
- command 4:
/switchon
. This command switches on the appliance based on the argument passed by the user. We can read the argument with the help of" ".join(context.args)
def switchon(update, context):
param = " ".join(context.args)
if param == "all":
for i in device_dict:
if i != "all":
GPIO.output(device_dict[i], GPIO.HIGH)
else:
out = device_dict[param]
GPIO.output(out, GPIO.HIGH)
- command 5:
/switchoff
. This command helps us switch off any appliance that has been turned on.
def switchoff(update, context):
param = " ".join(context.args)
if param == "all":
for i in device_dict:
if i != "all":
GPIO.output(device_dict[i], GPIO.LOW)
else:
out = device_dict[param]
GPIO.output(out, GPIO.LOW)
- Finally, we can create the main function.
def main_func():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(31, GPIO.OUT)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(33, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
GPIO.setup(40, GPIO.OUT)
updater = Updater("Your API Key", use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("listdevices", list_devices))
dp.add_handler(CommandHandler("switchon", switchon))
dp.add_handler(CommandHandler("switchoff", switchoff))
updater.start_polling()
updater.idle()
main_func()
Again, you can find the entire code on GitHub here
Output
Thank you for reading!! Hope you learnt something new about telegram bots as well as home automation.
Leave any comments, suggestions and questions in the comments section below, I will answer them as soon as possible
Happy Learning !! 😄