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 ;-(
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?
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.
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.