ctrl_buf also needs to be global in _irq (i.e. use the global keyword lile you did in the other function).
ubluetooth - async ? 'BLE' object isn't iterable
Re: ubluetooth - async ? 'BLE' object isn't iterable
Re: ubluetooth - async ? 'BLE' object isn't iterable
but still ctrl_buf is empty...
Code: Select all
ctrl_buf = bytearray()
class BLEInit:
def __init__(self, ble, name='tests', rxbuf=100):
self._ble = ble
self._ble.active(True)
self._ble.irq(handler=self._irq)
...
def _irq(self, event, data):
global ctrl_buf
if event == _IRQ_GATTS_WRITE:
conn_handle, attr_handle, = data
ctrl_buf = "test".encode()
def ble_listen():
ble = bluetooth.BLE()
ble_conn = BLEInit(ble)
async def read_ble_buffer():
while True:
global ctrl_buf
if (len(ctrl_buf) > 0):
print("new data!")
await uasyncio.sleep_ms(50)
ble = ble_listen()
loop = uasyncio.get_event_loop()
ble_buffer_service = read_ble_buffer()
loop.create_task(ble_buffer_service)
loop.run_forever()
Re: ubluetooth - async ? 'BLE' object isn't iterable
Are you advertising and connectable? Can you try adding a print to the _irq function?
Re: ubluetooth - async ? 'BLE' object isn't iterable
Yes im connectable to bluetooth.
Code: Select all
def _irq(self, event, data):
global ctrl_buf
if event == _IRQ_CENTRAL_CONNECT:
conn_handle, _, _, = data
self._connections.add(conn_handle)
print("Connect client: "+str(conn_handle))
elif event == _IRQ_CENTRAL_DISCONNECT:
conn_handle, _, _, = data
self._connections.remove(conn_handle)
print("Client disconnect")
self._advertise()
elif event == _IRQ_GATTS_WRITE:
conn_handle, attr_handle, = data
print("Client writting...: "+str(attr_handle))
ctrl_buf = "test".encode()
>>>
Client writting...: 20
Client writting...: 20
Re: ubluetooth - async ? 'BLE' object isn't iterable
Should this work? unfortunately it doesn't send ctrl_buf to the global variable.
I apologize for my intrusive questions.
I apologize for my intrusive questions.
Re: ubluetooth - async ? 'BLE' object isn't iterable
It looks like it should but there must be something else going wrong.
Can you add a print to the read_ble_buffer() loop to see what it thinks ctrl_buf is? Also to confirm that that loop is definitely running.
Code: Select all
async def read_ble_buffer():
while True:
global ctrl_buf
print(ctrl_buf)
if (len(ctrl_buf) > 0):
print("new data!")
await uasyncio.sleep_ms(50)
Re: ubluetooth - async ? 'BLE' object isn't iterable
Code: Select all
Connect client: 0
bytearray(b'')
bytearray(b'')
bytearray(b'')
bytearray(b'')
Client writting...: 20
Client writting...: 20
Client writting...: 20
bytearray(b'')
bytearray(b'')
bytearray(b'')
Re: ubluetooth - async ? 'BLE' object isn't iterable
Interestingly, it sees new data, but only after 2 running the script.
Re: ubluetooth - async ? 'BLE' object isn't iterable
Ok, I succeeded.
import ble
and refer to: ble.ctrl_buf
jimmo - Thank you very much for your help and ideas.
import ble
and refer to: ble.ctrl_buf
jimmo - Thank you very much for your help and ideas.
Re: ubluetooth - async ? 'BLE' object isn't iterable
I have one more problem. In our solution:
After clearing ctrl_buf - sometimes it happens that some data from irq - gatts_read is not provided. Can you use some sort of queue or synchronization here?
I send about 10-20 queries per second as bluetooth "write"
Code: Select all
async def read_ble_buffer ():
global ctrl_last_buf
while True:
if (len (ble.ctrl_buf)> 0):
print (ble.ctrl_buf)
await send_data(uart, remote_device, ble.ctrl_buf)
ble.ctrl_buf = ""
await uasyncio.sleep_ms (50)
I send about 10-20 queries per second as bluetooth "write"