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
wlan = network.WLAN(network.STA_IF)
while not wlan.isconnected() and wlan.status() >= 0:
print('Connected to Wifi!')
mqtt_server = 'broker.hivemq.com'
client_id = 'board1'
topic_pub_temp = b'test'
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
# Temperatur lesen
temperature_value = sensor_temp.read_u16() * conversion_factor
temperature_Celcius = 27 - (temperature_value - 0.706)/0.00172169/ 8
client = MQTTClient(client_id, mqtt_server, keepalive=3600)
except Exception as es:
print('Connected to %s MQTT Broker'%(mqtt_server))
print('Failed to connect to the MQTT Broker. Reconnecting...')
client = mqtt_connect()
except OSError as e:
There is no connection to the MQTT-Server - why not?
Can you please help me?
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 (?)
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
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 ...
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:
(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