Learn how to use the Telegram Bot, host a Telegram Bot on your Raspberry Pi, and chat with new Hexabitz IoT device!
Telegram is an instant messaging app based on cloud. Telegram is available for all devices like Android, IOS, Windows, Mac and Linux. User can send stickers, messages, pics, audio files and videos. So, Telegram has an API bot that allows the human to talk to machines. Telegram bot is like a robot which programmed with a set of instruction and answer to interact with the user.
In this project, we are going to control Hexabitz modules connected with Raspberry Pi using Telegram Bot. For this, we will create a telegram bot which is capable of sending and receiving messages from a Raspberry Pi. So we can control any module using this Telegram bot π€π‘πβ¨
Steps:π€ Telegram Bot Setup:If your phone doesnβt have Telegram app then go to play store to download.
Now open the app and it will ask for your number. Register and verify with your number to create an account. Also, give a name to your account after the verification.
After this, it will take you to the home screen. Now we need to create a new bot that will send and receive messages with the Raspberry Pi. Search for BotFather in the search menu.
Now write β/startβ to start chatting with the bot.
After that, write β/newbotβ to request a new bot. Now it will ask you to enter a name for the new bot.
Next, it will ask you to enter a username for the bot. Enter a unique username to create your bot.
Therefore, check your received message, Obtain access token. Save it, as we will need it in the code.
Next, search for the bot using its username to confirm that the bot has been created.
π‘ 4-Pin USB-Serial Prototype Cable: The 4-pin USB 2.0 to UART serial cable is an indispensable tool for Hexabitz development! It incorporates FTDIβs FT232H USB to UART interface, which handles all the USB signalling and protocols. The cable comes with four pins (TXD, RXD, 3.3V, GND). The cable provides a fast, Inexpensive and simple way to connect with your Hexabitz modules and other hardware and power them directly with 3.3v / 500mA. The cable comes with 4-pin, color-coded, female connectors that are compatible with 0.1β pitch male headers.
If you are using standard FTDI USB-UART 3.3V cable for power and communication, connect the colored cable wires as follows:
- Red (VCC) to 3.3V (top power pad, i.e., corner edge pad).
- Black (GND) to GND (bottom power pad, i.e., corner edge pad).
- Yellow (RXD) to Module TXD (top communication pad, i.e., side edge pad).
- Orange (TXD) to Module RXD (bottom communication pad, i.e., side edge pad).
π‘ Raspberry Pi Interface (HF1R0x):
Hexabitz HF1R0x module enables you to seamlessly interface your modules to a Raspberry Pi 3B/3B+/4B single-board computer.
If you are using HF1R0 module, make sure to connect your Hexabitz array at the right or left spot and put the Jumper at the right place.
π‘ 600V / 1.2A Solid State Relay (H0FR6x):
H0FR6x is a compact solid state relay (SSR) module capable of driving AC loads up to 600V and 1.2A. It is based on Panasonic AQH3213A SSR and STM32F0 MCU.
WARNING: Voltages higher than 24V can be dangerous! Make sure you know what you're doing and use protection (circuit breakers, etc.) whenever possible.
Be sure to download the firmware on the modules before using them:
https://hexabitz.com/docs/how-to/update-module-firmware/
π₯οΈ Raspberry Pi setup:The Raspberry Pi is great for powering all kinds of home automation projects. The Raspberry Pi to power a system where you can remotely turn on/off a lamp from anywhere. It's not just a lamp either, you can turn anything from any mobile device or from internet.
Start with installing thetelepot library on your Pi. Type the below command in the terminal to install it:
sudo pip install telepot
Telepot helps you build applications for Telegram Bot API. It works on Python 2.7 and Python 3. For Python 3.5+, it also has an async version based on asyncio.
BotFather gave you a token, something like
123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ
. With the token in hand, you can start using telepot to access the bot account.
Whenever the Pi receives a message from the Telegram bot, it will call "handle" function and this function reads the message and separate the text from it.
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
print('Got command: %s' % command)
You can using if condition will check for keywords and control the module according to the keyword. While message string is used to reply back to the user.
Now, in the below code enter token details in the commas. You can use βbot.getMe()β will to check whether a connection between the Pi and the Telegram bot was made successfully by printing a response.
Python pyserial:This module encapsulates the access for the serial port. It provides backends for Python running on Windows, OSX, Linux, BSD (possibly any POSIX compliant system) and IronPython. The module named βserialβ automatically selects the appropriate backend.
Depending on the version of python, there are several commands to install the package pyserial.
sudo apt-get install python-serial python3-serial
Python code to read the serial port: This section of code primarily instantiates the serial class, setting it up with all the various bits of information that it needs to make the connection with.
port β This defines the serial port that the object should try and do read and writes over.
baudrate β This is the rate at which information is transferred over a communication channel.
parity β Sets whether we should be doing parity checking, this is for ensuring accurate data transmission between nodes during communication.
stopbits β This is the pattern of bits to expect which indicates the end of a character or the data transmission.
bytesize β This is the number of data bits.
timeout β This is the amount of time that serial commands should wait for before timing out.
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate = 921600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
Test the System ππ² βοΈπ‘π§©Start your Bot and send your messages π¨οΈ
Look at your Pi, you can see Hexabitz relay states when you send "on", "pwm", "off" to the bot.
Hence, we have successfully controlled Hexabitz H0FR6 module with the telegram app, you can also replace it with any other modules.
And you can connect many H0FR6 modules together and combine with H0FR1 to drive DC loads as well.
Note: If you want to connect more than one module (array). Check out the following links:
https://www.hackster.io/aula-jazmati/remote-access-a-raspberry-pi-hf1r0-interface-3f116f
https://hexabitz.com/docs/how-to/make-a-pre-built-array-topology-file/
Comments