Attempting to use Bluetooth and both cores on the Pico W

So I’ve recently been trying to learn how to use both cores on the Pico W as well as asynchronous coding and I decided to try and come up with a project to help me practice. What I ended trying to do was send commands using my mobile to my pico w which would subsequently do some funky tasks on the Pico like determine the temperature or run some LED’s in an asynchronous way depending on comands sent (I’ll be honest all code I used to run Bluetooth was from https://github.com/makeuseofcode/Raspberry-Pi-Pico-W-WH-Bluetooth-Example-in-MicroPython). The problems started when I wanted to try and run a sequence of LED’s on a separate thread when a request was sent from my phone. The code used is below:

# Import necessary modules
from machine import Pin, ADC
import uasyncio as a
import bluetooth
import time as t
import sys
from ble_simple_peripheral import BLESimplePeripheral
import _thread

# Create a Bluetooth Low Energy (BLE) object
ble = bluetooth.BLE()

# Create an instance of the BLESimplePeripheral class with the BLE object
sp = BLESimplePeripheral(ble)

# Create a Pin object for the onboard LED, configure it as an output
led = Pin("LED", Pin.OUT)
re = Pin(15, Pin.OUT)
gr = Pin(6, Pin.OUT)
bl = Pin(1, Pin.OUT)
temp_sensor = ADC(4)
conversion_factor = 3.3 / 65535
event = False

# Initialize the LED state to 0 (off)
led_state = 0

# Define a callback function to handle received data
def on_rx(data):
    print("Data received: ", data)  # Print the received data
    global led_state  # Access the global variable led_state   
    global event
    if data == b'toggle\r\n':  
        led.value(not led_state)  # Toggle the LED state (on/off)
        led_state = 1 - led_state  # Update the LED state
    if data == b'leds\r\n': 
        event = True              # Update global event to true
    if data == b'ledsoff\r\n':
        event = False              # Update global event to false
    if data == b'temp\r\n':        # if temp request is recieved determine onboard temp
        reading = temp_sensor.read_u16() * conversion_factor
        temperature = 27 - (reading - 0.706)/0.001721
        temperature_data = str(temperature).encode()
        print('data sent: ',  temperature_data)
        sp.send(temperature_data)
    
        

async def green(): #green led async code
    gr.value(1)
    await a.sleep(2)
    gr.value(0)
    await a.sleep(2)

async def blue(): #blue led async code
    for i in range(4):
        bl.value(1)
        await a.sleep(0.5)
        bl.value(0)
        await a.sleep(0.5)

async def red(): #red led async code
    for i in range(10):
        re.value(1)
        await a.sleep(0.2)
        re.value(0)
        await a.sleep(0.2)

async def ma(): #use async to run connected LED's
    
    taskg = a.create_task(green())
    taskb = a.create_task(blue())
    taskr = a.create_task(red())

    await taskg
    await taskr
    await taskb
    


def numba2_thread(): # Run LED's using async on separate thread 
    global event
    while True:
        if event == 'run':
            a.run(ma())
        else:
            _thread.exit()


# Start main infinite loop
while True:
    if sp.is_connected():  # Check if a BLE connection is established
        sp.on_write(on_rx)  # Set the callback function for data reception
    if event == True: #if event is changed to true start new thread
        event="run" 
        _thread.start_new_thread(numba2_thread, ())

So the code seems to run perfectly until I try to turn on the led sequence by sending b’leds\r\n’ to the pico (using an app on my mobile called Serial Bluetooth Terminal which I downloaded on the play store). Once the led sequence has started running its seperate loop I can only send one more request to the pico (either to turn the leds off or do either of the other tasks in the code) after which point the Pico just refuses to recieve further commands. I have been scratching my head over this for ages now and tried loads of different code but can’t seem to figure out what i’m doing wrong. Please Help!

Leave a Comment