I want to reliably, repeatedly and extendably send a list of length 15 containing numbers 0-128 from a PC using Windows 10 64Bit to an Adafruit ItsyBitsy nRF52840 (circuitpython). A response will be sent from receiver to sender so that I can be sure the correct data was sent. I want to avoid using any time.sleep() or asyncio.sleep() delays in my code. My current code is as follows:
Sender side code:
async def run(write_value):
# Scan for devices
mac_addr = "XX:XX:XX:XX:XX"
tx_charac = "X"
rx_charac = "X"
#devices = await discover()
client = BleakClient(mac_addr, timeout=30)
await client.connect()
await client.write_gatt_char(tx_charac, bytearray(write_value))
await asyncio.sleep(5)
answer = await client.read_gatt_char(rx_charac)
print(answer)
await client.disconnect()
del client
return answer
answer = asyncio.run(run(x))
Receiver side code
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
ble = BLERadio()
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)
while True:
print("running")
ble.start_advertising(advertisement)
print("waiting to connect to BLE central")
while not ble.connected:
pass
print("connected")
while ble.connected:
s = uart.read()
uart.write(s)
if s:
sequence = [x for x in s]
if len(sequence)>1:
#Do some processing
print(sequence)
del s
del sequence
Unfortunately it seems like the code is working unreliably.
Is there anything I can do to improve the reliability, repeatability and extendebility of the sending and receiving process?
Thank you in advance!
I run into the following issues:
- First of all I always have to introduce a waiting time, in order to receive the right echo.
- Sometimes the device does not want to reconnect anymore after a while returning the error
in connect self._device_info = device.details.adv.bluetooth_address AttributeError: 'NoneType' object has no attribute 'bluetooth_address' "in connect self._device_info = device.details.adv.bluetooth_address AttributeError: 'NoneType' object has no attribute 'bluetooth_address'
- Furthermore later on I'd like to send lists containing more than 15 elements (e.g. 100) and I'm not sure yet how exactly to transmit a large amount of data at once.