Aula Jazmati
Published © MIT

Control Hexabitz Modules Using Raspberry Pi and Telegram Bot

Learn how to use the Telegram Bot, host a Bot on your Raspberry Pi, and use the messaging app to interact with your Hexabitz modules.

IntermediateFull instructions provided1 hour371
Control Hexabitz Modules Using Raspberry Pi and Telegram Bot

Things used in this project

Story

Read more

Schematics

Schematic

Code

Test with Raspberry Pi interface module(HF1R0x)

Python
import sys
import time
import telepot
import RPi.GPIO as GPIO
import serial
ser = serial.Serial(        
               port='/dev/ttyS0',
               baudrate = 921600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=0
           )
print(ser.name)
ser.write ('\r'.encode())
time.sleep(4) 
ser.write ('ping'.encode())
ser.write ('\r'.encode())
x=ser.read(3000)
print(x)
time.sleep(10)
def on():
        ser.write ('on 10000'.encode())
        ser.write ('\r'.encode())
        print('Hi')
        
def pwm():
        ser.write ('pwm 50'.encode())
        ser.write ('\r'.encode())
def pwm1():
        ser.write ('pwm 80'.encode())
        ser.write ('\r'.encode())
def off():
        ser.write ('off'.encode())
        ser.write ('\r'.encode())      
def handle(msg):
        chat_id = msg['chat']['id']
        command = msg['text']

        print('Got command: %s' % command)

    if command == 'On':
       on()
       time.sleep(0.2)
    elif command =='Off':
       off()
       time.sleep(0.2)
       
    elif command =='pwm':
       pwm()
       time.sleep(0.2)
    elif command =='pwm1':
       pwm1()
       time.sleep(0.2)
bot = telepot.Bot('Put your bot token here')
bot.message_loop(handle)
print('I am listening...')

while 1:
    try:
        time.sleep(10)
    
    except KeyboardInterrupt:
        print('\n Program interrupted')
        exit()
    
    except:
        print('Other error or exception occured!')

Test with FTDI USB-UART 3.3V cable

Python
import sys
import time
import telepot
import RPi.GPIO as GPIO
import serial
ser = serial.Serial(        
               port='/dev/ttyUSB0',
               baudrate = 921600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=0
           )
print(ser.name)
ser.write ('\r'.encode())
time.sleep(4) 
ser.write ('ping'.encode())
ser.write ('\r'.encode())
x=ser.read(3000)
print(x)
time.sleep(10)
def on():
    ser.write ('on 1000'.encode())
    ser.write ('\r'.encode())
    print('Hi')
        
def pwm():
    ser.write ('pwm 50'.encode())
    ser.write ('\r'.encode())
def off():
    ser.write ('off'.encode())
    ser.write ('\r'.encode())      
def handle(msg):
    chat_id = msg['chat']['id']
    command = msg['text']

    print('Got command: %s' % command)

    if command == 'On':
       on()
       #bot.sendMessage(chat_id,text="Hexabitz AC relay ON ")
       time.sleep(0.2)
    elif command =='Off':
       off()
       time.sleep(0.2)
       
    elif command =='pwm':
       pwm()
       time.sleep(0.2)

bot = telepot.Bot('Put your bot Token here')
bot.message_loop(handle)
print('I am listening...')

while 1:
    try:
        time.sleep(10)
    
    except KeyboardInterrupt:
        print('\n Program interrupted')
        exit()
    
    except:
        print('Other error or exception occured!')

Credits

Aula Jazmati

Aula Jazmati

49 projects β€’ 192 followers
(PhD) in Electronic Engineering 2023 πŸ’‘πŸ•ŠοΈ

Comments