Especifications
- Power supply redundancy. Critical functions can run even during multi-days power outages
- All devices (RPi, Critical Nodes, Modem, DVR) can be powered on/off remotely. Current and Voltage are measured and logued.
- 4G backup internet connection. All Nodes and Core change to alternate link on failure.
- Backup of the Core on an external VPS. All Nodes should connect to alternate Broker on failure.
- The two MQTT Brokers are bridged, so clients can be connected to either
Topology
The topology is simple and classic: In the Core is a MQTT Broker, the logic is done in Node-RED and the Nodes are mainly ESP8266 + PS + Whatever is needed (sensors, relays, ADC, Led, Switches, etc)
In the cloud , in a VPS, is the Secondary Node, which has an online MQTT Broker, and a Paused Node-RED. Also is in charge of watch the Main Core, in case of failure a Push Message is sended to the clients
This creates redundancy, but also gives the posibility of connect the house with a 4G link (with no public IP) and still have control from outside (as the Main Core MQTT Broker is bridged the the External Core)
The 4G link is created with a hotspot This SSID is defined in every node has a secondary network. So in the event for losing connection, they will try the secondary. In case of the main internet link is down and even the Main Core is down, the nodes will still be conneted the the External Core by 4G
Power
Here in Argentina, power outages are a common occurrence. Since my system is responsible for the alarm (and thefts are also frequent), I wanted a system capable of running critical functions without relying on external energy.
Additionally, I desired the ability to remotely control the power of different parts of the system.
After several iterations now I have this:
This gives a high level of redundance, including the ability to run critical functions without relying on the mains. The "Power Control" box is a node capable of turning on and off the four buses (3x12V + 1x5V). It also measures voltage and current using an ADS1115. Additionally, the node has connection redundancy (as discussed in the "Nodes" section) and incorporates some level of intelligence (e.g. The modem bus will turn itself on, after 30sec of being off)
Also measuring voltage allows to check if I lost mains, because the Power Supply has a greater voltage than the battery. Dirty, but works.
Main Core
The core is a Rpi3 with Raspbian Lite, Mosquitto as MQTT Broker, and Node-RED. There is most of the intelligence of the house:
- Turning on and off lights, acording the time of the day, sensors, etc
- Turning on and off pumps, acording tank levels / time of the day / presence
- Detection of open doors, gates, etc
- Lighs
- Pool automation
- Alarm, siren, sensors
- Loggin data
- Feeding the cat
- Pushing data to cells
- ... and many more!
Not big fan of Node-RED, it's hard to debug, but very easy to add a new function.
Nodes Hardware
All the Nodes are ESP8266, several versions (ESP01, ESP12...) but lastly I only use Wemos D1. They are small, had USB which is very handy to program, can be powered by 3.3V, or 5V, are easy to replace.
There are several versions of nodes, for example
A light control could be: Wemos D1 + 220V to 5V PS + Rele
A Infrared alarm sensor: ESP01 + 12V to 3.3V PS + IR Sensor
Nodes Firmware
I choose to use NodeMCU LUA to program the nodes. As an interpreted lenguaje allows me a very quick development, and debug (even remotly through some sort of MQTT console).
All nodes shared the 90% of the code and basic functions. e.g.
- On power up looks for the same SSIDs,
- When connect to the MQTT broker they send a message reporting being power up
- They subscribe to its configurable MQTT topic, but also to a broadcast one. Usefull to reset all nodes, to send the keep alive message, or even to update a piece of code on all devices at the same time
- Respon to some messages: reset, keep_alive, repor or set some pin status, upload a file...
- On broker disconnetions they try secondary ones.
- Respond to MQTT keep alive messages with health info
- Implement (configurable) debonce funcions for pins
- Had timers to turn on pins during a configurable time
- On disconnet of the main MQTT change to secondary.
- Had remote console by MQTT.
- Allow to updtate firmware also by MQTT
- A Watch dog will reset the ESP if no keep alive is received in 10 minutes (1 per minute is sended by the Core)
And each Node has it's special file with it's funcions, the other 10% of the code.
Nodes communication
All comunication is by MQTT, every Node do the best effort to keep connected to one of the, two defined brokers.
Also they had at least two different SSID, and will switch to the other on disconnection.
Nodes Autonomy
Not everything sould be on the Core. For example, the pump to fill the water tank. The Core had a Node reporting the level of water in the tank. Which if fall below some level (which depends of few things) it will send to the pump Node the command to turn it on. But the Core has to refresh the command every minute, if not, the bomb will turn it off. If something happends to the core or the network, the pump wont be eternally on. Almost the same with the pool filter: there is no command to just turn on the pump, always there is a number of minutes. So the Core turn it on, but is the Node responsibility of turn it off. Also is it's reponsability to check if there is water flow, and if not to turn the pump off.
This requires a little more work, but is much more safer, In a sort of way is as the nervous systems works...
Clients
An MQTT Dahsboard has all the buttons, gauges, checkboxes and indicators needes to operate everything.
For push messages I use PushOver ($5 once in life) which works VERY well. Is very easy to integrate in Node-RED (and Bash, Python, whatever), and has configurables priority and sounds: A door was opened with the alarm is deactivated, is just a message with no sound or notification. But the same door with the alarm activaded is a top priority push message (max volume, even when the phone is in silence, non stop until you acknowledged it.
Finally in the VPS there is a webpage to see logs, graphs of many variables, and a MQTT web client to check the health of the nodes, reset them, view the console, and upload new programs.