Cannot establish MQTT SSL connection

  • Hi everyone, this is my first attempt to publish with MQTT & SSL on I have zero experience in python. I do have some experience in Ruby and in Ruby on Rails. I tried non-ssl and it worked fine.
    Please have a look at the code. My goal is to use all three keys in the ssl_params dictionary. However i currently need only two for connecting to the mosquitto server. I generated the certificate file & key at the mosquitto website see here: . I have uploaded the file in /flash/cert dir via ftp. I have tried both 8883 & 8884 port. No luck.
    After executing the code in atom editor i get the following error:

    >>> Running mqtt
    Connecting to WiFi network 'TOTOLINK 2.4G (1)'
    Connecting to MQTT server...
    Traceback (most recent call last):
    File "<stdin>", line 50, in <module>
    File "/flash/lib/", line 69, in connect
    ValueError: invalid argument(s) value
    Pycom MicroPython 1.20.2.rc6 [v1.11-01f49f7] on 2020-02-28; FiPy with ESP32
    Pybytes Version: 1.3.1
    Type "help()" for more information.

    from mqtt import MQTTClient
    from network import WLAN
    import machine
    import time
    import ussl
    def sub_cb(topic, msg):
    ctr = 0
    ssid = "TOTOLINK 2.4G (1)"
    ip = ""
    g_ip = ""
    mask = ""
    password = "XXXXXXXXXXXXXXXX"
    device_id = "mydevice-AF4Wd24F2S"
    mqtt_url = "" 
    mqtt_user = ""
    mqtt_pass = ""
    mqtt_port = 8884
    #ssl_params = {'cert_reqs': ssl.CERT_REQUIRED, 'ca_certs': 'pycom/ca.pem', 'keyfile': '/flash/cert/client.pem', 'certfile': '/flash/cert/client.pem'}
    ssl_params = {'cert_reqs': ussl.CERT_REQUIRED, 'keyfile': '/flash/cert/client.key', 'certfile': '/flash/cert/client.pem'}
    wlan = WLAN(mode=WLAN.STA)
    # go for fixed IP settings (IP, Subnet, Gateway, DNS)
    wlan.ifconfig(config=(ip, mask, g_ip, g_ip))
    wlan.scan()     # scan for available networks
    wlan.connect(ssid=ssid, auth=(WLAN.WPA2, password))
    print("Connecting to WiFi network '" + ssid + "'")
    while not wlan.isconnected():
    print("Connecting to MQTT server...")
    client = MQTTClient(
    status = client.connect()
    if status == 0:
    while True:
        print("Sun is rising...")
        client.publish(topic="manos/status/sun", msg="RISE")
        print("Sun is setting...")
        client.publish(topic="manos/status/sun", msg="SET")

    Unfortunately the docs have only got a non-enqrypted connection setup.
    I just don't get it... Why don't they have more code examples?
    Can anybody help me get this done?

    Regards to all

  • I'm going to be working on something similar over the next little while, so if you do make progress, I would be interested to read about it.

    In my arduino environment, the 'secure' connection happens when you're starting the wifi... you either start the wifi client with or without 'security'....

Log in to reply

Pycom on Twitter