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 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()
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
from telepot.loop import MessageLoop
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***')
print 'I am listening ...'
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.
b) In the Telegram applications, run /on and /off to test.
And voilà, now your industrial Raspberry Pi PLC is remote controlled with your Telegram application.