Why do i need that?
To avoid mold in the house it needs to be properly ventilated. Bathrooms are in the danger zone, because of the high humidity and temperature. Some time ago i bough a ventilator which automatically turns on/off based on the humidity, but it was never working good. It has variable resistors to tune it + some complex analog electronic and in fact was never working as intended and in about 1 year controller completely died. So, i had to find a replacement and decided that this is a perfect task for the HA. First part of the task was to define the requirements:
- Ventilator must be able to turn on when some humidity value is reached (e.g. 80%) and turn off on low threshold (e.g. 60%)
- It should be easy to change this thresholds. I would expect that its possible for them to be different at winter/summer seasons, may be not.
- It would be great to monitor Temperature/Humidity in the bathroom with the same device and report it to the DomoticZ server.
- Additionally it should be possible to start ventilator by pressing a button (e.g. to test if it works or for manual action)
- And to have a lot of fun it should be built on cheap and open source components.
Choosing the right platform
Initially i had an idea (and prototype) on the Arduino Nano + Relay board + DHT21 sensor + 5V power supply + some connectivity board (wifi or bluetooth). However, i found that there will be a lot of connected components + wifi boards for the Arduino are very limited and sometime expensive, a lot of custom code needs to be written, etc. So i decided to try ESP8266 platform based devices.
One of the known devices based on this chipset is Sonoff TH10/TH16 (only relay is different, 10A and 16A):
I found controller on the Ali Express, because on the official web site it was out of stock.
What is inside?
- ESP8266 module which provides SoC with wifi connectivity. Serial port (3.3v, be careful!) soldered on the board, but no pins connected (but easy to add)
- Internal power supply circuit to power 3.3V SoC from the 220V power line
- Mini audio jack socket to connect different sensors. DHT21 and few others are available from the vendor. I ordered mine with DHT21 which is Humidity + Temperature (and is much better compared to DHT11)
- Relay (10 or 16A) connected to the SoC GPIO and manageable from it.
Flashing Sonoff TH16
Native firmare works using a cloud and is integrated with the vendor services. This was not something i would like to have, so i decided to re-flash it. There are many options for the ESP8266 (Arduino sketches, NodeMCU, firmware from vendor, etc). After quick research i decided to use an ESP Easy firmware based on such features:
- It is free and opensource
- Comes with a handy web interface and support of many different sensors, so very easy to start with.
- Supports DomoticZ, MTTQ and other connectivity options.
- Allows limited scripting (Rules engine) to implement device-side logic
I decided to use version 2.0 (ESP Easy Mega) which is still under development, because release was lacking some of the functionality i need. It is very easy to flash firmware to the device – all you have to do is to solder 4 pins in front of the board, which are VCC, RX, TX, GND. To flash it you will need 3.3V TTL Serial converted with a 3.3v output to power the board.
⚠WARNING⚠!!! never try to flash the device when it is connected to the 220V powerline. There is no proper ground isolation and most likely it will kill your device, serial converter and PC. You should disconnect device from the 220v and power it from the TTL converted instead (or any other 3.3v source)
To flash the firmware i been using esptool.py tool and such command:
esptool.py -b 115200 --port /dev/tty.usbserial-A900ZKU9 write_flash 0x0000 ESPEasy_v2.0.0-dev8_normal_1024.bin
Device needs to be programmed in the flash mode, which is entered by powering on device with the button pressed.
Of course serial port name will depend on your OS and controller. After flash you should reboot the device and it will automatically create an SSID ESP_0 with a WPA password configesp. On 192.168.4.1 address there is a web interface to setup connection to the home WIFI network.
EasyESP initial configuration
After Sonoff device is connected to the wifi you can access it from the browser. First think i would recommend to do is to assign blue led to the WIFI status indication. This could be done in the Hardware section, by setting wifi status led to GPIO-13 . Next thing is to define devices available on the board: button (GPIO-0), and DHT21 sensor (GPIO-14). Additionally i defined dummy device, which will be later used in the rules engine:
To report values to the DomoticZ controller you have to define it in the controller section:
. Additionally you will have to create virtual devices (using virtual switch) in the DomoticZ for the Temperature + Humidity sensor and the switch. ID in devices section should match one from DomoticZ. Also you may want to setup syslogd ip to see logs from the device when not using serial. This is important, because in my case DHT sensor was not working properly when powered from TTL converter (properly not enough current) but its not possible to use serial when 220V is connected.
To avoid any dependency on DomoticZ controller i decided to put this simple logic in the device. There is a Rules engine (it needs to be enabled in the Advanced menu) which allows to define some simple device-side rules. It is very limited, but for my task it is enough. Some of the features i been using:
- Virtual device as variable holder and event loop provider. Every virtual device allow to store up to 4 values and creates own event loop (loop time is set by “Delay” value).
- Timers, which allows to call user-defined callback on specified timeout (and set a new one if needed)
- Events from the device (button, temperature sensors, etc). Events can be used in the rules.
- System startup event to define actions on a startups.
- Simple (no nested) if/else controls.
- Many functions which can be called from the code, e.g. to manager GPIO, do HTTP calls, etc.
Below is a ruleset i created. It defines threshold to turn on/off, reports FAN status to the DomoticZ, supports button which turns on ventilator for a 3 minutes even if conditions are not met:
On System#Boot do
TaskValueSet,3,1,75.00 // temperature on dummy sensor
TaskValueSet,3,2,70.00 // humidity on dummy sensor
// main loop
On OnOffValues#HumidityOn do
event FanOffIfNotTimer // workaround for no nestled if
On FanOffIfNotTimer do
// timer button is pressed
On Button#Switch=0.00 do
If [OnOffValues#FanOn]=0 // do nothing if FAN is already on
timerSet,1,180 // turn off in a 3 minutes
// turn off FAN after timer
On Rules#Timer=1 do
If [Temp#Humidity]<[OnOffValues#HumidityOn] // do not turn off if Humidity is High
// command to turn on fan
On FanOn do
TaskValueSet,3,3,1 // FAN is on
// command to turn off fan
On FanOff do
TaskValueSet,3,3,0 // FAN is off
Additionally i can turn on FAN from the DomoticZ using HTTP call to the
http://bath.local/tools?cmd=event+Button%23Switch URL. I used this as “On Action” for the “Push On” button switch. Of course, nice looking visualization is available:
Everything works as it should, however some of the things i would like to improve in the feature:
- Solution is not secured properly. I am planning to use own SSID for the HA devices to solve this problem, ESP Easy do have password protection, but it is to secure only web access, API calls are still not secured.
- Thresholds can be easily changed remotely using TaskValueSet command call
- Should threshold depend on the temperature or it is overkill? How long will DHT21 survive in such wet environment ? Subject to test… Anyway, its cheap.
- May be MQTT is better compared to DomoticZ HTTP API? Any comments on that?
As always, comments and suggestions are welcome 🙂