Getting Started with MQTT on Raspberry Pi Pico W - Connect to the Internet of Things!

Hi @Dale280804, Welcome to the forums!!!

Having a look at the Pimoroni have an example with MQTT for this board.

It looks like it should be simple enough to get it all working alongside this tutorial.

Hi Aaron,

Thank you for the welcome and pointing me in the right direction, however i am not sure what i need to put in for the MQTT broker details? (please see attached pic)
I have installed mqtt on the pico and when i run the script it shows a message on the display “saying posting posting enviro+ data via mqtt wait a minute”

Any help appreciated on what details I need to add for mqtt broker settings.

(attachments)

Hi @Dale280804,

The code below is from the tutorial that shows where the Adafruit IO details need to go.
You should be able to translate where they go in the Pimoroni code easily enough.

# Fill in your Adafruit IO Authentication and Feed MQTT Topic details
mqtt_host = "io.adafruit.com"
mqtt_username = ""  # Your Adafruit IO username
mqtt_password = ""  # Adafruit IO Key
mqtt_publish_topic = ""  # The MQTT topic for your Adafruit IO Feed

# Enter a random ID for this MQTT Client
# It needs to be globally unique across all of Adafruit IO.
mqtt_client_id = "somethingreallyrandomandunique123"

# Initialize our MQTTClient and connect to the MQTT server
mqtt_client = MQTTClient(
        client_id=mqtt_client_id,
        server=mqtt_host,
        user=mqtt_username,
        password=mqtt_password)

Thanks Aaron,

I managed to get it working and connected and then something went wrong with my script on thonny and it throws up an error when trying to run with the below codes

MPY: soft reboot
5
5
5
5
5
[Errno 12] ENOMEM
[Errno 12] ENOMEM
[Errno 12] ENOMEM

Hi @Dale280804,

That is likely a running out of memory error.

There’s a lot of information out there on this issue.
From threads like this, adding the Garbage Collector library may work as a bandaid fix and help find where in your code the memory is getting tied up so you can try and optimise it.

1 Like

That’s great, thanks Aaron I’ll take a look.

1 Like

Let us know how you go!

I’ve just ran though all this with an old DHT11, attempting to replace my expensive rPI’s around here with the Pico W’s so that I can repurpose the Pi’s for bigger and better things than monitoring my reptile enclosures… it all looks good, but umqtt.simple gives me an error attempting to publish. [Errno 9] EBADF. (Bad File).

I’m running Thonny 4.1.6, and using Pico Firmware v1.23.0, micropython-umqtt.simple v1.3.4.

Hey @Michael67832,

I can think of a couple of things to check initially:

  1. Make sure the socket is correctly initialised
  2. Double check all of your info in the publish script (MQTT uname and password, wifi details, etc.)
  3. File paths, a bad file error could come from a slightly different file path than the guide for whatever reason.
  4. Make sure everything has correct admin privilege to access network resources

I would also suggest posting snippets of your code, just to have some extra eyes look over it.

Hope this helps!