bluetoooth.py
900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import uos
import machine
import utime
uart0 = machine.UART(0,baudrate=9600)
# --------------------------------------------------- #
# INTERNAL FUNCTIONS
# --------------------------------------------------- #
def _clartBuf(uart=uart0):
''' Clear Buffer
'''
while uart.any():
print(uart.read(1))
# --------------------------------------------------- #
# FUNCTIONS
# --------------------------------------------------- #
def send_data_bt(sdata:str):
''' Send string data using Bluetooth
'''
_clartBuf()
uart0.write(sdata)
def recv_data_bt():
''' Receive string data using Bluetooth
'''
_clartBuf()
prvMills = utime.ticks_ms()
received_data = ''
while (utime.ticks_ms()-prvMills)<2000:
if uart0.any():
single_data = uart0.read(1)
received_data += single_data.decode('utf-8')
return received_data