• Usha Saha

Controlling Raspberry Pi's GPIOs using the Telegram bot

Updated: Sep 26, 2020


This article Controlling Raspberry Pi's GPIOs using the Telegram bot is all about how one can control the Raspberry Pi's #GPIOs(General Purpose Input/Output's) with a telegram bot. Let's have some idea about what is a Telegram bot. The Telegram bot is basically a platform provided by telegram where the #telegram is controlled by software rather than manually. The bot can perform functions like search, remind, teach, broadcast, and many more. So let's get headed towards the article and the codes implemented.


Hardware used

The list of components used are shown below;


Raspberry Pi 3 Model B+

The Raspberry Pi is a series of small single-board computers. Here 3 Model B+ indicates the generation of the RPi board. The Raspberry Pi is also written as RPI or RPi or Raspberry Pie. The Raspberry Pi Model B+ is of the nearby size of a credit card.


Raspberry Pi 3 Model  B+
Raspberry Pi 3 Model B+

Bread Board:- A breadboard is a rectangular board with small holes in it which are used for connecting elements. The connections are not fixed and can be varied. In a breadboard, all the holes in one horizontal line are in series connection with each other. Breadboards are made from plastic and come in all shapes, sizes, and even different colors, the most common sizes are "full-size," "half-size," and "mini" breadboards.

Bread Board
Bread Board

Jumper wires:- Jumper wires are used to establish a connection between different elements in the circuit and #Raspberrypi board. There are two types of wire, male and female. The male wires have the ends that can be connected to other ends and the female wires are the ones that have the receiving end.

Jumper Wires
Jumper Wires

USB Cable

This cable is used to connect the RaspberryPi to the power supply. It also helps in the transmission of data.



 USB Cable
USB Cable

LED:- A Light Emitting Diode is a semiconductor device that emits light when current flows through it.

LED's
LED's

Circuit Connections


The circuit connections are very easy to understand. Here the GPIO pins are connected to the anode of the two LEDs. The #GPIO 17 and 27 pins are connected to the two anode pins of the green and red led respectively. The cathode terminal of the diodes is grounded on the breadboard along with the negative terminal of the Raspberrypi board.


Circuit Connections
Circuit Connections


Codes


import time

The time module is imported.

import RPi.GPIO as GPIO

Next, the RaspberryPi GPIOs are imported. This would let us set the modes that are the input and the output modes.

import telepot

Next, the #telepot library is imported to get all the necessary configuration for the communication with the telegram application.

from telepot.loop import MessageLoop

Under the telepot library, the message loop is imported.

red = 27
green = 17

In the next step, the red and green LEDs are assigned pin 27 and 17 of the GPIO pin respectively.

GPIO.setmode(GPIO.BCM)

The mode is set for the GPIO pin. BCM is the pin that would be pointed on the board.

GPIO.setwarnings(False)

The warning is set as false.

#LED Red
GPIO.setup(red, GPIO.OUT)
GPIO.output(red, GPIO.LOW) #Off initially
#LED green
GPIO.setup(green, GPIO.OUT)
GPIO.output(green, GPIO.LOW) #Off initially

To start with initially, the GPIOs are set as output and made low for both the red and green led.

def action(msg):

In the next set of codes, a function called action is initiated through which the message from the telegram application would be used. And based on the message received the keywords would be extracted like turn off, green, red, etc.

chat_id = msg['chat']['id']

The chat id contains all the information regarding the #bot.

command = msg['text']

The text message part is the command to the RPi GPIO on which it has to respond.

print 'Received: %s' % command

The text that is received from the telegram is printed through this command.

if 'on' in command:
    message = "Turned on
if 'red' in command:
    message = message + "red "
    GPIO.output(red, GPIO.HIGH)
if 'green' in command:
    message = message + "green "
    GPIO.output(green, GPIO.HIGH)
if 'all' in command:
    message = message + "all "
    GPIO.output(red, GPIO.HIGH)
    GPIO.output(green, GPIO.HIGH)
    message = message + "light(s)"

Once the message is received then check for the keywords. For example if 'on' is the command that means the message would be "turned on" the led. Similarly, for the message "red " the meaning would be to turn on the red led that is the respective GPIO would be made high. Same for the message "green ". The message "all " implies turn on both the LEDs. This same action follows for the "off" mode as well.

message = message + "light(s)"
telegram_bot.sendMessage (chat_id, message)

At the end of the complete on and off code, a light string is added, and in the next code, this same message is sent to the telegram bot.

 telegram_bot = telepot.Bot('Your-API-Token-For-The-Bot')

Next, the telegram bot variable receives the message from the telepot module which in turn would be used to communicate with the RPi GPIO pins. Here the telegram bot variable contains the bot function from the telepot module that is used to communicate with the RPi over a channel created with the API key.


Here the #API token is the same that would be received after the successful formation of a bot.

print 'Start controlling...'

This command states that the RaspberryPi is ready to communicate with the bot.

while 1:
time.sleep(10)

After the entire process, the loop is made to continue for an infinite time with the "while" code with a time sleep of 1 sec.


Complete code

import time
import RPi.GPIO as GPIO
import telepot
from telepot.loop import MessageLoop

red = 27
green = 17

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#LED Red
GPIO.setup(red, GPIO.OUT)
GPIO.output(red, GPIO.LOW) #Off initially
#LED green
GPIO.setup(green, GPIO.OUT)
GPIO.output(green, GPIO.LOW) #Off initially

def action(msg):
chat_id = msg['chat']['id']
command = msg['text']
print 'Received: %s' % command
if 'on' in command:
    message = "Turned on
if 'red' in command:
    message = message + "red "
    GPIO.output(red, GPIO.HIGH)
if 'green' in command:
    message = message + "green "
    GPIO.output(green, GPIO.HIGH)
if 'all' in command:
    message = message + "all "
    GPIO.output(red, GPIO.HIGH)
    GPIO.output(green, GPIO.HIGH)
    message = message + "light(s)"
    telegram_bot.sendMessage (chat_id, message)
if 'off' in command:
     message = "Turned off "
if 'red' in command
      message = message + "red "
      GPIO.output(red, GPIO.LOW)
if 'green' in command:
     message = message + "green "
     GPIO.output(green, GPIO.LOW)
if 'all' in command:
     message = message + "all "
     GPIO.output(red, GPIO.LOW)
     GPIO.output(green, GPIO.LOW)    
     message = message + "light(s)"
     telegram_bot.sendMessage (chat_id, message)
     telegram_bot = telepot.Bot('Your-API-Token-For-The-Bot')
     MessageLoop(telegram_bot, action).run_as_thread()
     print 'Start controlling...'
     while 1:
     time.sleep(10)

Download the codes from here

tele_control (Main)
.txt
TXT • 2KB

Working

To start with download the telegram application form the play store, sign in making an account, and search for the #BotFather account. Under this click on start and this will initiate the process of creating the bot. On the left-hand side on can see the control commands from there choose the /newbot to create a new bot and furnish a few details like the name, username for the bot, and done with the bot creation process.


In this article, the working is based on fetching the keywords form the text that had been entered in the application. The next step is to open the newly created bot and start the session. With the program running in the background enter text like turn on/off any specific led and see the output on the led. Let us have a view on the implementation part.


For a better understanding lets understand with the help of images. The first image command to turn on the green led.


GREEN led on command
GREEN led on command

As a response to the command, the green led turns on.

GREEN led glowing
GREEN led glowing

Similarly, the next command would be to turn on the red led.

RED led on command
RED led on command

Responding to the command the red led turns on.


RED led Glowing
RED led Glowing

Turn off all the led command.

All led off command
All led off command

Both the LEDs are turned off.


All leds off
All leds off

Turn on both the LED's command.


All on command
All on command

Both the LEDs are turned ON.

All leds on
All leds on

In the same way, one can design for any other commands also like turn off one led. Turn off green led, or turn off red led and check for the outputs. If having any doubt go through the video configuration, the link is given below.

To sum up, through the article #Controlling Raspberry Pi's GPIOs using the Telegram bot one would learn how to connect the telegram bot in the mobile with the Raspberry Pi's along with how to command the Raspberry Pi's GPIOs and make actions more automated.


To learn more of such similar projects visit the links below

Interfacing servo motor with RPi

Interfacing Water/Rain Sensor with Arduino

Interfacing DHT11 sensor with Raspberry pi and LCD

90 views