Monday, February 8, 2021

Raspberry Pi Pico/MicroPython: pair HC-05 and HC-06 Bluetooth 2.0 Modules


This exercise run MicroPython on Raspberry Pi Pico, program to pair HC-05 (connected to UART 0) and HC-06 (connected to UART 1). Then run a simple test program to verify.

Please note HC-05 and HC-06 are aged products and have many variants. My test is base on my modules bought at about 2015/16. It may be not same as yours.

HC-05 version: 2.0-20100601
HC-06 version: linvorV1.8

Enter AT-command mode:

HC05:
Press & Hold the onboard button while power on.

HC06:
Power-up in NOT CONNECTED

Run the MicroPython code on Raspberry Pi Pico to pair the HC-05/HC-06.

mPico_pair_HC-05-06_20210209a.py
import uos
import machine
import utime

"""
Raspberry Pi Pico/MicroPython
Pair HC-05 connected to UART0 to HC-06 connected to UART1

default UART
UART(0, baudrate=9600, bits=8, parity=None, stop=1, tx=0, rx=1)
UART(1, baudrate=115200, bits=8, parity=None, stop=1, tx=4, rx=5)

Connection:
RPi Pico UART0  HC-05
GP0(pin 1)      RX
GP1(pin 2)      TX

RPi Pico UART1  HC-06 
GP4(pin 6)      RX
GP5(pin 7)      TX

To enter AT-Command mode-
HC05:
Press & Hold the onboard button while power on.

HC06:
Power-up in NOT CONNECTED

for HC-05 only:
ROLE  0 = Slave
      1 = Master
      2 = Slave-loop
CMODE 0 = connect fixed address
      1 = connect any address
      2 = Slave-loop
"""

print(uos.uname())
uart0 = machine.UART(0,baudrate=38400)  #at-command
uart1 = machine.UART(1,baudrate=9600)   #at-comand

#2 sec timeout is arbitrarily chosen
def sendCMD_waitResp(cmd, uart=uart0, timeout=2000):
    print("CMD: " + cmd)
    uart.write(cmd)
    waitResp(uart, timeout)
    print()
    
def waitResp(uart=uart0, 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)

#indicate program started visually
led_onboard = machine.Pin(25, machine.Pin.OUT)
led_onboard.value(0)     # onboard LED OFF/ON for 0.5/1.0 sec
utime.sleep(0.5)
led_onboard.value(1)
utime.sleep(1.0)
led_onboard.value(0)

print(uart0)
print(uart1)

print("- uart0 -")
waitResp()
sendCMD_waitResp("AT\r\n")
sendCMD_waitResp("AT+ORGL\r\n")           #Restore default setting
sendCMD_waitResp("AT+VERSION\r\n")
sendCMD_waitResp("AT+UART?\r\n")
sendCMD_waitResp("AT+UART=9600,0,0\r\n")  #9600 baud, 1 stop, parity=none
sendCMD_waitResp("AT+UART?\r\n")
sendCMD_waitResp("AT+PSWD?\r\n")
sendCMD_waitResp("AT+PSWD=4321\r\n")      #Set PIN = "4321"
sendCMD_waitResp("AT+PSWD?\r\n")
sendCMD_waitResp("AT+ROLE?\r\n")
sendCMD_waitResp("AT+ROLE=1\r\n")         #Master
sendCMD_waitResp("AT+ROLE?\r\n")
sendCMD_waitResp("AT+CMODE?\r\n")
sendCMD_waitResp("AT+CMODE=1\r\n")        #connect any address
sendCMD_waitResp("AT+CMODE?\r\n")

sendCMD_waitResp("AT+ADDR?\r\n")

print("- uart1 -")
waitResp()
sendCMD_waitResp("AT", uart1)
sendCMD_waitResp("AT+VERSION", uart1)
sendCMD_waitResp("AT+NAMEHC-06", uart1)
sendCMD_waitResp("AT+PIN4321", uart1)     #Set PIN = "4321"

print("Done")

The REPL out should be like this:
>>> %Run -c $EDITOR_CONTENT
(sysname='rp2', nodename='rp2', release='1.14.0', version='v1.14 on 2021-02-02 (GNU 9.3.0 MinSizeRel)', machine='Raspberry Pi Pico with RP2040')
UART(0, baudrate=38400, bits=8, parity=None, stop=1, tx=0, rx=1)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, tx=4, rx=5)
- uart0 -
b''
CMD: AT

b'OK\r\n'

CMD: AT+ORGL

b'OK\r\n'

CMD: AT+VERSION

b'+VERSION:2.0-20100601\r\nOK\r\n'

CMD: AT+UART?

b'+UART:38400,0,0\r\nOK\r\n'

CMD: AT+UART=9600,0,0

b'OK\r\n'

CMD: AT+UART?

b'+UART:9600,0,0\r\nOK\r\n'

CMD: AT+PSWD?

b'+PSWD:1234\r\nOK\r\n'

CMD: AT+PSWD=4321

b'OK\r\n'

CMD: AT+PSWD?

b'+PSWD:4321\r\nOK\r\n'

CMD: AT+ROLE?

b'+ROLE:0\r\nOK\r\n'

CMD: AT+ROLE=1

b'OK\r\n'

CMD: AT+ROLE?

b'+ROLE:1\r\nOK\r\n'

CMD: AT+CMODE?

b'+CMOD:0\r\nOK\r\n'

CMD: AT+CMODE=1

b'OK\r\n'

CMD: AT+CMODE?

b'+CMOD:1\r\nOK\r\n'

CMD: AT+ADDR?

b'+ADDR:2014:12:20016\r\nOK\r\n'

- uart1 -
b''
CMD: AT
b'OK'

CMD: AT+VERSION
b'OKlinvorV1.8'

CMD: AT+NAMEHC-06
b'OKsetname'

CMD: AT+PIN4321
b'OKsetPIN'

Done


Power off and on HC-05 in normal mode. It will connect to paired HC-06 automatically.

Run this code to verify, mpyPico_comm_9600_20210209a.py
import uos
import machine
import utime

"""
Raspberry Pi Pico/MicroPython to test bluetooth/Sertal
bi-direction commnunication between HC-05/HC-06

default UART
UART(0, baudrate=9600, bits=8, parity=None, stop=1, tx=0, rx=1)
UART(1, baudrate=115200, bits=8, parity=None, stop=1, tx=4, rx=5)

both set to 9600 baud

Connection:
RPi Pico UART0  HC-05/06
GP0(pin 1)      RX
GP1(pin 2)      TX

RPi Pico UART1  HC-05/06 
GP4(pin 6)      RX
GP5(pin 7)      TX

To enter normal mode:
HC-05:
Just power on WITHOUT pressing the onboard button
HC-06:
Just power on

"""

print(uos.uname())
uart0 = machine.UART(0,baudrate=9600)
uart1 = machine.UART(1,baudrate=9600)
    
def clartBuf(uart=uart0):
    print("Clear UART buffer "+ str(uart))
    while uart.any():
        print(uart.read(1))

#indicate program started visually
led_onboard = machine.Pin(25, machine.Pin.OUT)
led_onboard.value(0)     # onboard LED OFF/ON for 0.5/1.0 sec
utime.sleep(0.5)
led_onboard.value(1)
utime.sleep(1.0)
led_onboard.value(0)

print(uart0)
print(uart1)

clartBuf()
clartBuf(uart1)

#Master HC-05 send a dummy bytes in lower case
uart1.write("1234567890abcdefghijklmnopqrstuvwxyz\r\n")

#Slave HC-05 convert received bytes to upper case, and echo back.
#if you see in REPL a byte in lower case, it is send from Master to Slave,
#in upper case, it is echo back from Slave to Master.
prvMills = utime.ticks_ms()
bUart0 = b''
bUart1 = b''
while (utime.ticks_ms()-prvMills)<3000:
    if uart0.any():
        b0 = uart0.read(1)
        bUart0 = bUart0 + b0
        print("UART(0): " + b0.decode('utf-8'))
        uart0.write(b0.upper().decode('utf-8'))
    if uart1.any():
        b1 = uart1.read(1)
        bUart1 = bUart1 + b1
        print("UART(1): " + b1.decode('utf-8'))

print("UART0: ")
print(bUart0)
print("UART1: ")
print(bUart1)

print("===========")
if bUart0 == bUart1.lower():
    print("MATCH")
else:
    print("UN-MATCH!!!")
print("===========")
    
print("- Done -")


REPL output:
>>> %Run -c $EDITOR_CONTENT
(sysname='rp2', nodename='rp2', release='1.14.0', version='v1.14 on 2021-02-02 (GNU 9.3.0 MinSizeRel)', machine='Raspberry Pi Pico with RP2040')
UART(0, baudrate=9600, bits=8, parity=None, stop=1, tx=0, rx=1)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, tx=4, rx=5)
Clear UART buffer UART(0, baudrate=9600, bits=8, parity=None, stop=1, tx=0, rx=1)
Clear UART buffer UART(1, baudrate=9600, bits=8, parity=None, stop=1, tx=4, rx=5)
UART(0): 1
UART(0): 2
UART(0): 3
UART(0): 4
UART(1): 1
UART(0): 5
UART(1): 2
UART(0): 6
UART(1): 3
UART(0): 7
UART(1): 4
UART(0): 8
UART(1): 5
UART(0): 9
UART(1): 6
UART(0): 0
UART(1): 7
UART(0): a
UART(1): 8
UART(0): b
UART(1): 9
UART(0): c
UART(1): 0
UART(0): d
UART(1): A
UART(0): e
UART(1): B
UART(0): f
UART(1): C
UART(0): g
UART(1): D
UART(0): h
UART(1): E
UART(0): i
UART(1): F
UART(0): j
UART(1): G
UART(0): k
UART(1): H
UART(0): l
UART(1): I
UART(0): m
UART(1): J
UART(0): n
UART(1): K
UART(0): o
UART(1): L
UART(0): p
UART(1): M
UART(0): q
UART(1): N
UART(0): r
UART(1): O
UART(0): s
UART(1): P
UART(0): t
UART(1): Q
UART(0): u
UART(1): R
UART(0): v
UART(1): S
UART(0): w
UART(1): T
UART(0): x
UART(1): U
UART(0): y
UART(1): V
UART(0): z
UART(1): W
UART(0): 
UART(1): X
UART(0): 

UART(1): Y
UART(1): Z
UART(1): 
UART(1): 

UART0: 
b'1234567890abcdefghijklmnopqrstuvwxyz\r\n'
UART1: 
b'1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n'
===========
MATCH
===========
- Done -

Next:

No comments: