Hi - I am trying to use the HiveMQ Cloud to start getting familiar with MQTT using a Raspberry Pico W as the client device. I was using your recent blog about using Raspberry Pi Pico / Airlift WiFi as a reference although I am using microPython instead of circuitPython.
I can use the HiveMQ public broker service without any issues but when I try to connect to the HiveMQ cloud I get the error message “MQTT connect failed: extra keyword arguments given” which seems to be related to the ssl_params
Snippet of code:-
from umqtt.simple2 import MQTTClient
It is nice to see your interest in MQTT and HiveMQ, please welcome to our community!
The error extra keyword arguments given refers to a method from your SSL library. Can you see the error in the traceback, something like this?
Traceback (most recent call last):
TypeError: extra keyword arguments given
Did you try running your code without specifying ssl_params? The issue is that SSL parameters that the library supports might differ between implementations:
With ssl=False I get IndexError: bytes index out of range in simple.py, line 97
Was anyone else able to connect to HiveMQ Cloud with this or other MicroPython library?
Can the cause of this exception 5 (connection refused) be seen in the logs on HiveMQ Cloud side?
I have fixed the “TypeError: extra keyword arguments given” issue, however, as per FrankD’s post I am now getting a MQTTException: 5 error. Is there any logging on the HIveMQ side that is available to help troubleshoot??
FYI: The certificate verification within the ussl micro-python module is still being developed and hence some of the confusion.
I have attached some code which can be used to test basic ssl functionality with the “sslparams” - All being well you should get “b’HTTP/1.1 200 OK\r\n’” as a response
import usocket as socket
import ussl as ssl
ca_certificate_path = "/certs/catrust-micropython.der"
print('Loading CA Certificate')
with open(ca_certificate_path, 'rb') as f:
cacert = f.read()
f.close()
print('Obtained CA Certificate')
hostname = 'micropython.org'
port_no = 443
sslparams = {'server_side':False, 'key':None, 'cert':None,'cert_reqs':ssl.CERT_REQUIRED, 'cadata':cacert}
def main(use_stream=True):
sock = socket.socket()
addr = socket.getaddrinfo(hostname, port_no)[0][-1]
sock.connect(addr)
sock = ssl.wrap_socket(sock, **sslparams)
sock.write(b"GET / HTTP/1.0\r\n\r\n")
print(sock.read(17))
sock.close()
main()
Thanks Daria, but unfortunately with this certificate still has the same error…
# Connect to HiveMQ
print('Loading CA Certificate')
with open("/isrgrootx1.pem", 'r') as f:
cert = f.read()
sslparams = {'cert': cert}
print('Loaded certificate: ' + cert[0:55] + '...' + cert[-55:])
print("Connecting to " + secrets["broker"] + " as user " + secrets["mqtt_username"])
client = MQTTClient(client_id="picow",
server=secrets["broker"],
port=secrets["port"],
user=secrets["mqtt_username"],
password=secrets["mqtt_key"],
keepalive=3600,
ssl=True,
ssl_params=sslparams)
client.connect()
print('Connected to %s MQTT Broker'%(mqtt_server))
Result, as you can see with the content of the certificate:
Loading CA Certificate
Loaded certificate: -----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIRAIIQz7D...Q99b21/+jh5Xos1AnX5iItreGCc=
-----END CERTIFICATE-----
Connecting to f250f4960aed4d5da670e2248e84ff30.s1.eu.hivemq.cloud as user picow-user
b' \x02\x00\x05'
Traceback (most recent call last):
File "<stdin>", line 74, in <module>
File "/lib/umqtt/simple.py", line 100, in connect
MQTTException: 5
Having some kind of log or error output in HiveMQ Cloud would really be helpful in investigating these kind of cases…
It takes a lot longer until (± 60", so looks like some configured timeout) another error is thrown:
OSError: (-29312, 'MBEDTLS_ERR_SSL_CONN_EOF')
This last error apparently means: " the underlying TCP socket was closed (returned an EOF - End Of File - result). It looks like the initial TCP connection was established (“handshake in progress…” line), and then the server closed the socket during the handshake for some reason (maybe something with the parameters offered by the client?). "
Thanks Ian, those commands worked, but I tried with all possible combinations (I think) with or without the isrgrootx1.pem and any of the three certificates in hivemq-com-chain.der but still end up with either connection error 5 or OSError: (-9984, 'MBEDTLS_ERR_X509_CERT_VERIFY_FAILED').
Having no logs at all in HiveMQ Cloud to follow connection attempts and see what is going on to better understand connection issues, is in my opinion a major missing function that would prevent many issues here in the forum.
Frank - I’m having the same experiences as you, having tried the same things.
Ian - If it’s working for you, would you mind sharing your complete (sanitised) code as an example we could use, please?
As an aside, I’m able to use the Mosquitto test server in authenticated, encrypted mode without any problems - so it appears to me to be something specific to HiveMQ rather than the UMQTT library or my use of it.
I cannot get the HiveMQ MQTT connection to work as I am getting the “MQTTException: 5”, however, I can make a succession TLS connection to the server. I am trying to debug whether it could be an issue with umqtt.simple or ussl.
It is important that you run the latest nightly microPython build as the certificate checking feature is still under development - I am running “rp2-pico-w-20220906-unstable-v1.19.1-375-ge90b85cc9.uf2”
I believe the “MQTTException: 5”. error is caused by an issue with the ussl / usocket module for 2 reasons:-
I have used extensively the same code in Python to send MQTT CONNECT message and I get CONNECT ACK back from HiveMQ
Using the raw MQTT CONNECT data from test 1 on MicroPython I get the same “MQTTException: 5” error message whereas using the same raw MQTT CONNECT in Python I get the “CONNECT ACK”
Thanks a lot Ian for further investigating this problem!!!
What do you think should be our next move? Do you think you have enough detailed info for a ticket for the ussl/usocket project, or do we need more support/answers from HiveMQ first?
I see that CircuitPython also has progressed and has unstable builds with Wifi support for Pico W, but don’t have time right now to try out…
PS yes, I did add the NTP already without better results