Especifications


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:

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. 

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.