Showing posts with label HC-06 Bluetooth Module. Show all posts
Showing posts with label HC-06 Bluetooth Module. Show all posts

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:

Tuesday, November 3, 2015

Raspberry Pi Bluetooth Serial Terminal, using HC-06 Bluetooth Module.


HC-06 is Bluetooth to UART Converter. This  post show how to config Raspberry Pi 2 running Raspbian Jessie, to connect HC-06 with serial port. Connect and log-in from Windows 10.

Connect HC-06 to Raspberry Pi GPIO connector:
RPi TXD - HC-06 RXD
RPi RXD - HC-06 TXD
RPi 3.3V - HC-06 VCC
RPi GND - HC-06 GND
*make sure your HC-06 is operate on 3.3V, NOT 5V.


By default, HC-06 work on 9600 baud. So we have to edit /boot/cmdline.txt in Raspberry Pi/Raspbian Jessie, to set 9600 baud.

in default:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

modify (with sudo right) default 115200 to 9600.
dwc_otg.lpm_enable=0 console=ttyAMA0,9600 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

Then you can connect and login from Windows 10 with Bluetooth.

This video show how to config Raspberry Pi, and log-in from Windows 10 using PuTTY. In this video, Raspberry Pi desktop is Remote Desktop Connection via WiFi. PuTTY run as Bluetooth Serial Terminal.




more:
- Android Bluetooth Terminal to login Raspberry Pi + HC-06

JBtek HC-06 Bluetooth to UART Converter COM Serial Communication Slave Mode

JBtek HC-06 Bluetooth to UART Converter COM Serial Communication Slave Mode
  • Industrial serial port bluetooth, Drop-in replacement for wired serial connections, transparent usage. You can use it simply for a serial port replacement to establish connection between MCU and GPS, PC to your embedded project and etc. Computer and peripheral devices
  • GPS receiver Industrial control MCU projects
  • Running in slave role: Pair with BT dongle and master module
  • Coupled Mode: Two modules will establish communication automatically when powered. PC hosted mode: Pair the module with bluetooth dongle directly as virtual serial.
  • Default serail port setting : 9600, 1 Pairing code : 1234
Product Description
Item Name Bluetooth UART RS232 serial converter Module Operating Frequency Band 2.4GHz unlicensed ISM band Bluetooth Specification V2.0+EDR Output Power Class Class 2 Operating Voltage 3.3V Host Interface USB/UART Audio Interface None Flash Memory Size 8Mbit

Dimensions 27.2mm x 12.8mm x 2.5mm
Net Weight 9.6 g / 0.3 oz
Key Features: supports Slave mode, modified by AT commander
  1. CSR Bluetooth Chip Solution 
  2. Bluetooth Spec v2.0+EDR Compliant 
  3. Enhanced Data Rate (EDR) compliant with V2.0.E.2 of specification for both 2Mbps and 3Mbps modulation modes 
  4. Full Speed Bluetooth Operation with Full Piconet Support and Scatternet Support 
  5. Increadible samll size with 3.3V input, and RoHS Compliant 
  6. UART interface and with baudrate setup function 
  7. Support for 8Mbit External Flash Onboard 
  8. Support for 802.11Co-Existence Product Description 
The Bluetooth UART RS232 serial Converter Module can easily transfer the UART data through the wireless Bluetooth, without complex PCB layout or deep knowledge in the Bluetooth software stack, you can combine this bluetooth module with your system: * MCU, ARM or DSP systems. * SOC systems. * Personal Digital Assistants (PDAs) * Computer Accessories * Other systems your want to use under bluetooth functions. [Hardware] CSR solution, Bluetooth V2.0 protocol support AT commander support Master and Slave mode, modify by AT commander support 1200-115200 baudrate 3.3V single supply
Size:
core module: 28mm*15mm*2.35mm,
whole module:35.33mm*19.6mm*3.7mm
current: 30mA MAX can be used with all MCU by UART port 2 indicator lights