Pico w mqtt_as to HiveMQ Cloud

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
karl60
Posts: 2
Joined: Thu Aug 18, 2022 7:40 pm

Pico w mqtt_as to HiveMQ Cloud

Post by karl60 » Thu Aug 18, 2022 8:02 pm

Newbie trying to understand MQTT

Situation: With a HiveMQ Cloud broker I can simultaneously pub/sub using:
  • * Mosquitto command line
    * Standard Python (on laptop) using HiveMQ code examples
    * Windows MQTT Explorer app
So I'm happy the HiveMQ Broker side of things is working OK.

But I can't get a Pico W with MQTT_AS connected to the HiveMQ Broker

I've added the following to the MQTT_AS config.py file and obviously populated secrets.py accordingly:

Code: Select all

config['server'] = secrets['hivemq_broker']
config['port'] = secrets['port']
config['user'] = secrets['hivemq_username']
config['password'] = secrets['hivemq_key']
With port set to 8883 I get the following error (I'm using the mqtt_as code example range.py as a basis):

Code: Select all

Checking WiFi integrity.
Got reliable connection
Connecting to broker.
Connection failed. Error: (-1, 'Connection closed by host')

I would appreciate a push in the right direction to solving this problem.
Thanks

danjperron
Posts: 51
Joined: Thu Dec 27, 2018 11:38 pm
Location: Québec, Canada

Re: Pico w mqtt_as to HiveMQ Cloud

Post by danjperron » Mon Aug 22, 2022 2:23 pm

So I'm happy the HiveMQ Broker side of things is working OK.

But I can't get a Pico W with MQTT_AS connected to the HiveMQ Broker
Maybe print the config just in case something is not ok.

What is your source code ? Giving the config won't tell us if your code is OK!
Do you have the micropython reference code url?

If you have a Raspberry Pi or a linux server, try a local mosquitto broker first. Then when it is working you could try the internet broker.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Pico w mqtt_as to HiveMQ Cloud

Post by pythoncoder » Mon Aug 22, 2022 4:51 pm

If you're using TLS (as implied by 8883) you need to set config['ssl'] = True. Please study the various tls examples.

The suggestion of developing code with a local mosquitto broker is a good one. Getting TLS working can be tricky and it's good to know that your code is solid before you attempt it.
Peter Hinch
Index to my micropython libraries.

fishjoe
Posts: 7
Joined: Wed Aug 24, 2022 9:25 am
Location: Chrischurch

Re: Pico w mqtt_as to HiveMQ Cloud

Post by fishjoe » Thu Sep 15, 2022 10:12 am

Im having the same problem so I wish to get some help.

Just few questions:
How should I get ssl work? Do i have to have any certificate file? Why do I not need when I using other client app.(they only need to "tick" on option "using SSL"). If so, where should I get whatever certificate file and where do I need to place it onto the device? How do I put it onto device, as it does not connect as flash drive?

Many thanks.

fishjoe
Posts: 7
Joined: Wed Aug 24, 2022 9:25 am
Location: Chrischurch

Re: Pico w mqtt_as to HiveMQ Cloud

Post by fishjoe » Sat Oct 08, 2022 9:46 am

worked out solution.

Code: Select all

client = MQTTClient(secrets.client_id, secrets.mqtt_server, port=secrets.mqtt_port, user=secrets.mqtt_username, password=secrets.mqtt_psd, ssl=True, ssl_params={
        "server_hostname": secrets.mqtt_server})
especially:

Code: Select all

ssl=True, ssl_params={
        "server_hostname": secrets.mqtt_server}

Post Reply