Building Automation & SMART HOME based on ESP8266 microcontrollers & Raspberry pi platform
Home (building) automation may decrease home maintenance costs or reduce wasting and definitely increases comfort of the household members
Purpose of the project:
The project has been motivated by author's hobby. Electronics, programming and networking it's a great mix while almost everything may be done especially having Internet of Things (IoT) support.
Project approach:
The project is NOT based on "ready to go" smart devices/systems dedicated for home automation like Tasmota, ESPhome solution or smart sockets with branded firmware, however some of them are in use too.
The project is mostly based on microcontrollers ESP8266 which can work with a number of Arduino peripherals. Adding some code (C++) gives control on their input/output with desired logic behind. Thanks to that, a ESP8266 with Arduino sensor, stepper motors, radars and so on, may be used in many roles and do whatever is needed.
All of it, is integrated with user-friendly User Interface (UI) which is based on Home Assistant software hosted on local Raspberry Pi server. HA UI does use its own API based on XML which is parsed by ESP8266 microcontrollers. Data is stored in Home Assistant database and it may be pulled by standard SQL queries.
Quality of used sensors is good enough. After few years of using Arduino sensors, I can surely say, they are reliable. Most importantly, the sensors fulfill their function in all the cases. Measures are done in an accepted measure tolerance, so there is no need to looking for new solutions. It has to be pointed too - the prices of these microcontrollers and sensors are extremely low, means ESP8266 + relays + temperature sensors + PIR sensor may be build withing less than $10 USD.
Place of implementation:
This project has been implemented in two-floored house with basement and attic. It also covers backyard, garden and estate gate and gateway. It does use home WiFi network.
Portable module may be used everywhere on our Globe where GSM coverage exists.
Technologies used:
Hardware platform: RaspBerry PI (advantage because of its GPIOs).
Operating System: Linux based, Raspberry Pi OS (former Raspbian) .
IoT approach.
ESP8266 microcontrollers (pure ESP8266, E12x and nodeMcu).
Devices communication based on WiFi 2.4 GHz (TCP/IP stack) and wired.
Programming languages: C, C++, C#, Python, Bash.
Programming environment: Arduino IDE.
Database: SQLite.
API: XML with JSON.
Integration with GSM network.
VPN.
PBX support, using IVR and caller white-listing (access control), based on Asterisk software.
Artificial Intelligence (AI): Google Assistant with IFTTT
Using 5 V power supply with micro USB socket (compatible with mobile chargers or power banks).
Portable module to perform and log measurements anywhere in the World.
MQTT broker for smart sockets flashed with TASMOTA software.
OTA for ESP8266 upgrades without cables.
Using Raspberry Pi audio card for audio notifications.
Arduino sensor i.e. Dallas DS18B20, DHT22 etc.
System characteristics:
Scalability.
User Interface (UI) for control through Mobile, Tablet or PC.
Integrated with Google Assistant (controlling devices by voice commands however Home Assistant is not involved in the feature).
Graphs of stored data in User Interface.
Designed to limit WiFi radiation (lower network traffic and less energy consumption).
Real Time (current date) on-boarded on ESP8266 with Daylight saving time support.
Ability to integrate with any device (e.g. amplifier, went, TV, garden pump, lockers etc.)
Some of the main features:
Fans:
Kitchen, bathroom/restroom, whole floor.
Exhaust fans (750 m³/h each) are installed on chimney. One is dedicated for the kitchen cooker hood, second for bathroom/restroom. There also two fans installed inside (~540m³/h each) for internal air circuit and to supply fresh air from outside. Small fan is also installed in pantry and is connected with bathroom steerage.
All these are based on single ESP8266 board.
Some use cases:
Took a bath/shower?
Automation will trigger fan to remove vapor once you are done. Fan will be triggered if no human presence detected for 10 minutes (PIR sensor).Drying the laundry.
Wet clothes become dry fast if temperature is high and humidity is low. Automation triggers fan if specific humidity values are met. In winter season, clothes are dry up to 24 hours.Unpleasant smell in restroom/bathroom.
Fan can be triggered by manual button. Will be turned off after adjustable time.Cooking/Frying:
Kitchen hood fan will be automatically triggered once cooking started and turned off once finished. This feature is based on temperature above a stove compared to temperature on whole level.
Speed of the fan is controlled by automation depends on temperature outside (cost savings).
Trigger up in automation mode: temperature above stove measured by DS18b20 sensor higher 5°C than local environment (floor level). May be adjustable over Home Assistant UI. Went efficiency: up to 750 m³/h (500 Pa).
Trigger down: if temperature above stove is less or equal in compare to other rooms.
Heavy cooking or frying mode - if activated over button in Home Assistant User Interface (called later HA UI) kitchen fan goes to full speed and supply fan is activated.
Automation may be disabled. Automation fully done on esp8266. States are send to Home Assistant UI.Some fresh air needed on whole floor?
Virtual button on HA UI to run all fans in full speed for adjustable time. Opening windows windless days and using this feature helps to replace air quickly (airing).Another automation, if activated, will trigger up airing withing or after wake-up alarm (useful in winter season to replace air daily).
Bathroom went automation.
Trigger up: Relative humidity (RH) higher than 70% however Absolute Humidity (AH) in bathroom must be less than AH outside and no human presence detected for at least 10 minutes (PIR sensor). Trigger down: RH lower than 55% or AH bathroom same or less than AH outside or by virtual button (active for 15 min). Automation modes: active, not active.
Kitchen fan speed live-regulation based on temperature outside.
Reason - cost savings. No recuperation at home is installed, so in a winter season a lot of heat was went out during cooking and frying.
100%, 80%, 50%, 30% levels over automation - based on combination of outside and inside temperatures. For example, if outside less than -5°C fan will work on 30%. Manual customization over HA UI if automation for kitchen ventilation is deactivated, 1-100%. Special function - on demand (as trigger in HA UI) - 100% went speed and 100% went speed for supply fan (i.e. frying).
Regulation of the fan is made of two relays controlled by ESP8266 and separate AC regulator, 230V AC (grid voltage), 4000W, (Pic. 8). Regulator has buttons for speed up/down which were replaced by relays as their equivalent (mechanical pinning).
LED strips lighting (kitchen, bathroom, staircases and restroom):
To achieve cost saving, the LED lights (60W in total) are configured to auto turn on/off depends on PIR detection in specific area. Ideal solution if small kids at home.
All these automations are based on ESP8266 board.
Based on twilight sensor.
Continuous light if PIR sensor detects any move.
LEDs above the sink can be set into "economy mode" - if dark outside, lighting time taken from HA UI panel (longer), else (during the day) lighting set for 15 seconds. Timer auto-extended if movement detected.
"Sleepy eyes protection" - Bathroom, restroom, kitchen LED lights are deactivated between given hours (i.e. 0:00 AM - 5 AM). LED above the sink remain activated.
Light control based on virtual buttons + dusk sensor to trigger lights (energy saving).
Night light (soft light) control.
Photovoltaic (PV) management:
Based on national rules, an inverter moves in to failure mode if average of grid voltage on any of phases in last 10 minutes exceeds 253V.
Grid over-voltage protection (>253V) based live-monitoring of the grid voltage on each phase using smart sockets with Tasmota software integrated with Home Assistant and self-controlled over Python script.
A Python script monitor voltage values on each phase using Tasmota HTTP API. It calculates average from last 2 minutes by having 40 measures. Triggers up the socket if grid voltage is over 253V or if peak of 256V appears. Then turning off if average is below 251V.
Behind each socket there is 50 liter boiler (for each phase). Power of each boiler is 2kW (3x2kW) which successfully decrease voltage and keeps energy consumption in the same time reduces gas consumption that would be consumed to heat water.Boiler temperature measurement.
Boiler temperature LED indicator. To know if water in boilers is heated up. Installed blinking LED light. It blinks per programmed temperature edges i.e blinks 4 times per 1 second if water in boiler is above 50°C and 1 time per 3 seconds if it is 30°C or not blinking if below 30°C.
That helps to manage balance between natural gas consumption and electricity consumption by using hot water from boiler.PV inverter as well as electricity supplier data is integrated with Home Assistant thanks to so-called custom integrations (made by Home Assistant community).
Quick and easy access to info about energy generation/consumption.
Helps to manage current electricity energy consumption/generation over biling periods (own calculations) to find a moment when cheaper is to start using natural gas (for central heating system, hot water circuit and cooking). Own calculations based on data availalble through the Integration (on GitHub).
Central heating and hot water management:
Hot water circuit (keeps hot water in circuit to have hot water in faucet immediately).
Two ESP8266 are used:
Esp8266 #1 (multi-function), upstairs, by default, controls lights in kitchen, bathroom, restroom. For the feature purpose, it sets one of GPIO to high state if human presence detected in (or) kitchen, bathroom, restroom. The output of this ESP8266 is input for second ESP8266.
Esp8266 #2 (multi-function), basement. For the feature purpose it checks state of the input wired with ESP8266#1. It also checks temperature of water circulation pipe with DS18b20 sensor.
If it meets given conditions, means:
high state on input (someone step in into kitchen/bathroom/restroom),
cold water in circulation pipe (< 33°C),
hours different than 9 AM and 3 PM (adjustable)
It turns on the water pomp for specific time (currently 5 min). It also sends states of pump and water temperatures to Home Assistant UI to gather statistics.
Other home Devices management:
Amplifier control over ESP8266: manual button, virtual button and time triggered up. Trigger down: timer or button (virtual or manual). Side note: Amplifier is from '90.
"Wake me up with favorite music!" - every room has speaker installed.
Alarm can be set to play music to wake up or for other purpose.
Speakers can be set to automatically turn off after specified time (power saving).
PBX based on Asterisk:
Estate gate and gateway:
Apart from of physical buttons, controlled over mobile based on GSM with IVR.
Access only from white-listed callers to open gate or gateway.
Audio invitation for caller. If caller calls for specific number PBX says "Hi... to open gate press 1 etc".
Audio notification for households - once caller opened gate or gateway, specific information is played over speakers i.e. person X opened the gate.
Apart from PBX, there are some automations to keep gate opened if needed.
Network devices control:
specific callers are authorized to restart router by phone call by pressing specific extension.
All the features above are based on Raspberry PI GPIO.
Audio notifications:
Audio notifications which are NOT based on Home Assistant:
see PBX section. Own script that read Asterisk log and based on the log, playing specific information like, who just opened the gate or gateway (by mobile phone).
Audio notification partially related with Home Assistant (based on automation that runs script to use audio card)
Notification about thunderstorm: If specific number of lightings are detected in amount of time (over blitzortung integration), the audio notifications (voice) is played to notify Household about coming storm.
Notification about someone is entering specific zone. Very useful to know if someone is coming to specific floor.
CCTV:
Home Assistant is used to preview streams from local as well as remote cameras (from other locations)
Presence detection:
It is useful to know if there is anyone at home. There is specific solution implemented - if set of PIRs on a level detect move, they triggers specific virtual button and keep it active until in next 15 minutes there will be no movement.
Other:
LED lights on whole level are behind UPS
Anti-freeze feature for fans installed on chimney (active if temp. outside < -1°C).
Gas and Carbon Monoxide detection.
Leak detection.
Weather station: twilight, rain, temperature, humidity, barometer sensors.
Smart socket control over Tasmota integrations.
Back-end features that improve stability of whole system:
ESP8266 software is resistant on lack of local network or the Internet and keeps working on latest data.
Home assistant with custom integrations may be not stable, may contain bugs if any of custom integration made by community is released. To achieve stability, the following have been implemented:
Restart HA if log size increased too fast (common issue).
Restart HA if HA PID is present but service is not availalble (done by CURL command).
Sets up default (customized) values for all sensors (entities) if restart happen.
Critical scripts/tools observers. Raspberry manages PV over-voltage issue - grid AC voltage higher than 253V and causes failure mode of the solar inverter. To manage the task flawless it is observing condition of smart sockets (is active, sending data, is logged into local network etc) as well as condition of Python script is that manages the sockets. If failure detected it kills the PID of the script and attempting to start it again.
Re-run Asterisk if from some reason its PID is gone.
Weekly auto-backup.
Whole SD card snapshot is implemented.
Most critical nodes have power supply redundancy (power bank).
Whole network is behind UPS.
All devices are configured to work power-saving mode (40mA @3,3V) for specific period of time.
Home alarm:
There is expanded alarm management. It will not be described here due security reasons. However there is interesting feature that brings comfort to household members:
Information in HA UI (icon) as well as LED indicator (blinking in specific way) about main entrance door are fully locked.
Raspberry PI - roles:
Server for Home-Assistant.
Gateway for local network.
Pi-Hole implemented to filter out adds, tracking links, analytics, porn, crime, hazard.
Firewall based on iptables to block i.e. TOR network in local network.
Network File System (NFS) to keep family photos, videos in highest quality.
VPN server for access from Internet.
Audio notifications over speakers.
Network monitoring.
Using Raspberry PI GPIO for critical roles i.e. opening gate (flawless).
Self-health based on bash and python scripts as mentioned above.
Home Assistant - roles:
Home Assistant software is critical as it is interface for human as well as interface for esp8266 microcontrollers. That means that data send by devices is used by other microcontrollers and devices as their input. For instance, weather station gives data about humidity outside, so esp8266 that manage fan for bathroom may trigger fan in desire moment. Weather sensor gives also data about light (day/night), so microcontroler responsible for lights may turn them off/on depends of time of day.
Home Assistant is known from add-ons or, let's say, plugins so called "integrations". Integrations may be official, fully supported but also may be created by community (over HACS). Community integrations give huge chances that there will be software that is currently needed.
Home Assistant in this project has following integrations (so far):
Solar inverter integration. Thanks to that some calculations may be done, i.e. auto-consumption of solar energy as well as live monitoring of solar energy production.
Environmental integrations like Arliy and others to get:
Information about air pollution.
Information about level of river which is close to the location.
Information about lighting (Blitzortung) to give audio notification about coming strong storm.
Integrations related with devices:
Tasmota to control smart sockets - critical for PV management described above.
Integration for printer to get statistics about printed pages and level of ink.
Integration for routers to get information about bandwidth utilization.
Integrations for TVs to control them by automations (i.e. turning off and midnight).
Integration for Google calendars.
In deployment & planned features:
Integration with central heating system.
SMS and email notification.