In compliance with high industry standards

Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Telegram Bot with Raspberry Pi PLC

Tutorial for controlling GPIOs of Raspberry Pi PLC with

Ever thought about controlling your PLC 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. 

In this tutorial, we will learn how to use the GPIO pins of Industrial shield's Raspberry PLC with a Telegram bot. Once you implement this, you can modify this as per your application and requirements. 



Requirements : 

Raspberry PLC

Telegram Account


Steps: 


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 recieve 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 recieve a "Token". Make sure you save it and not share it with untrusted parties.


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 recieve 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()




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 . 


And voila, you are now controlling your Raspberry pi PLC with your telegram application. 





Related Links:

Basics about digital outputs of Raspberry PLC








Looking for your ideal PLC?

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

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


Industrial PLC controller comparison >>

Do you want more information?

Just fill the form!

Tell me more!