Cannot establish MQTT SSL connection
-
Hi everyone, this is my first attempt to publish with MQTT & SSL on test.mosquitto.org. 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: https://test.mosquitto.org/ssl/ . I have uploaded the mqtt.py 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 unenqrypted.py
>>>
>>>
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/mqtt.py", 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): print(msg) ctr = 0 ssid = "TOTOLINK 2.4G (1)" ip = "192.168.2.100" g_ip = "192.168.2.1" mask = "255.255.255.0" password = "XXXXXXXXXXXXXXXX" device_id = "mydevice-AF4Wd24F2S" mqtt_url = "test.mosquitto.org" 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(): machine.idle() print("Connecting to MQTT server...") client = MQTTClient( device_id, mqtt_url, port=mqtt_port, keepalive=60, ssl=True, ssl_params=ssl_params ) client.set_callback(sub_cb) status = client.connect() if status == 0: print("Success\n") else: print("Failure\n") while True: print("Sun is rising...") client.publish(topic="manos/status/sun", msg="RISE") time.sleep(1) print("Sun is setting...") client.publish(topic="manos/status/sun", msg="SET") client.check_msg() time.sleep(1)
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'....