Skip to content

Instantly share code, notes, and snippets.

@sammachin
Last active April 22, 2024 14:45
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sammachin/b67cc4f395265bccd9b2da5972663e6d to your computer and use it in GitHub Desktop.
Save sammachin/b67cc4f395265bccd9b2da5972663e6d to your computer and use it in GitHub Desktop.
Raspberry Pi PicoW MQTT-GPIO With Node-RED

This will require the umqtt.simple library on the PicoW,

You can install this with upip by running:

import upip
upip.install('umqtt.simple')

If you use a public MQTT broker such as test.mosquitto.org you should change the prefix value in line 8 of the micropython from MYNAME to something unique or you may recieve other peoples commands

Also you will need to update the topics in the flow from MYNAME

You will need to enter the WiFi Details for your network in like 12 of the micropython, change YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD to appropriate values

[{"id":"a1278c3bb2492ac8","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"072eb8be8fa5b953","type":"mqtt out","z":"a1278c3bb2492ac8","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"f59f047b7402cb1e","x":710,"y":120,"wires":[]},{"id":"436051cad470a16e","type":"inject","z":"a1278c3bb2492ac8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"MYNAME/GPIO/15","payload":"1","payloadType":"num","x":180,"y":140,"wires":[["072eb8be8fa5b953"]]},{"id":"91d674ba9493c9cb","type":"mqtt in","z":"a1278c3bb2492ac8","name":"","topic":"MYNAME/pico","qos":"2","datatype":"auto","broker":"f59f047b7402cb1e","nl":false,"rap":true,"rh":0,"inputs":0,"x":120,"y":300,"wires":[["330a1ccd80e08cdc"]]},{"id":"330a1ccd80e08cdc","type":"debug","z":"a1278c3bb2492ac8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":330,"y":300,"wires":[]},{"id":"92c6361a251d3684","type":"inject","z":"a1278c3bb2492ac8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"MYNAME/GPIO/15","payload":"0","payloadType":"num","x":180,"y":180,"wires":[["072eb8be8fa5b953"]]},{"id":"c3f373ccb42e5d2c","type":"inject","z":"a1278c3bb2492ac8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"MYNAME/GPIO/20","payload":"1","payloadType":"num","x":180,"y":60,"wires":[["072eb8be8fa5b953"]]},{"id":"b5a434643a3e22fb","type":"inject","z":"a1278c3bb2492ac8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"MYNAME/GPIO/20","payload":"0","payloadType":"num","x":180,"y":100,"wires":[["072eb8be8fa5b953"]]},{"id":"f59f047b7402cb1e","type":"mqtt-broker","name":"","broker":"test.mosquitto.org","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
import network
import time
from machine import Pin
from umqtt.simple import MQTTClient
prefix = "MYNAME/"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('YOUR_WIFI_SSID', 'YOUR_WIFI_PASSWORD')
while not wlan.isconnected() and wlan.status() >= 0:
print("Waiting to connect:")
time.sleep(1)
def callback(topic, msg):
t = topic.decode("utf-8").lstrip(prefix)
print(t)
if t[:5] == 'GPIO/':
p = int(t[5:])
data = int(msg)
led = Pin(p, Pin.OUT)
led.value(data)
client.publish(prefix+'pico', str(p)+"-"+str(data))
def heartbeat(first):
global lastping
if first:
client.ping()
lastping = time.ticks_ms()
if time.ticks_diff(time.ticks_ms(), lastping) >= 300000:
client.ping()
lastping = time.ticks_ms()
return
client = MQTTClient(prefix+"picow", "test.mosquitto.org",user=None, password=None, keepalive=300, ssl=False, ssl_params={})
client.connect()
heartbeat(True)
client.set_callback(callback)
client.subscribe(prefix+"GPIO/#")
while True:
client.check_msg()
heartbeat(False)
import network
import time
from machine import Pin
from umqtt.simple import MQTTClient
prefix = "MYNAME/"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('YOUR_WIFI_SSID', 'YOUR_WIFI_PASSWORD')
while not wlan.isconnected() and wlan.status() >= 0:
print("Waiting to connect:")
time.sleep(1)
def callback(topic, msg):
t = topic.decode("utf-8").lstrip(prefix)
print(t)
if t[:5] == 'GPIO/':
p = int(t[5:])
data = int(msg)
led = Pin(p, Pin.OUT)
led.value(data)
client.publish(prefix+'pico', str(p)+"-"+str(data))
client = MQTTClient(prefix+"picow", "test.mosquitto.org",user=None, password=None, keepalive=300, ssl=False, ssl_params={})
client.connect()
client.set_callback(callback)
client.subscribe(prefix+"GPIO/#")
while True:
client.wait_msg()
@Richard238
Copy link

Spotted a tiny typo in the read-me, like/line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment