This exercise run on Raspberry Pi Pico/MicroPython, communicate with HC-08 via UART.
Firstly, it send a sequence of AT command to HC-08 to reset the module to factory setting, and read information about it.
Then stay in a loop to wait any income data from BLE, print on REPL terminal and each to BLE.
It's tested with Android App Serial Bluetooth Terminal.
Connection:
Pico HC-08
--------------------
3V3 3V3
GND GND
GP4(Tx) Rx
GP5(Rx) Tx
Example code, mpyPico_uart_HC08.py
"""
RPi Pico (MicroPython) exercise:
work with HC-08 Bluetooth UART Module
tested with 'HC-08 V3.1,2017-07-07'
"""
import os
import utime
import machine
#print sys info
print(os.uname())
#indicate program started visually
led_onboard = machine.Pin(25, machine.Pin.OUT)
led_onboard.value(0) # onboard LED OFF for 0.5 sec
utime.sleep(0.5)
led_onboard.value(1)
#2 sec timeout is arbitrarily chosen
def sendCMD_waitResp(cmd, timeout=2000):
print("CMD: " + cmd)
uart.write(cmd)
waitResp(timeout)
print()
def waitResp(timeout=2000):
prvMills = utime.ticks_ms()
resp = b""
while (utime.ticks_ms()-prvMills)<timeout:
if uart.any():
resp = b"".join([resp, uart.read(1)])
print(resp)
def sendCMD_waitRespLine(cmd, timeout=2000):
print("CMD: " + cmd)
uart.write(cmd)
waitRespLine(timeout)
print()
def waitRespLine(timeout=2000):
prvMills = utime.ticks_ms()
while (utime.ticks_ms()-prvMills)<timeout:
if uart.any():
print(uart.readline())
#print uart info
uart = machine.UART(1, baudrate=9600,
bits=8, parity=None, stop=1)
print(uart)
#clear bufer in UART
waitResp()
sendCMD_waitResp("AT")
sendCMD_waitResp("AT+") #invalid command
sendCMD_waitResp("AT+T") #invalid command
sendCMD_waitResp("AT+DEFAULT")
#Restore default setting
# The module will reset, restart for 200ms
utime.sleep(0.5)
sendCMD_waitResp("AT")
sendCMD_waitRespLine("AT+RX") #check basic parameters
sendCMD_waitResp("AT+VERSION") #version and date
sendCMD_waitResp("AT+ROLE=?") #Master/Slave role
sendCMD_waitResp("AT+ROLE=x") #invalit setting
sendCMD_waitResp("AT+ROLE=?")
sendCMD_waitResp("AT+NAME=?") #Name
sendCMD_waitResp("AT+ADDR=?") #hardware address
sendCMD_waitResp("AT+RFPM=?") #RF power
sendCMD_waitResp("AT+BAUD=?") #UART baud
sendCMD_waitResp("AT+CONT=?") #connect ability
sendCMD_waitResp("AT+LUUID=?") #Search UUID
sendCMD_waitResp("AT+SUUID=?") #Service UUID
sendCMD_waitResp("AT+TUUID=?") #Characteristic UUID
print("running...")
print("----------")
while True:
line = uart.readline()
print(line)
uart.write(line)
print()
print("- bye -")
Next:
HC-08 Bluetooth UART communication module is a new generation of Bluetooth specification V4.0 BLE Bluetooth protocol based on the transmission module.
Product page: http://www.hc01.com/products/6
HC-08 English datasheet: http://www.hc01.com/downloads/HC-08A%20version%20english%20datasheet.pdf
No comments:
Post a Comment