# Node RED on Pico - why is it not working?

**URL:** <https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092>\
**Category:** General\
**Created:** [23 January 2023 16:00 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092 "2023-01-23T16:00:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![beg1nner](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@beg1nner](https://discourse.nodered.org/u/beg1nner)\
**Post date:** [23 January 2023 16:00 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/1 "2023-01-23T16:00:44Z")

</div>

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)](https://www.tomshardware.com/how-to/raspberry-pi-pico-w-node-red)

My Code running on the Pico:

```auto
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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 January 2023 16:19 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/2 "2023-01-23T16:19:34Z")

</div>

> [@beg1nner](#):
>
> My Pi should also work as a Node-RED-broker.

What mqtt broker did you install on the Pi - Mosquitto?  
Is the broker running - Can you [publish and subscribe with command line tools](http://www.steves-internet-guide.com/mosquitto_pub-sub-clients/) on the Pi?  
Can you publish and subscribe using Node-red on the Pi?

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/4061c0dd4323c5125e82198e1f53311fba88055f.jpeg)

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

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [23 January 2023 16:53 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/3 "2023-01-23T16:53:39Z")

</div>

> [@beg1nner](#):
>
> `mqtt_server = 'broker.hivemq.com'`

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 January 2023 17:19 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/4 "2023-01-23T17:19:44Z")

</div>

> [@beg1nner](#):
>
> mqtt\_server = '[broker.hivemq.com](http://broker.hivemq.com)'

Doh! ☺

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [23 January 2023 18:23 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/5 "2023-01-23T18:23:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [23 January 2023 23:32 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/6 "2023-01-23T23:32:17Z")

</div>

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

> [@beg1nner](#):
>
> ```auto
> while True:
> client.publish(topic_pub_temp, str(temperature))
> 
> ```

Additionally: The Pico W is a perfect platform to run [Node-RED MCU Edition](https://flows.nodered.org/node/@ralphwetzel/node-red-mcu-plugin); 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 ...

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [23 January 2023 23:58 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/7 "2023-01-23T23:58:53Z")

</div>

> [@beg1nner](#):
>
> At the end I tried to connect the Pico W to the MQTT-server - it failled. I've got no clue, why ;-(

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.

---

<div class="post-metadata">

**Author:** ![beg1nner](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@beg1nner](https://discourse.nodered.org/u/beg1nner)\
**Post date:** [24 January 2023 10:02 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/8 "2023-01-24T10:02:30Z")

</div>

> [@Andrei](#):
>
> 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

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](http://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](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()" 🙂

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 😃  
your Dodo

---

<div class="post-metadata">

**Author:** ![beg1nner](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@beg1nner](https://discourse.nodered.org/u/beg1nner)\
**Post date:** [28 February 2023 16:46 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/9 "2023-02-28T16:46:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 April 2023 16:47 UTC](https://discourse.nodered.org/t/node-red-on-pico-why-is-it-not-working/74092/10 "2023-04-29T16:47:27Z")

</div>

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