Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MQTT pver SSL and update to attemp disconnects and extra loggin on mqtt failures #125

Merged
merged 7 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,6 @@ log.txt
uploads/
readings/
rain.txt

# Pycharm idea
.idea/
8 changes: 8 additions & 0 deletions documentation/destinations/mqtt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions enviro/config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 38 additions & 5 deletions enviro/destinations/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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