OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) Using MQTT to log data Reply To: Using MQTT to log data

#34439

Ian
Participant

I have taken Dan’s version of OSPi with the notify_change.py plugin and modified it (Sorry – I am not familiar with Git, I tried, but eventually just downloaded a zip file and extracted the files. When I have some more time I will read more about Git!).

I have a working version (call it early alpha!) that uses the notifications Dan introduced to send MQTT messages whenever these events occur. There is a web page that allows you to select which notifications you want published to MQTT, and what the topic will be for each. You can also set the MQTT host and port. There are defaults for all these parameters.

I haven’t developed any Node-Red flows, but I have used an Android app called myMQTT to check the MQTT messages are being sent. They are set with the retain feature of MQTT, so that if you subscribe after the event, you will get the latest message in the topic. They appear almost instantly on my phone.

Please excuse the code (whats wrong with Fortran anyway!) and feel free to make any suggestions. I am very much a Python novice, and struggled with some aspects – getting numerous errors. You will note some routines duplicated in the modules mqtt.py and notify_change.py – it was easiest that way and I am sure someone can let me know how to avoid it! The module mqtt.py is mostly about getting the parameters input by the user, whereas notify_change.py uses the stubs Dan created to process events.

This needs a lot of thought as to what data is to be sent for each event. I am quite sure I have got it wrong in many cases, and will be spending more time looking at that aspect. The message that is sent can be a string (“Someone logged in”, or a python object eg {“snames”:[“s1”.”s2″,”s3″,”s4″,”s5″,”s6″,”s7″,”s8″]}.  The topic should indicate which event the message is in response to.

mqtt.py and notify_change.py must go in the plugins directory (and be set executable). mqtt.html goes in the templates directory.

Node-red can be used to process these messages, if necessary reformatting them and sending them on to other applications such as email, storing in a database, and so on. The example I posted earlier provides examples that would be a good starting point.