Search

Sonoff TH10 - Hygro-controled VMC with Tasmota, MQTT and OpenHab

Contents[Hide]

dropcap sonoff th10

Nowadays, a lot of IOT devices are working with the help of some servers based in China, USA, … Hopefully some Open Source firmware are available to free your IOTs to manage them fully and decide to which server they will connect.

Tasmota is one of these Open Source firmwares built to handle lots of IOTs based on ESP8266 chipset, like Sonoff devices.

One very interesting device is Sonoff TH 10 which includes a 10A switch, a temperature sensor and a humidity sensor.

This simple article explains how to :

  1. flash Tasmota Open Source firmware on a Sonoff TH10 wifi device
  2. configure the device with Tasmota firmware to connect to a Mosquitto MQTT broker
  3. setup an OpenHab server to manage a VMC based on this device, where humidity level manages the switch state.

To flash an alternative firmware on a Sonoff device, you will need a soldering iron, a USB to Serial adapter and a Linux computer.

This process has been tested with a basic FTDI USB adapter on a Ubuntu 18.04 LTS computer, but it should work on most USB to serial adapters and on any modern Linux distribution.

1. Install software

To flash the firmware, you will need to install python3-pip and latest esptool :

Terminal
# sudo apt install python3-pip
# pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
# pip3 install esptool
# esptool.py version
esptool.py v2.6
2.6

2. Download Tasmota

Next, you'll need to download latest Tasmota firmware.

Different firmware versions are available from Github projet site

As we are dealing with a device with sensors, we'll download sonoff-sensors version.

At the time of this article, latest version was v6.4.1 :

Terminal
# wget https://github.com/arendst/Sonoff-Tasmota/releases/download/v6.4.1/sonoff-sensors.bin

3. Connect your Sonoff device

To connect you Sonoff TH10 device to your workstation, you need to USB to Serial adpater like FTDI232.

Then, you need to solder and connect VCC, GND, Rx and Tx connexion points from the USB to Serial adpater port to the Sonoff TH10 board VCC, GND, E-Tx and E-Rx soldering points.

Make sure to cross Rx and Tx on both sides.

tasmota ftdi232  tasmota sonoff th10

 

4. Start the Flash process

During the flash process, make sure not to connect the Main power supply on the Sonoff TH10. Power will be supplied by the FTDI232.

4.1. Connect the device and enter flash mode

Every time you want to enter flash mode on your sonoff TH10, you need to plug the USB connector of FTDI232 while holding the RESET button on the Sonoff TH10.

After 5 seconds, you can release the RESET button. Your Sonoff TH10 should be in flash mode.

One LED may blink fast or may be off. It doesn't matter.

You can check quite easily that Sonoff TH10 has entered into flash mode by reading its wifi MAC address.

All following commands should be launched with sudo to make sure you have access to USB serial port.

Terminal
# sudo $HOME/.local/bin/esptool.py read_mac
esptool.py v2.6
Found 1 serial ports
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: 80:7d:3a:68:7a:84
Uploading stub...
Running stub...
Stub running...
MAC: 80:7d:3a:68:7a:84
Hard resetting via RTS pin...

Everything is ok ! You are now ready to flash Tasmota Open Source firmware to free your Sonoff from chinese cloud.

Between every command of the flash process you will need to re-enter flash mode. To do so, just :

  • unplug FTDI232
  • re-plug it while pressing reset button on Sonoff TH10
  • wait for 5 seconds
  • you should be again in flash mode

4.2. Backup current firmware

Next step is to back-up actual firmware, just in case.

 Unplug FTDI232 and enter flash mode again.

Terminal
# sudo $HOME/.local/bin/esptool.py read_flash 0x0000 0x10000 image1M.bin
esptool.py v2.6
Found 1 serial ports
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: 80:7d:3a:68:7a:84
Uploading stub...
Running stub...
Stub running...
65536 (100 %)
65536 (100 %)
Read 65536 bytes at 0x0 in 6.3 seconds (83.8 kbit/s)...
Hard resetting via RTS pin...

4.3. Erase current firmware

Next step is to erase current firmware.

Unplug FTDI232 and enter flash mode again.

Terminal
# sudo $HOME/.local/bin/esptool.py erase_flash
esptool.py v2.6
Found 1 serial ports
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: 80:7d:3a:68:7a:84
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 7.6s
Hard resetting via RTS pin...

4.4. Flash Tasmota firmware

Final step is to flash Tasmota firmware.

Unplug FTDI232 and enter flash mode again.

Terminal
# sudo $HOME/.local/bin/esptool.py write_flash 0x0000 sonoff-sensors.bin
esptool.py v2.6
Found 1 serial ports
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
MAC: 80:7d:3a:68:7a:84
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 1MB
Compressed 534032 bytes to 365310...
Wrote 534032 bytes (365310 compressed) at 0x00000000 in 32.4 seconds (effective 131.7 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...

Your Sonoff devices is now flashed with Tasmota firmware.

5. Connect to your Wifi network

You can now unplug your USB adapter and connect your Sonoff TH10 to the mains.

It will boot in config mode.

From you computer or from your smartphone, connect to its Wifi network SSID which should be sonoff-xxxx.

Configure your Home wifi network SSID, password and Hostname (make sure its unique on your LAN).

Once everything is setup, just Save.

sonoff th10 wifi

When you save the new configuration, your Sonoff TH10 should reboot and connect to your home network using DHCP.

From your Linux workstation, you can easily find its IP address with following commands looking for the Hostname you've defined.

Terminal
# fping -q -r 0 -g 192.168.x.0/24
# arp -a
...
vmc-front (192.168.x.157) at 80:7d:3a:34:45:3e [ether] on wlp1s0
...

6. MQTT configuration

You can now connect to its new IP address on your LAN and set it up according to your network.

In the following configuration, I use a SI7021 Temperature + Humidity sensor. If you use another type of sensor, you may have to adjust some configuration.

Make sure to plug the sensor completely in the jack plug.

Configure your Sonoff TH 10 device as follow :

sonoff th10 home  sonoff th10 sensor  sonoff th10 mqtt

 

With this MQTT setup, Sonoff TH10 will publish on the following topics :

  • your/topic/path/vmc/tele/LWT Online
  • your/topic/path/vmc/stat/POWER OFF
  • your/topic/path/vmc/stat/RESULT
    {“POWER”:“OFF”}
  • your/topic/path/vmc/tele/STATE
    {“Time”:“2019-02-24T17:38:26”,“Uptime”:“0T05:12:54”,“SleepMode”:“Dynamic”,“Sleep”:50,“LoadAvg”:19,“POWER”:“ON”,
    “Wifi”:{“AP”:1,“SSId”:“your-wifi-ssid”,“BSSId”:“00:21:F2:C6:42:22”,“Channel”:11,“RSSI”:100}}
  • your/topic/path/vmc/tele/SENSOR
    {“Time”:“2019-02-24T17:38:26”,
    “SI7021”:{“Temperature”:25.4,“Humidity”:68.6},
    “TempUnit”:“C”}

You'll be able to switch it ON / OFF on the following topic :

  • your/topic/path/vmc/cmnd/POWER ON
  • your/topic/path/vmc/cmnd/POWER OFF

7. OpenHab Configuration

Now that your Sonoff TH10 is declared on your MQTT broker, here is an OpenHab configuration example to control a VMC system (centrally controlled ventilation).

This configuration is using the switch to start VMC in full speed and the humidity sensor to automatically start VMC when humidity is too high.

It handles 3 running modes :

  • VMC forced OFF
  • VMC forced ON
  • VMC in AUTO mode

In AUTO mode, VMC state is based on the humidity level of the room :

  • if humidity is above a max level, VMC is switched ON to run full speed
  • if humidity is below a max level, VCM is switched OFF to run low speed

In OpenHab client, ventilation control looks like this :

sonoff th10 openhab vmc

First step is to declare the MQTT thing :

/etc/openhab2/things/mqtt.things
Bridge mqtt:broker:mosquitto [ host="your.mqtt.host.ip", secure=false, username="your-mqtt-login", password="your-mqtt-password" ]
{
    Thing topic bathroomvmc "Bathroom VMC" {
    Channels:
        Type switch : power "Power"   [ stateTopic="your/topic/path/vmc/stat/POWER", commandTopic="your/topic/path/cmnd/POWER", on="ON", off="OFF" ]
        Type string : state "State"   [ stateTopic="your/topic/path/vmc/tele/STATE"]
        Type string : sensor "Sensor" [ stateTopic="your/topic/path/vmc/tele/SENSOR"]
     }
}

Second step is to declare the MQTT channels and other items :

/etc/openhab2/items/vmc.items
// Bathroom - VMC switch MQTT and other items
Switch Bathroom_VMC_Power "VMC" <vmc> {channel="mqtt:topic:mosquitto:bathroomvmc:power"}
String Bathroom_VMC_State "State" {channel="mqtt:topic:mosquitto:bathroomvmc:state"}
String Bathroom_VMC_Sensor "sensor" {channel="mqtt:topic:mosquitto:bathroomvmc:sensor"}
String Bathroom_VMC_SSID "SSID [%s]" <wifi>
Number Bathroom_VMC_RSSI "Wifi [%2d %%]" <wifi>
Number Bathroom_VMC_Mode "VMC" <vmc>
Number Bathroom_VMC_HumidityMax "Humidity max [%.0f %%]" <hygro>
Number Bathroom_VMC_Humidity "Humidity [%.1f %%]" <hygro>
Number Bathroom_VMC_Temperature "Temperature [%.1f °C]" <temperature>

Third step is to create the rules beetween acquisition and command items :

/etc/openhab2/rules/vmc.rules
// -----------------------------------
// Rule to extract data from state JSON
//
// Wifi data :
//  - SSID
//  - RSSI (signal quality)
// -----------------------------------
 
rule "Bathroom - Convert State to values"
when
    Item Bathroom_VMC_State received update
then
    // use the transformation service to retrieve the value
    val valSSID = transform("JSONPATH", "$.Wifi.SSId", Bathroom_VMC_State.state.toString)
    val valRSSI = transform("JSONPATH", "$.Wifi.RSSI", Bathroom_VMC_State.state.toString)
 
    // post the new value to the Number Item
    postUpdate(Bathroom_VMC_SSID,valSSID)
    postUpdate(Bathroom_VMC_RSSI,valRSSI)
end
 
// -----------------------------------
// Rule to extract data from sensor JSON
//
// Sensor data :
//  - Temperature
//  - Humidity
// -----------------------------------
 
rule "Bathroom - Convert Sensor to values"
when
    Item Bathroom_VMC_Sensor received update
then
    // use the transformation service to retrieve the value
    val valTemperature = transform("JSONPATH", "$.SI7021.Temperature", Bathroom_VMC_Sensor.state.toString)
    val valHumidity = transform("JSONPATH", "$.SI7021.Humidity", Bathroom_VMC_Sensor.state.toString)
 
    // post the new value to the Number Item
    postUpdate(Bathroom_VMC_Temperature,valTemperature)
    postUpdate(Bathroom_VMC_Humidity,valHumidity)
end
 
// ----------------------------------------------------------------------
// Rule to handle the automatic VMC control based on humidity level
//
// Items
//  - Bathroom_VMC_Mode        : number handling VMC mode (on, off and auto)
//  - Bathroom_VMC_Humidity    : humidity sensor item
//  - Bathroom_VMC_HumidityMax : number handling target humidity level
//  - Bathroom_VMC_Power       : switch to manage high speed VMC
// ----------------------------------------------------------------------
 
rule "Bathroom - VMC auto"
when
    Item Bathroom_VMC_Mode changed or
    Item Bathroom_VMC_Humidity received update
then
    // --------------------
    // Items initialisation
    // --------------------
 
    // set default mode to auto
    if (Bathroom_VMC_Mode.state == NULL) { postUpdate(Bathroom_VMC_Mode,2) }
 
    // set default humidity target level to 50%
    if (Bathroom_VMC_HumidityMax.state == NULL) { postUpdate(Bathroom_VMC_HumidityMax,50) }
 
    // ------------------
    // VMC mode selection 
    // ------------------
 
    // VMC forced off (0)
    if (Bathroom_VMC_Mode.state == 0 && Bathroom_VMC_Power.state != OFF) { sendCommand(Bathroom_VMC_Power,OFF) }
 
    // VMC forced on (1)
    else if (Bathroom_VMC_Mode.state == 1 && Bathroom_VMC_Power.state != ON) { sendCommand(Bathroom_VMC_Power,ON) }
 
    // VMC mode auto (2)
    else if (Bathroom_VMC_Mode.state == 2 && Bathroom_VMC_HumidityMax.state != NULL) {
        // current humitity target and turn on/off temperatures
        var Number levelTarget = Bathroom_VMC_HumidityMax.state as DecimalType
        var Number levelTurnOn = levelTarget + 1
        var Number levelTurnOff = levelTarget - 1
 
        // switch ON in case of high humidity level
        if (Bathroom_VMC_Humidity.state > levelTurnOn && Bathroom_VMC_Power.state != ON) { sendCommand(Bathroom_VMC_Power,ON) }
 
        // else, switch OFF in case of low humidity level
        else if (Bathroom_VMC_Humidity.state < levelTurnOff && Bathroom_VMC_Power.state != OFF) { sendCommand(Bathroom_VMC_Power,OFF) }
 
        // else, switch OFF ii not initialised
        else if (Bathroom_VMC_Power.state == NULL) { sendCommand(Bathroom_VMC_Power,OFF) }
    }
end

Last step is to declare a sitemap to display the items :

/etc/openhab2/sitemaps/bathroom.sitemap
sitemap bathroom label="Bathroom" {
    Frame label="VMC" {
        Switch item=Bathroom_VMC_Mode mappings=[0="Off", 1="On", 2="Auto"] icon="vmc_auto_off" visibility=[Bathroom_VMC_Power==OFF]
        Switch item=Bathroom_VMC_Mode mappings=[0="Off", 1="On", 2="Auto"] icon="vmc_auto_on" visibility=[Bathroom_VMC_Power==ON]
        Setpoint item=Bathroom_VMC_HumidityMax minValue=10 maxValue=100 step=2
        Default item=Bathroom_VMC_Humidity
        Default item=Bathroom_VMC_Temperature
    }
}

Your Sonoff TH 10 is now fully managed by your OpenHab server.

 

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus