Node RED on Pico - why is it not working?

Hi there,

I'd like to know why my project isn't working.
On my Raspberry Pi (4B) I've created a WIFI-accesspoint with RaspAP. My Pi should also work as a Node-RED-broker.
After that I've connected a Raspberry Pi Pico W with the WIFI of the Pi - it perfectly worked.
Then I've tried to run Node-RED on my Pi 4 - it also worked.
At the end I tried to connect the Pico W to the MQTT-server - it failled. I've got no clue, why ;-(

I follwed this tutorial (except the part with the sensor; I have none):
How to Use Raspberry Pi Pico W With Node-RED | Tom's Hardware (tomshardware.com)

My Code running on the Pico:

import network, time, machine, ledled
from secrets import *
from umqtt.simple import MQTTClient

rp2.country("DE")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, PASSWORD)
while not wlan.isconnected() and wlan.status() >= 0:
    ledled.obled(1, 0.5)
    time.sleep(1)
    print('wait ...')
ledled.obled(2, 0.1)
print('Connected to Wifi!')

mqtt_server = 'broker.hivemq.com'
client_id = 'board1'
topic_pub_temp = b'test'

# Temperatur-Sensor
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)

# Temperatur lesen
def temperature():
    temperature_value = sensor_temp.read_u16() * conversion_factor 
    temperature_Celcius = 27 - (temperature_value - 0.706)/0.00172169/ 8
    time.sleep(2)
    return temperature_Celcius

def mqtt_connect():
    client = MQTTClient(client_id, mqtt_server, keepalive=3600)
    try:
        client.connect()
    except Exception as es:
        print(es)
    print('Connected to %s MQTT Broker'%(mqtt_server))
    return client

def reconnect():
   print('Failed to connect to the MQTT Broker. Reconnecting...')
   time.sleep(5)
   machine.reset()

try:
    client = mqtt_connect()
    time.sleep(2)
except OSError as e:
    reconnect()

while True:
    client.publish(topic_pub_temp, str(temperature))
    time.sleep(5.0)

There is no connection to the MQTT-Server - why not?
Can you please help me?
Best regards,
your Dodo

What mqtt broker did you install on the Pi - Mosquitto?
Is the broker running - Can you publish and subscribe with command line tools on the Pi?
Can you publish and subscribe using Node-red on the Pi?

Have you configured it to accept connections from other devices?
This page https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/ describes the alternate ways to set up Mosquitto access.

Try it with an IP instead as I suspect the pico is not dns aware (?)

1 Like

Doh! :smiling_face:

One common problem with connecting to any broker is that the client_id must be unique to that broker.

To eliminate that issue - change it to something a bit random

1 Like

My Python knowledge is a bit rusty (meanwhile), yet shouldn't this be temperature() ... as it's calling the function?

Additionally: The Pico W is a perfect platform to run Node-RED MCU Edition; the link's pointing to the plugin that we've created to integrate this into your Node-RED editor. It simplifies your tasks as you stay fully within Node-RED ...

4 Likes

What tells you that the connection to the broker failed ? What is the error message that you are getting ?

I would suggest changing the topic name to something less generic than "test". You are using a public broker and probably you will receive tons of message if you subscribe to this topic. Better to use a topic structure that is unlikely to be used by someone else.

Edit: Further to the aformentioned correction to apply to the temperature the code looks all right by me. I tend to believe you are facing some ip connectivity issue.

I received kind of "ping" messages. Lots of it.
So what to do next:

  • the client_id should be something like board789734gjklg3 ...
  • I'd go to a less public broker.

To get a private MQTT-broker can I use the Pi4's wlan0? Then'd instead of "broker.hivemq.com" I'd use the IPv4-address shown in the picture below:
https://www.bitblokes.de/wp-content/uploads/2019/08/raspap-interface-dashboard.png
(It is not the real address, it's just an example from the internet)

  • after that, I'll change "temperature" to "temperature()" :slight_smile:

At the moment I have no access to the programms because of holidays. After that I'll test it out.
Thanks to all for help :smiley:
your Dodo

Hi there,

the problem is resolved: the wifi-connection wasn't set up in a correct way. So I've reseted the Pi and installed RaspAp again - now it works perfectly.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.