Sonoff TH16 with OpenHab

After an encouraging success with Sonoff Touch I ordered the TH16 model. It is designed to be hidden out of sight (as it is ugly and with tiny button) and it has additional 2.5mm port to connect a sensor. I chose AM2301 model which provides temperature and humidity measurements.

We begin with flashing the device following my guide for Sonoff Touch. After that we need to connect firmly the sensor. Please note that the plug needs to be flush with the casing, because it won’t work otherwise (it needs to loudly click when connected). I lost some time before figuring it out.

Then go to the webpage of you Sonoff, click Configuration, then Configure Module. While there change Module type to 04 Sonoff TH and GPIO 14 Sensor to 02 AM2301. After clicking Save it will reboot and you will be able to see temperature and humidity on main page of the Sonoff. If it doesn’t show up you need to check your cabling.

Having that working properly we need to go to Configure MQTT under Configuration. For my setup I chose a meaningful name like sonoff-road and sonoff/%prefix%/%topic%/ as Full Topic. Clicking Save will reboot the device again and you should be getting MQTT messages published to your broker. But this time they are quite different than those sent on simple power change. The topic is tele instead of stat and it sends JSON formatted data, like in following example caught with mosquitto_sub -d -t "#":

{"Time":"2018-02-24T22:11:10", "AM2301":{"Temperature":20.8, "Humidity":41.1}, "TempUnit":"C"}

It is published with the topic sonoff/tele/sonoff-road/SENSOR.

To use this data correctly we need to install JSONPath transform from the PaperUI of the OpenHab. Navigate to Add-ons and from Transformations tab click Install besides JSONPath item. Now we are ready to use it in .item file as follows:

Number ExternalHumidity "hum [%.1f]"
{mqtt="<[mymqtt:sonoff/tele/sonoff-road/SENSOR:state:JSONPATH($.AM2301.Humidity)]" }

For sake of completeness here is a item for handling power state:

Switch LightRoad "Road Lights" (gLightsCeiling) [ "Lighting" ]
{mqtt=" >[mymqtt:sonoff/cmnd/sonoff-road/power:command:*:default],
<[mymqtt:sonoff/stat/sonoff-road/POWER:state:default]" }

Flashing Sonoff Over The WiFi

This is a quick guide to installing the alternative firmware on the Sonoff Touch using only the WiFi. No soldering is required, just a laptop with a wireless adapter. I wanted to reflash this device for two reasons – to connect it to the OpenHAB via MQTT and to get rid of Itead closed source cloud enabled software. All steps below were carried out on a Ubuntu 17.10 although I am pretty sure they should also be working fine on a Ubuntu 16.04.

Preparing the tools

$ sudo apt install python3-pip python3-dev 

After that you need to get the latest PIP which is not bundled with Ubuntu. You could do it either via the virtualenv or just on your account. I went with the second option

$ python3 -m pip install --upgrade pip 

After that you can clone the SonOTA repository

$ git clone https://github.com/mirko/SonOTA.git 

Before you start flashing you need to make sure that you don’t have any firewall rules on INPUT that could cause trouble. If using ufw you need to check if it is off

$ sudo ufw status
Status: inactive

Flashing

When the environment is set up you can go into SonOTA directory and start the process:

$ cd SonOTA ; ./sonota.py

It will ask you about the IP address you wanted to use. Make sure it is the one on the interface connected to the same WiFi that Sonoff will be using. Also type in SSID name and the password. It would look like this (please bear in mind your IP will be different)

Current IPs: ['192.168.77.19']
Select IP address of the WiFi interface:
0: 192.168.77.19
Select IP address [0]:
WiFi SSID: YourWifiSSID
WiFi Password: YourWifiPassword

Using the following configuration:
Server IP Address: 192.168.77.19
WiFi SSID: YourWifiSSID
WiFi Password:
Platform: linux
** Now connect via WiFi to your Sonoff device.
** Please change into the ITEAD WiFi network (ITEAD-100001XXXX). The default password is 12345678.
To reset the Sonoff to defaults, press the button for 7 seconds and the light will start flashing rapidly.
** This application should be kept running and will wait until connected to the Sonoff...

At this point you need to look for a WiFi network name starting with ITEAD- and followed by some numbers. Unfortunately this wasn’t the case for me. That network wasn’t showing up at all. So I had to reset the whole device by touching the button on the Sonoff and waiting for about 7 seconds (until it started to flash its LED rapidly). After that the network showed up and I was able to connect to it with a password 12345678.

Being connected to that network you will see some more diagnostic messages until it shows the notice about “FinalStage” network, looking like this

*** IMPORTANT! *** ** AFTER the first download is COMPLETE, with in a minute or so you should connect to the new SSID "FinalStage" to finish the process. ** ONLY disconnect when the new "FinalStage" SSID is visible as an available WiFi network.

At this point you need to look for a network with a SSID “FinalStage” and connect to it. After a while it will continue with diagnostic messages (I edited them for brevity)

This server should automatically be allocated the IP address: 192.168.4.2.
If you have successfully connected to "FinalStage" and this is not the IP Address you were allocated, please ensure no other device has connected, and reboot your Sonoff.
............................Current IPs: []
.Sending file: /ota/image_arduino.bin
.Current IPs: ['192.168.4.2']
The "FinalStage" SSID will disappear when the device has been fully flashed and image_arduino.bin has been installed.
If there is no "Sending file: /ota/image_arduino.bin" log entry, ensure all firewalls have been COMPLETELY disabled on your system.
......200 GET /ota/image_arduino.bin (192.168.4.1) 17943.19ms

After a while the “FinalStage” network will disconnect you and dissappear. It means it has flashed completly and you need to look for a WiFi name starting with sonoff- followed by 4 digits. After connecting to it you need to point your browser at http://192.168.4.1/ and finish the setup. This step is required because the Sonoff won’t remember the settings you provided at the beggining of the whole process.

DHT11 vs DHT22

I am in a slow process of setting up home automation. It was Domoticz at first but my colleague talked me into OpenHab and I never looked back. But this is about something else.

I am using NodeMCU with ESP Easy software and sending sensors readings using MQTT protocol to the Mosquitto broker. Results were quite good altough readings seemed very innacurate and rounded to the nearest integer. So you had either 19°C or 20°C but not the decimal part. It shouldn’t be any suprise to me because when I announced that I am playing with DHT11 I was immediately told to ditch it for DHT22.

So I ordered DHT22 (AM2303 in fact) and set up them both on the same NodeMCU and used InfluxDB to store the data and then graph it with Grafana. The orange line is DHT11 and the green one is DHT22.

Comparison: DHT11 vs DHT22

The difference is amazing. Humidity reading is about 10% – 15% higher on DHT22 than on DHT11. The temperature difference is a little more, but it comes from the integer only reading. In worst case it is almost 2°C. Also as you can see it is much smoother because it gives you decimal readings. The DHT11 is less than a dollar per one and DHT22 is about $2.50. The choice for me is quite clear.

Please nothe that I don’t have a good reference thermometer so I didn’t include absolute comparision. It was rather one sensor vs another. But for sake of completness I used standard home digital thermometer. It was showing 19.8°C while DHT11 had 19°C and DHT22 had 19.4°C.