Home Automation System - SARCNET

School Amateur Radio Club Network
School Amateur Radio Club Network
School Amateur Radio Club Network
School Amateur Radio Club Network
Title
Go to content
Home Automation System
UNDER CONSTRUCTION

Introduction
A natural spin-off from all of our Raspberry Pi, Arduino and ESP8266 projects was the idea of building our own Home Automation System. We wanted to learn and use the best techniques to intelligently control and automate existing household appliances such as:
  • Solar inverters;
  • Smart meters;
  • Smart plugs;
  • Air conditioners;
  • Smoke detectors;
  • Video surveillance and
  • Security systems.
Requirements
  1. We need to monitor and control the appliances in our home.
  2. We need a graphical user interface to access these appliances from our smart phones when we are at home or away from home.
  3. We need local automation of these appliances to provide intelligent, energy-efficient, home heating, cooling, safety, surveillance and security.
  4. We need to ensure cyber security.
Design Decisions
Before we describe the various solutions that we have used, we wanted to write down some of our important design decisions.
  1. Third-party, proprietary, closed-source and cloud-based, solutions: This is a DIY project. We want to learn as much as possible ourselves and will shy away from existing, third-party or proprietary solutions if we can do it ourselves. That rules out existing home automation applications like HomeAssistant, which is very good and open-source and can run locally, but it's not exactly what we want. We have a privacy, security and availability aversion to all cloud-based solutions. All processing must be done in-house and must work even if the Internet connection is down.
  2. Local communications: Home Automation Systems typically comprise a central server, in our case a Raspberry Pi, and numerous, Internet of Things (IoT) devices scattered around the home. These IoT devices can use a plethora of communications techniques such as: WiFi; Bluetooth; Zigbee; LoRaWAN and Cellular. Since all our ESP8266 devices support WiFi, we will just stick to that.
  3. External communications: We need to access our Home Automation System when away from home using our smart phones. So the logical approach is to use the cellular broadband network and the Internet for that.
  4. Operating System: Typically we will need to flash all our IoT devices with an IoT operating system. There are too many embedded operating systems to mention here. We have not done any extensive evaluation, but initial success flashing an ARLEC Grid Connect smart-plug with Tasmota and a Wemos D1-Mini clone with ESPHome was so easy that we decided to check them both out. Tasmota has a nice web interface: Simply select the function of each pin from a drop down. The device reboots and it works. ESPHome requires a YAML script file: Compile and upload an image: The device reboots and it works. They both permit the device to be reprogrammed over the air, which is essential. In the end we chose ESPHome, because it is fully scriptable. What we don't like about it is that it takes 12 minutes to compile an image on a RPi 3 and it uses YAML of all things not Python (what were they thinking?)
  5. Messaging Protocol: To efficiently send commands and receive telemetry in real-time from IoT devices we need a messaging protocol such as MQTT, AMQP, DDS, XMPP or CoAP. Again, since we had some luck with Tasmota and ESPHome using the Message Queuing Telemetry Transport (MQTT) protocol, so we will stick to that. One advantage of using a messaging protocol is that the server and all clients only need to know the IP address of the MQTT Broker, and not each other.
  6. Rule Processing: Typically home automation rules are processed by cloud-based servers such as Node-Red or If This Then That (IFTTT). Well, we don't want our rules in the cloud. We can easily write our own Python scripts on our server to automate our IoT devices.
  7. Graphical User Interface: The GUI to monitor and control our home must run on any PC or mobile device. We don't have the time or expertise to write apps for all platforms, so the GUI will have to be accessible on any connected device via a web browser. The GUI web pages will be provided by a web server running on our Home Automation Server.
  8. Cyber Security: There is no real security with IoT devices, so we will use a separate WiFi router to isolate them from our normal home WiFi network and not permit them to have any Internet access. Each IoT device will have a static IP address and will be whitelisted on the IoT router's MAC filter. Strong WiFi access passwords and OTA programming passwords will be used. External access will be limited to the web pages from the Home Automation Server. It will be the only device which spans both networks, but will provide no route between them. For external access we will use a secure VPN. Having trialled both OpenVPN and WireGuard on our iPhones, we chose WireGuard for its smaller footprint and more secure and efficient tunnelling. It works well on all PC and mobile platforms.
Components
The Home Automation System comprises the following components:
  1. Home WiFi/Ethernet/Internet Router
  2. IoT WiFi Router
  3. Home Automation Server
  4. Solar Inverter and Smart Meter
  5. Reverse Cycle Air Conditioners
  6. Temperature Sensors
  7. Smart Plugs
  8. Garage Door Opener
  9. Smoke Detectors
  10. Video Surveillance System
  11. Security System
  12. Uninterruptible Power Supplies
Layout

Home WiFi/Ethernet/Internet Router
The home WiFi router provides a Local Area Network over WiFi or Ethernet for our home PCs and mobile devices. It also provides a gateway to the Internet via a hybrid fibre/coaxial cable network.
IoT WiFi Router
The IoT WiFi router provides a Local Area Network over WiFi or Ethernet for our home IoT devices. It is completely isolated from the Internet.
Home Automation Server
The Home Automation Server is the central hub of the system. It monitors and controls the home IoT devices via the IoT WiFi Router; implements intelligent automation rules. Provides a web-based, graphical user interface, over the Home WiFi/Ethernet Router, or over the Internet and broadband mobile network via a secure VPN.
Solar Inverter and Smart Meter
The solar inverter provides information about the electrical energy being provided by the roof-top solar array and the electrical energy being provided to the home and the grid. It connects to a smart meter via a CAN bus cable. All the information is provided over a WiFi network in XML format using a ReST protocol.
Reverse Cycle Air Conditioner
The reverse cycle air conditioners are used to heat and cool the home. The air conditioning mode (heating, cooling or dehumidifying) and the set temperature can be set over the WiFi network in CSV format using a ReST protocol.  
Temperature Sensors
The temperature sensors accurately measure the inside and outside air temperature. They connect via the WiFi network. They comprise our ESP-01S WiFi Module with a Dallas Semiconductor DS18B20 temperature sensor.
Smart Plugs
The smart plugs control the power to various household appliances. They connect via the WiFi network. Four of the Smart Plugs are ARLEC Grid Connect units flashed with the ESPHome operating system. One is our ESP-01S WiFi Module.
Garage Door Opener
The garage door opener connect to our Merlin MR850 roller door opener. It connects via the WiFi network using our ESP-01S WiFi Module.
Smoke Detectors
The smoke detectors alert to the presence of fire in the home or garage. They connect via the WiFi network. They comprise our ESP-01S WiFi Module connected to the unused I/O pin of the MC145012 chip in a standard home smoke detector.
Video Surveillance System
The video surveillance system provides CCTV cameras covering different zones of the home, including networked video recording and playback facilities with off-site storage. It connects via the WiFi network.
Security System
The security system provides monitoring of passive infrared sensors, an internal screecher, external siren and alerting to an off-site security monitoring centre. To connect it to our IoT WiFi network we had to design an build our own Dual-Zone WiFi Module.
Uninterruptible Power Supply
The uninterruptible power supply provides battery backup for all critical systems.

Interfaces
To connect various appliances to our new IoT network we had to design and build several interface modules, including:
  • ESP-01S WiFi Interface Module
  • Dual Zone WiFi Interface Module
ESP-01S WiFi Module
We wanted to connect some temperature sensors, smoke detectors, motion sensors and a garage door opener to our IoT WiFi network. The natural solution was to use the tiny Espressif ESP-01S breakout boards. These have an ESP8266 processor, 1MB of RAM and a WiFi interface.


Esspressif ESP-01S Breakout Board

These ESP-01S breakout boards have 4, 3.3V, General Purpose Input/Output (GPIO) pins. However, since we needed to connect it to a 12V power supply, a 12V switch input, a 12V relay output and a DS18B20 one-wire temperature sensor, we had to design and build our own ESP-01S WiFi Module to mount the ESP-01S breakout board together with extra interface components.
ESP-01S Boot-Up Investigation
One drawback with the Espressif ESP-01S breakout board is that the only available GPIO pins can be quite temperamental when the device is booting up. Some GPIO pins pulse high or low for up to 170ms and some can't be pulled low while booting (and some do both). So deciding which GPIO pins to use can be quite a challenge. Most users encounter these problems and simply apply workarounds using heavy filter capacitors on the GPIO pins. This is not an appropriate solution, of course.

To fully investigate the situation, we provided a 4k7 Ohm pull-up resistor on GPIO pins 0 to 3. We set each pin as a non-inverting output and alternately set the output high and low after boot up at the earliest possible opportunity. This was to discover the full extent of the boot-up waveforms. For those interested in the ESPHome YAML configuration, this is done using an on_boot event in the esphome section and turning on or off the outputs at a priority level of 600 - just after the GPIO pins are configured. In all cases the output waveform did not stabilise for 150ms after boot. Here are the boot-up waveforms that we recorded:


GPIO 0 Set High and Low After Boot


GPIO 1 Set High and Low After Boot



GPIO 2 Set High and Low After Boot

GPIO 3 Set High and Low After Boot

ESP-01S Pin Usage
Here are our design decisions:
  • GPIO3 is unusable as an output as the low level was above 1V, but it is the only pin that can be low on boot. So we used it as an input.
  • GPIO2 can be used as an I/O pin for the DS18B20 one-wire bus, but must be pulled high on boot.
  • GPIO1 is not needed in our design, but must be pulled high on boot.
  • GPIO0 can be used as an output, but only with a properly designed Low Pass Filter (LPF) to stop the boot-up pulses propagating to the output, and must be pulled high on boot.
ESP-01S WiFi Module Schematic Diagram
Here is the schematic diagram of our ESP-01S WiFi Module. U1 is the ESP-01S with 4 I/O pins, GPIO 0 - 4. U2 is a 3.3V regulator, which can handle up to 15V input. U3 is a Dallas Semiconductor DS18B20 one-wire temperature sensor. R1 - R4 are pull-up resistors on all I/O pins. Q1 is an input MOSFET, which can handle up to +/- 20V on its gate. Q2 is the LPF in conjunction with R7, R8 and C3. Q3 is an inverter, Q4 is an output, which can handle up to 60V at 115mA.

ESP-01S Module Schematic Diagram

ESP-01S WiFi Module LT Spice Simulation
To ensure the Low Pass Filter worked well under all conditions we used LT Spice to model and simulate its operation. Here is the LT Spice schematic: The GPIO provides 3.3V pulses. A 12V external supply is connected to Q3 via a 120 Ohm resistor, limiting the current to 100mA. We designed the LPF to remove up to 230ms of input pulses - a 50% margin on our measurements.

LT Spice Schematic of the Low Pass Filter output circuit

The LT Spice simulation is shown below. The GPIO waveform is shown in green. The output waveform is shown in blue. A 3.3V 230ms GPIO pulse is generated at 1s to simulate the boot-up pulse. A 3.3V 1s GPIO pulse is generated at 2s to simulate a normal GPIO output waveform. We can see that the 230ms pulse at boot-up does not propagate to the output, while the normal GPIO output does, except that it is shortened by about 230mS, which is OK. The main usage of this output circuit is to pulse a garage door opener, for about 1s, while making sure that the garage door is not operated at boot up.


LT Spice Simulation of the Low Pass Filter output circuit

ESP-01S WiFi Module Printed Circuit Board
The PCB top-view is shown below. The ESP-01S is mounted on the bottom side of the PCB.

ESP-01S WiFi Module Top View

The PCB side-view is shown below. In practice the plastic spacer on the header is removed and the ESP-01S is mounted flush. Depending on the application, some of the components may be optional.

ESP-01S WiFi Module Side View
ESP-01S WiFi Module Software
The ESP-01S WiFi Module was programmed using ESPHome YAML script.
Dual-Zone WiFi Module
We wanted to connect our home security system, which has 5 Passive Infra-Red (PIR) motion detectors, around the home, to our IoT WiFi network. We have a 4-zone, NX-4 Security System, which is configured for zone-doubling to support up to 8-zones. To achieve this, each zone input can be connected to two sensors, each with a different value End-Of-Line (EOL) resistor. The zone inputs change from being a simple 2-level analog input to being a 4-level analog input. We found that the voltages indicating which sensors are active are actually quite precise. We looked at various Arduino configurations and decided that we would need a 4-channel analog to digital converter, with 4 analog voltage inputs and 8 digital outputs, followed by a WiFi-enabled device with 8 digital inputs. This could not be achieved in one device. A 3.3v 8MHz Atmega328 Pro Mini board was used as the analog to digital converter and a ESP8266 D1-Mini board was used as  the WiFi interface. The security system provides a battery-backed 12V power supply. The processor boards have an on-board 3.3V regulator, but to be kind to them, we would feed them from a regulated 5V supply.
Dual-Zone WiFi Module Hardware
The two boards were mounted on a prototyping board with its own 5V regulator:


Dual-Zone WiFi Module

The input wires of the Dual-Zone WiFi Module are colour-coded as follows: Brown, red, orange, yellow: Dual-Zone Inputs 1-4. White: 12V. Black: Ground. The Green LED indicates power. The Red LED indicates that one or more zones are active. Annoyingly, the ESP8266 D1-Mini board is also affected by boot-up problems on several of its I/O pins. Hence, only 5 of the 8 digital outputs of the Atmega328 Pro Mini board could be supported. Luckily, in our home, we only have 5 PIR detectors!
Dual-Zone WiFi Module Software
The software for the D1-Mini board was an ESPHome YAML script. The software for the Pro-Mini board was written in C++ with the Arduino IDE. We used the constant declaration area to calculate all the voltages and resistor values required. Here is the code for it:

//These are the characteristics of the security system and its sensors
//The security system has 4 physical zones inputs (1-4)
const float Ri = 3240.0;                                //This is the measured internal pullup resistor, in ohms, on each physical zone in the security system:
//In dual-zone mode each physical zone input supports two zones, one low and one high as follows: 1/5, 2/6, 3/7, 4/8
//Each sensor comprises a Normally-Closed contact and a series End-Of-Line resistor.  
const float Rh = 3750.0;                               //The EOL resistance on the high zone sensor, in ohms. The high zone is active when Rl is NOT present.
const float Rl = 6970.0;                                //The EOL resistance on the low zone sensor, in ohms. The low zone is active when Rh is NOT present.
const float Rp = 1.0 / (1.0 / Rh + 1.0 / Rl);    //The EOL resistance with both sensors inactive, in ohms.
const float Vs = 13.55;                                 //The measured security system supply voltage, in volts.
//The dual-zone input voltage is Vz
//Vz equals Vn, Vh, Vl or Vs, depending on which sensors are active as follows: None, High, Low or Both sensors
//Here are the formulas for the dual-zone input voltage in each case, in order of decreasing voltage:
const float Vl = Vs * Rl / (Ri + Rl);                 //Low
const float Vh = Vs * Rh / (Ri + Rh);              //High
const float Vn = Vs * Rp / (Ri + Rp);              //None
//To identify active zones we require 3 threshold voltages between Vs & Vl, Vl & Vh and Vh & Vn: Vsl, Vlh and Vhn. They are:
const float Vsl = (Vs + Vl) / 2.0;
const float Vlh = (Vl + Vh) / 2.0;
const float Vhn = (Vh + Vn) / 2.0;
//The active zones can then be tested as follows:
//Both: if Vz >= Vsl
//Low:  else if Vz >= Vlh
//High: else if Vz >= Vhn
//None: Otherwise

//The constant K is used to calculate the actual zone input voltage from the analog read value
//Given the Arduino supply voltage and the input resistive-divider values
const float Vcc = 3.3;                                   //The Arduino Pro Mini supply voltage
const float Amax = 1023.0;                           //The analogRead values range between 0 and Max for voltages between 0 and Vcc
const float R1 = 47000.0;                             //The chosen voltage divider resistor connected to the zone input
const float R2a = R1 / (Vs / Vcc - 1);             //Calculate the required voltage divider resistor connected to ground (= 15132)
const float R2 = 15000.0;                             //The chosen voltage divider resistor connected to ground
const float Vmax = Vcc * (R1 + R2) / R2;      //The maximum permitted zone input voltage
const float K = Vmax / Amax;                       //A constant to convert analogRead values to zone input voltage

And here are the printed results:

System Characteristics:
Ri: 3240
Rh: 3750
Rl: 6970
Rp: 2438
Vs: 13.55
Vl: 9.25
Vh: 7.27
Vn: 5.82
Vsl: 11.40
Vlh: 8.26
Vhn: 6.54
Vcc: 3.30
Amax: 1023
R1: 47000
R2a: 15132
R2: 15000
Vmax: 13.64
K: 0.0133

Back to content