diff --git a/.gitignore b/.gitignore index f2d2050..03af2b2 100644 --- a/.gitignore +++ b/.gitignore @@ -135,3 +135,6 @@ log.txt uploads/ readings/ rain.txt + +# Pycharm idea +.idea/ diff --git a/documentation/destinations/mqtt.md b/documentation/destinations/mqtt.md index 8484097..ad470f6 100644 --- a/documentation/destinations/mqtt.md +++ b/documentation/destinations/mqtt.md @@ -14,6 +14,14 @@ We recommend [Mosquitto](https://mosquitto.org/) which is an open source and lig Steve's Internet Guide has instructions for installing Mosquitto on both [Windows](http://www.steves-internet-guide.com/install-mosquitto-broker/) and [Linux](http://www.steves-internet-guide.com/install-mosquitto-linux/) which are worth checking out. +## Support for local SSL with MQTT Broker + +1 > Upload the certificate file to your pico for example a file called ca.crt + +2 > Update the config.py file and add the line `mqtt_broker_ca_file = 'ca.crt'` replacing ca.crt with the path to the file + +The mqtt.py destination file will attempt top use SSL if the `mqtt_broker_ca_file` is not none. + ## Using a cloud hosted MQTT broker Alternatively you can avoid any software setup and having to manage your own server by using one of the many hosted MQTT broker services. These generally come with a small monthly fee (£3-5) or have very limited capabilities. diff --git a/enviro/config_template.py b/enviro/config_template.py index 92d36d3..9556813 100644 --- a/enviro/config_template.py +++ b/enviro/config_template.py @@ -30,6 +30,8 @@ mqtt_broker_address = None mqtt_broker_username = None mqtt_broker_password = None +# mqtt broker if using local SSL +mqtt_broker_ca_file = None # adafruit ui settings adafruit_io_username = None diff --git a/enviro/destinations/mqtt.py b/enviro/destinations/mqtt.py index e6d26cf..414fa7b 100644 --- a/enviro/destinations/mqtt.py +++ b/enviro/destinations/mqtt.py @@ -12,15 +12,48 @@ def upload_reading(reading): username = config.mqtt_broker_username password = config.mqtt_broker_password nickname = reading["nickname"] - + + # check if ca file paramter is set, if not set it to not use SSL by setting to None + try: + config.mqtt_broker_ca_file + except AttributeError: + config.mqtt_broker_ca_file = None + try: - # attempt to publish reading - mqtt_client = MQTTClient(reading["uid"], server, user=username, password=password, keepalive=60) + if config.mqtt_broker_ca_file: + # Using SSL + f = open("ca.crt") + ssl_data = f.read() + f.close() + mqtt_client = MQTTClient(reading["uid"], server, user=username, password=password, keepalive=60, + ssl=True, ssl_params={'cert': ssl_data}) + else: + # Not using SSL + mqtt_client = MQTTClient(reading["uid"], server, user=username, password=password, keepalive=60) + # Now continue with connection and upload mqtt_client.connect() mqtt_client.publish(f"enviro/{nickname}", ujson.dumps(reading), retain=True) mqtt_client.disconnect() return UPLOAD_SUCCESS - except: - logging.debug(f" - an exception occurred when uploading") + + # Try disconneting to see if it prevents hangs on this typew of errors recevied so far + except (OSError, IndexError) as exc: + try: + import sys, io + buf = io.StringIO() + sys.print_exception(exc, buf) + logging.debug(f" - an exception occurred when uploading.", buf.getvalue()) + mqtt_client.disconnect() + except Exception as exc: + import sys, io + buf = io.StringIO() + sys.print_exception(exc, buf) + logging.debug(f" - an exception occurred when disconnecting mqtt client.", buf.getvalue()) + + except Exception as exc: + import sys, io + buf = io.StringIO() + sys.print_exception(exc, buf) + logging.debug(f" - an exception occurred when uploading.", buf.getvalue()) return UPLOAD_FAILED