Re: How to get the Bluetooth module working (JYMCU...)?
Posted: Fri Dec 05, 2014 10:13 pm
I continued in turbinreiters thread instead. better to keep it in one thread.
Please see the new forum at
https://forum.micropython.org/
Code: Select all
import pyb
def bt_tx(data):
# Transmit data, wait, check for received data and print it
print('\nTransmitting: ', data)
n = uart.write(data_tx)
pyb.delay(100) # Maybe not needed
data_rx = uart.read()
print('\tReceived: ', data_rx)
pyb.delay(100) # Maybe not needed
# rescue_mode = False
rescue_mode = True
sw = pyb.Switch()
pyb.LED(1).off()
pyb.LED(2).on()
p_key = pyb.Pin('X1', pyb.Pin.OUT_PP)
# AT mode can be initialised in two ways
# 1: Key-pin low at power up => use configured rate
# 2: Key-pin high at power up => use 38400 BPS
if(rescue_mode):
p_key.high()
# AT commands to be sent at 38400 BPS
uart = pyb.UART(1, 38400)
else:
p_key.low()
# AT commands to be sent at configured BPS
uart = pyb.UART(1, 9600)
print('Disconnect and reconnect power to bluetooth module')
print('Click switch')
# Red LED on HC-05 blinks fast in normal mode, slow in rescue mode
wait = True
while(wait):
if sw():
pyb.LED(1).on()
pyb.LED(2).off()
wait = False
p_key.high() # High for normal AT-mode
# Check if AT mode is working, module should respond with 'OK'
data_tx = 'AT\r\n'
bt_tx(data_tx)
# Ask module for version
data_tx = 'AT+VERSION?\r\n'
bt_tx(data_tx)
# Ask module for name
data_tx = 'AT+NAME?\r\n'
bt_tx(data_tx)
# Set new module name
data_tx = 'AT+NAME=MicroPython HC-05\r\n'
bt_tx(data_tx)
# Ask module for name
data_tx = 'AT+NAME?\r\n'
bt_tx(data_tx)