Home Automation Spaghetti

Lightweight message brokers like mosquitto, that makes use of the MQTT are of great use in the world of home automation and the Internet-of-Things. With MQTT, functionality like interfacing with specific hardware, interfacing with webservices, reading/writing databases can be dived into separate programs/scripts instead of putting everything together in a complex monolithic application. This provides flexibility, and a means to extend existing functionality by listening in on existing MQTT topics, that already being published.

What I like about a lightweight message bus is illustrated by the following example which I’m actually using at the moment. One program (a perl script) is connected to the OVMS webservice, where it listens to updates on the status of my electric vehicle, a Twizy, and publishes this data   (location, state of charge, speed, …) in the form of a JSON encoded object via MQTT. Another program, which is driving a LED matrix display (located in the kitchen), is subscribed to these MQTT messages, and uses them to display the actual charging state on the display.

 

UntitledWith this in place, It is possible to add a third program that writes all data to file, a fourth program that switches a wall outlet when the state-of-charge has reached 100%, a fifth program that plays an audible message when the Twizy is arriving home, a sixth .. etc. etc. While this setup is great, and is very, very flexible, the relation and dependencies between publishers and subscribers if written in program code, and therefore difficult to understand.

And that’s where Node-RED come into the picture. Node-RED is visual tool that displays the process flow between (the spaghetti) between a number of building blocks. With Node-RED it is possible to visually design a flow that, in the context of the previous example, trigger on messages from the Twizy, transform these in to readable text, and publish them to a LED matrix output. It is still spaghetti, but now it’s visible. A sample flow that I built recently is that where updates from the Twizy (received through MQTT, which is the block on the left), are routed based on changes in distance or charging state (blocks DISTANCE and STATE), transformed into a set of different messages, and then publised to a Text-to-speed engine (SONOS), or mobile phone (with Notify My Android).

twizyfbp