Control LED on PicoW using Node-Red and HiveMQ (broker)

 To switch the onboard LED of a Raspberry Pi Pico W using Node-RED over the internet, the most robust method is using the MQTT protocol. This involves a "Broker" (a middleman in the cloud) that allows your Node-RED dashboard to talk to the Pico W regardless of where they are physically located.

1. The "Middleman": Set up a Cloud MQTT Broker

Since you want to control it via the internet, you need a public broker.

  • HiveMQ Cloud or EMQX Cloud are great free options for small projects.

  • Sign up and create a "Cluster."

  • Create a Username and Password in the "Access Management" section.

  • Note down your Broker URL (e.g., xxxxxx.s1.eu.hivemq.cloud).


2. The Pico W: MicroPython Code

You need the umqtt.simple library. In Thonny, go to Tools > Manage Packages and search for umqtt.simple to install it.

Save the following as main.py on your Pico W:

Python
import network
import time
from machine import Pin
from umqtt.simple import MQTTClient

# 1. WiFi Credentials
SSID = "Your_WiFi_Name"
PASSWORD = "Your_WiFi_Password"

# 2. MQTT Settings
MQTT_BROKER = "your-broker-url.com" # From Step 1
CLIENT_ID = "PicoW_LED_Controller"
TOPIC = "picow/led"
USER = "your_username"
PWD = "your_password"

# Setup Onboard LED
# Note: On Pico W, the LED is on the wireless chip, referenced as "LED"
led = Pin("LED", Pin.OUT)

def connect_wifi():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(SSID, PASSWORD)
    while not wlan.isconnected():
        print("Connecting to WiFi...")
        time.sleep(1)
    print("Connected!", wlan.ifconfig())

def on_message(topic, msg):
    print(f"Received: {msg}")
    if msg == b"1":
        led.on()
    elif msg == b"0":
        led.off()

connect_wifi()

# Connect to MQTT
client = MQTTClient(CLIENT_ID, MQTT_BROKER, user=USER, password=PWD, port=8883, ssl=True)
client.set_callback(on_message)
client.connect()
client.subscribe(TOPIC)
print(f"Subscribed to {TOPIC}")

while True:
    client.check_msg() # Check for new messages from Node-RED
    time.sleep(0.1)

3. Node-RED: Create the Internet Interface

You will need the node-red-dashboard palette installed for the UI.

  1. MQTT Out Node:

    • Server: Enter your Cloud Broker URL and port (usually 8883 for SSL).

    • Security: Enter your MQTT username and password.

    • Topic: picow/led

  2. Dashboard Switch Node:

    • Set On Payload to string 1.

    • Set Off Payload to string 0.

    • Connect the Switch node to the MQTT Out node.

  3. Deploy: Click the "Deploy" button.

How it Works

When you toggle the switch on your Node-RED dashboard (accessible via http://your-ip:1880/ui), it sends a "1" or "0" to the cloud broker. Your Pico W, which is constantly "listening" to that same topic over the internet, receives the message and triggers led.on() or led.off().

Would you like me to help you set up the specific SSL/TLS settings in Node-RED to ensure the connection to the cloud broker is secure?


Control LED using MQTT and Node-RED This video provides a complete walkthrough of connecting the Pico W to an MQTT broker and building a Node-RED interface to control the LED.

Comments

Popular posts from this blog

HackRF with srsLTE and openlte

Blinking a LED in STM32F103C8T6 (Blue Pill) Board

Read Continuously from Serial Port in QT