8 except ModuleNotFoundError
:
9 print("Please install pyserial module first.")
32 serial
.PARITY_NONE
:b
'N',
33 serial
.PARITY_ODD
: b
'O',
34 serial
.PARITY_EVEN
:b
'E'
37 def send(cmd
, timeout
=1):
38 print("<<" + cmd
.decode('utf8'))
42 ti
= time
.perf_counter()
45 if (ser
.in_waiting
> 0):
46 # When receiving data, reset timer and shorten timeout
47 ti
= time
.perf_counter()
51 # We stop either after 1s if no data or 50ms after last data received
52 if time
.perf_counter() - ti
> wait
:
55 print(">>" + out
.decode('utf8'))
58 def usart_bt_testcomm(baudrate
, parity
):
59 print("Configuring UART: %i 8%s1" % (baudrate
, p2c
[parity
].decode('utf8')))
65 stopbits
=serial
.STOPBITS_ONE
,
66 bytesize
=serial
.EIGHTBITS
74 if __name__
== '__main__':
75 print("WARNING: process only if strictly needed!")
76 print("This requires HC-06 dongle turned ON and NOT connected!")
77 if input("Is the HC-06 dongle LED blinking? (Say 'n' if you want to abort) [y/n] ") != 'y':
81 print("\nTrying to detect current settings... Please be patient.")
83 if not usart_bt_testcomm(115200, serial
.PARITY_NONE
):
84 brs
= [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1382400]
85 ps
= [ serial
.PARITY_NONE
, serial
.PARITY_ODD
, serial
.PARITY_EVEN
]
88 for p
, b
in [(i
, j
) for i
in ps
for j
in brs
]:
89 if usart_bt_testcomm(b
, p
):
92 print("Sorry, add-on not found. Abort.")
95 print("Reconfiguring add-on to default settings.")
97 resp
=send(b
'AT+VERSION')
99 resp
=send(b
'AT+NAME%s' % name
)
101 resp
=send(b
'AT+PIN%s' % pin
)
103 resp
=send(b
'AT+ROLE=%s' % role
)
104 # Change BT Parity N:
107 resp
=send(b
'AT+BAUD%s' % baud2id
[115200])
111 print("Trying to connect add-on with the new settings.")
115 parity
=serial
.PARITY_NONE
,
116 stopbits
=serial
.STOPBITS_ONE
,
117 bytesize
=serial
.EIGHTBITS
120 if (send(b
'AT', timeout
=2) == b
'OK'):
121 print("HC-06 dongle successfully reset")
123 print("Lost contact with add-on, please try again")