Telegram Bot with Raspberry Pi PLC controller
Tutorial for controlling GPIOs of the Open Source PLC Raspberry Pi with Telegram
20 July, 2021 by
Telegram Bot with Raspberry Pi PLC controller
Boot & Work Corp. S.L., Amil Shrivastava


Introduction

Ever thought about controlling your Programmable Logic Controller with only text messages via Telegram? Or even better, getting scheduled updates or alerts from your PLC in real time to your Telegram application? Well, it is now possible with industrial automation. 

In this tutorial, we will learn how to use the GPIO pins of Industrial Shields Raspberry Pi based PLC with a Telegram bot for industrial control. Once you implement this, you can modify this as per your application and requirements.

Requirements

Step 1: Create a Bot in Telegram

a) To this, you will need to open the application and search for @BotFather. BotFather can create and manage bots.

b) Send /start to BotFather to receive a list of commands. ( "/" is a Telegram convention of sending commands to the bot).

c) To make a new bot, send /newbot. Following this the BotFather will send you instructions to follow such as deciding the name, username etc.

d) Once that is done, you will receive a "Token". Make sure you save it and not share it with untrusted parties.

Step 1: Create a Bot in Telegram-Telegram Bot with Raspberry Pi PLC

Step 2: Creating Python script to communicate with the bot from the PLC

a) Install the dependencies 

$sudo apt-get install python-pip
$sudo pip install telepot

b) Test your token

Enter the Python console by typing $python in your terminal. Following this, enter the following commands. You should receive a dictionary with your bot's details. If not, then there must be some error.

>>>import telepot
>>>bot = telepot.Bot('*** copy bot token from browser ***')
>>>bot.getMe()

Step 2: Creating Python script to communicate with the bot from the PLC-Telegram Bot with Raspberry Pi PLC

c) Create the .py file with your code in it to control the digital outputs of the PLC. The idea for this tutorial is to turn an LED on and off by sending messages on Telegram. 

$sudo nano bottest.py

Code

import os
import time
import telepot
from telepot.loop import MessageLoop

def test(msg):
    chat_id = msg['chat']['id']
    command = msg['text']
    print 'Got command: %s' % command
    if command == '/on':
        bot.sendMessage(chat_id, 'The LED is on!')
        os.system("sudo ./set-digital-output Q0.4 1")
    if command == '/off':
        bot.sendMessage(chat_id, 'The LED is off!')
        os.system("sudo ./set-digital-output Q0.4 0")
elif command == 'Is the bot working fine?':
bot.sendMessage(chat_id, 'Yes it is')

bot = telepot.Bot('***Your Token***')
MessageLoop(bot, handle).run_as_thread()
print 'I am listening ...'
while 1:
    time.sleep(10)
Make sure you keep the "set-digital-output" file from "/home/pi/test/analog/" in the same folder as your "bottest.py"

Step 3: Once the file has been created, it is time to run it and test the bot.

a) Run the following command to run the file that we just created.

$python bottest.py

b) In the Telegram applications, run /on and /off to test. 

Testing /on and /off - Step 3: Once the file has been created, it is time to run it and test the bot. -Telegram Bot with Raspberry Pi PLC

And voilà, now your industrial Raspberry Pi PLC is remote controlled with your Telegram application. 

Related Links

Basics about digital outputs of Raspberry PLC

Telegram Bot with Raspberry Pi PLC controller
Boot & Work Corp. S.L., Amil Shrivastava
20 July, 2021
Share this post
Archive

Looking for your ideal PLC?

Take a look at this product comparison with other industrial controllers Arduino-based.

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>

Do you want more information?

Just fill the form!

Tell me more!