Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
MohammadReza Sharifi
Published © MIT

How to Connect RPi Pico W to Node-RED and Home Assistant

In this tutorial, we will learn how to connect Raspberry Pi Pico W microcontroller to Home Assistant and Node-RED.

IntermediateFull instructions provided2 hours504
How to Connect RPi Pico W to Node-RED and Home Assistant

Things used in this project

Story

Read more

Schematics

PCB Board in altium designer

circuit diagram

Code

micropython script on raspberry pi pico w

MicroPython
This code sends data from Raspberry Pi Pico W to Node-RED using http protocol
import network
import urequests
import ujson
from machine import Pin, ADC
import dht
from time import sleep

dht_sensor = dht.DHT11(Pin(15))
analog_value = ADC(Pin(26))

# Connect to Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('Mrsh77', '1m77n2299215r77#')

# Wait for connection
while not wlan.isconnected():
    pass

def air_params():
    #sleep(2)
    dht_sensor.measure()
    temp = dht_sensor.temperature()
    hum = dht_sensor.humidity()
    CO_value = analog_value.read_u16()
    
    return temp, hum, CO_value


# Define the URL and the message
url = 'http://192.168.1.232:1880/endpoint/data'

while True:
   try: 
       temperature, humidity, co = air_params()
       #print(temperature, humidity, co)
       sensor_data = {
           "temperature" : temperature,
           "humudity" : humidity,
           "co" : co
           }
       
       json_data = ujson.dumps(sensor_data)
       
       response = urequests.post(url, data=json_data)
       #print(response.text)
       sleep(2)
       
   except Exception as e:
       print("there is a problem: ",e) 
       sleep(2) 
        
       
       
   

Node-RED block diagram

JSON
[{"id":"2341359dfcc41a1b","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"8f82d852e91ea3d1","type":"http in","z":"2341359dfcc41a1b","name":"","url":"/data","method":"post","upload":false,"swaggerDoc":"","x":110,"y":100,"wires":[["8e8b2445cc7d4bff","67350e2017c13a95"]]},{"id":"2a1fb16360660daf","type":"debug","z":"2341359dfcc41a1b","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":620,"y":60,"wires":[]},{"id":"67350e2017c13a95","type":"function","z":"2341359dfcc41a1b","name":"function 1","func":"// The 'msg.payload' contains the JSON string\nvar data = JSON.parse(msg.payload);\n\n// Convert temperature and humidity to integers\nvar temperature = parseInt(data.temperature);\nvar humidity = parseInt(data.humudity); // Note: There's a typo in 'humidity' here\nvar co = parseInt(data.co);\n// Update the 'msg.payload' with the integer values\nmsg.payload = {\n    temperature: temperature,\n    humidity: humidity,\n    co :co\n};\n\n// Return the message to be passed to the next node\nreturn msg;\n","outputs":1,"timeout":"0","noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":80,"wires":[["445d3f92310ca1bb"]]},{"id":"445d3f92310ca1bb","type":"function","z":"2341359dfcc41a1b","name":"function 2","func":"// Create three separate messages\nvar temperatureMsg = { payload: msg.payload.temperature };\nvar humidityMsg = { payload: msg.payload.humidity };\nvar coMsg = {payload: msg.payload.co}\n// Return an array of messages to be sent to the three output ports\nreturn [temperatureMsg, humidityMsg, coMsg];\n","outputs":3,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":80,"wires":[["2a1fb16360660daf","8e3a85d4cac4d091"],["80f3c315337ef632","85fc4f093b201f90"],["340b7869b238a389","a367690bf0a2e4bb"]]},{"id":"80f3c315337ef632","type":"debug","z":"2341359dfcc41a1b","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":180,"wires":[]},{"id":"340b7869b238a389","type":"debug","z":"2341359dfcc41a1b","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":300,"wires":[]},{"id":"8e3a85d4cac4d091","type":"ui_gauge","z":"2341359dfcc41a1b","name":"","group":"4b1179aae9a5f91f","order":1,"width":0,"height":0,"gtype":"compass","title":"Temp","label":"'C","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","diff":false,"className":"","x":630,"y":100,"wires":[]},{"id":"85fc4f093b201f90","type":"ui_gauge","z":"2341359dfcc41a1b","name":"","group":"4b1179aae9a5f91f","order":2,"width":0,"height":0,"gtype":"gage","title":"Humidity","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","diff":false,"className":"","x":660,"y":220,"wires":[]},{"id":"a367690bf0a2e4bb","type":"ui_gauge","z":"2341359dfcc41a1b","name":"","group":"4b1179aae9a5f91f","order":2,"width":0,"height":0,"gtype":"wave","title":"CO","label":"PPM","format":"{{value}}","min":0,"max":"1000000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","diff":false,"className":"","x":610,"y":360,"wires":[]},{"id":"8e8b2445cc7d4bff","type":"http response","z":"2341359dfcc41a1b","name":"","statusCode":"","headers":{},"x":270,"y":140,"wires":[]},{"id":"4b1179aae9a5f91f","type":"ui_group","name":"Air Params","tab":"4c2a2d8b4284e446","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"4c2a2d8b4284e446","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Credits

MohammadReza Sharifi

MohammadReza Sharifi

13 projects • 7 followers
I'm an Electrical Engineer and Maker.

Comments