Hi there,
I have the following piece of code on a pico W, which is successfully sending button press values via bluetooth. The while loop in the send_data_task function cycles and every time a button is pressed, the value is pushed into a heap queue. I'm also reading the voltage of the battery powering the pico in a separate function, and pushing this value into the same queue. The voltage value is also successfully sent, but only when a button is pressed.
The send_data_task loop is set to empty the queue and send the contents a soon as the queue has a length greater than zero, which works just fine for all the button pushes. Unfortunately, the voltage value remains in the queue and is only sent when something a button press occurs. Ideally want it to be sent independently of any other action so that I can have a battery reading which updates periodically even when the rest of the device is doing nothing.
Any thoughts?
Cheers.
I have the following piece of code on a pico W, which is successfully sending button press values via bluetooth. The while loop in the send_data_task function cycles and every time a button is pressed, the value is pushed into a heap queue. I'm also reading the voltage of the battery powering the pico in a separate function, and pushing this value into the same queue. The voltage value is also successfully sent, but only when a button is pressed.
The send_data_task loop is set to empty the queue and send the contents a soon as the queue has a length greater than zero, which works just fine for all the button pushes. Unfortunately, the voltage value remains in the queue and is only sent when something a button press occurs. Ideally want it to be sent independently of any other action so that I can have a battery reading which updates periodically even when the rest of the device is doing nothing.
Any thoughts?
Cheers.
Code:
async def read_voltage(): global queue while True: vsys = machine.ADC(29) machine.Pin(29, machine.Pin.IN) machine.Pin(25, mode=machine.Pin.OUT, pull=machine.Pin.PULL_DOWN).high() voltage = vsys.read_u16() * conversion_factor percentage = 100 * ((voltage - empty_battery) / (full_battery - empty_battery)) if percentage > 100: percentage = 100 heapq.heappush(queue,[int(percentage)]) print(queue, 'read_voltage') await asyncio.sleep(5) async def send_data_task(connection, write_characteristic): """Send data to the central device.""" global queue while True: for button in buttons: if button.value() == 1: heapq.heappush(queue,button.name) if allbuttons == 0 and stays[pad.stay] == 1: stays[pad.stay] = 0 if len(queue) > 0: try: heapq.heapify(queue) print(queue) msg = heapq.heappop(queue) log.append(msg) if len(msg) > 1: write_characteristic.write(msg) # Peripheral writes data here else: msg = str('Battery - ') + str(msg[0]) write_characteristic.write(msg.encode('utf-8')) await asyncio.sleep_ms(100) message = '' except Exception as e: print(f"Error while sending data: {e}") continue elif len(log) == 50: log = [] logcounter = logcounter + 50async def run_peripheral_mode(): """Run the peripheral mode.""" # Set up the Bluetooth service and characteristics ble_service = aioble.Service(BLE_SVC_UUID) # Characteristic for the central to write write_characteristic = aioble.Characteristic( ble_service, _WRITE_CHARACTERISTIC_UUID, read=True, write=True, capture=False ) # Characteristic for the peripheral to write read_characteristic = aioble.Characteristic( ble_service, _READ_CHARACTERISTIC_UUID, read=True, write=True, capture=False ) aioble.register_services(ble_service) print(f"{BLE_NAME} starting to advertise") while True: async with await aioble.advertise( BLE_ADVERTISING_INTERVAL, name=BLE_NAME, services=[BLE_SVC_UUID], appearance=BLE_APPEARANCE) as connection: print(f"{BLE_NAME} connected to {connection.device}") # Create tasks for sending and receiving data tasks = [ asyncio.create_task(send_data_task(connection, read_characteristic)), asyncio.create_task(read_voltage()) ] await asyncio.gather(*tasks) print(f"{IAM} disconnected") breakStatistics: Posted by SonSon — Tue Feb 24, 2026 3:17 pm — Replies 4 — Views 78