MQTT ESP8266 MicroPython IDE

Is it possible to setup MQTT connection ESP8266 with a MicroPython IDE instead of Arduino IDE?

I use umqtt.robust or umqtt.simple library and get MQTTException 5.

Hi Daniel,

Great that you are interested in MQTT!

When you client is trying to connect to the broker and receiving the CONNACK message with response code 5, this means that the client has connected but was not authorised for some reason. Most often it is a wrong username or password.

To get better help here, please share your connection details and the full text of the error message you get.

Thanks,
Dasha from HiveMQ Team

Dasha,

I double checked the MQTT login/passwd. I fear the issue is related to the SSL connection setup with port 8883. I doubt if ssl_params is correct. Also certificates are most likely to be made.

main.py extract
def connect_and_subscribe():
global client_id, mqtt_server, topic_sub
ssl_params = {“server_hostname”: “…eu.hivemq.cloud” }
client = MQTTClient(client_id, mqtt_server, user=hmq_username, password=hmq_passwd, ssl=True, ssl_params=ssl_params)
client.set_callback(sub_cb)
client.connect()
client.subscribe(topic_sub)
print(‘Connected to %s MQTT broker, subscribed to %s topic’ % (mqtt_server, topic_sub))
return client

Execution log
MPY: soft reboot
port=8883
ssl=True
ssl_params={‘server_hostname’: ‘…eu.hivemq.cloud’}
user=…Fc
password=…zub
Traceback (most recent call last):
File “main.py”, line 25, in
File “main.py”, line 14, in connect_and_subscribe
File “umqttsimple.py”, line 107, in connect
MQTTException: 5
MicroPython v1.18 on 2022-01-17; ESP module with ESP8266
Type “help()” for more information.

Thanks for helping me out.
Daniel

Hi Daniel,

Thank you for providing the example!

(1)
When you try to specify ssl=True and omit the ssl_params ( ssl_params={}),

  1. does it result in an error?
  2. is the error code different?

(2)
If/when certificates are required, documentation on how to prepare and uploaded them to the board, can be found here: HiveMQ Cloud (check out ‘LittleFS Filesystem Uploader’ and ’ Upload certificates to the board’). In case these instructions don’t apply to your case let me know.

Regards,
Dasha

Dasha,

(1) When I omit the ssl_params the error remains the same: MQTTException: 5.
(2) I will have a look at the instructions and get back to you with the result.

Daniel

Dasha,

Is it possible to connect to the HiveMQ cloud without ssl? The cluster details indicate Port(TLS)=8883.
If I’m not mistaking connection without ssl results in port=1883.

I had a quick look at the LittleFS and Upload certificates. All instructions should be done on the Arduino IDE. However if I want to continue in MicroPython IDE, these will be flashed once this firmware is installed afterwards.

Daniel

Daniel,

The password variable in your code – is it a string or array of bytes?

By specification, the username is an UTF-8 encoded string. The password is binary data with a maximum of 65535 bytes .

Could you try converting the password string into bytes, for example

passwordBytes = "passwordString".encode()

or

passwordBytes = "passwordString".encode("utf-8")

or

passwordBytes = b'passwordString'

Kind regards,
Dasha from HiveMQ

No success, same error. With and without the ssl_params.

Hi Daniel,

HiveMQ Cloud only supports secure connection (port 8883 for the MQTT; port 8884 for the WebSocket)

There is a PUBLIC broker with insecure connection broker.hivemq.com:1883 , but this is for initial test only, since all the data at this broker is accessible to anybody.

Kind regards,
Dasha from HiveMQ

Hi Daniel,

Did you already try to encode the password string into bytes as in the example above?

Thanks,
Dasha from HiveMQ

Dasha,

Yes I did. None of the 3 possibilities were successful. The same MQTTException = 5.

Finally I got it working with the Arduino IDE and the provided guidelines.

The issue is most likely related to the SSL setup on the MicroPython ESP8266 IDE.

I am still in favor to have a solution on MicroPython IDE iso the Arduino IDE. It is more flexible in terms of code development and deployment.

Please feel free to provide input to get the MicroPython solution working after all. Meanwhile I will focus on the application I want to build iso the connection setup.

Not the best solution for production or comercial applicaion but you can call

WiFiClientSecure espClient;
espClient.setInsecure();

From the Arduino IDE then you dont have to mess with the certs etc, for home use its ok
Maybe you can do something similar from Python

Simon,

Thanks for your point of view. Could be an option for a proof of concept while gaining experience.

Daniel