Home > Espressif, FreeRTOS, Hardware, Testing > Getting Started with Sonoff TH16 ESP8266 Relay and Sensor using the Stock Firmware and eWelink Android App

Getting Started with Sonoff TH16 ESP8266 Relay and Sensor using the Stock Firmware and eWelink Android App

November 18th, 2016 Leave a comment Go to comments

Sonoff TH16 is a WiFi relay device powered by Espressif ESP8266 WiSoC, able to handle a load up to 16A (3500Watts @ 220V), and including a jack for sensors. The company recently sent me a sample together with  Sonoff AM2301 temperature and humidity sensor, and Sonoff POW power meter device, and in the first part of my Sonoff review I checked out the hardware for all three items. In the second part, I’ll report my experience with Sonoff TH16 and the temperature sensor with a practical example, as I’ve installed it to control a groundwater pump.water-tank-pumps

What you see above is a picture of my house water tank with two pumps, the white one keep the pressure in the taps and is always on, and the blue one pumps the water from the ground and is the one I’m going to control. Over the last year or so, I’ve tried two other solutions with SimpleLink self-powered power switch and later a NodeMCU board running NodeMCU firmware and a simple web page to control a relay. The first solution worked as long as the white pump did not start, but if it did the ensuing voltage drop may turn the switch off. The second solution shown below was quickly hacked together and worked fine for many months, but if the WiFi connection was lost, I had to restart NodeMCU board by turning off and on the circuit breaker for the pumps.

Click to Enlarge

NodeMCU + Relay controlling a water pump – Click to Enlarge

As you’ll see from the “Before” side of the first picture, I also kept everything in a plastic box, and using Sonoff TH16 cleaned things up a bit.

water-pump-parametersFirst let’s make sure the pump parameters are not out of the specs of Sonoff TH16, and there’s indeed no issue as the pump Wmax is 1020W, and it operates at 220 to 230 V AC. Sonoff TH10 & TH16 relays have three outputs and inputs with the ground/earth pins (E), neutral pins (N), and live pins (L) clearly marked once you open the cover.

sonoff-th16-ground-live-neutralWhen you connect a two prong plug into a wall socket, you don’t normally have to care with side you plug it in since we are dealing with alternative current, but for safety reasons it might be a good thing to connect the 110V or 220V live wire into the live IN terminal. One good way to find out the live wire is to put it on your tongue and if it tickles you’ve found the right one. NO! Don’t do it… We are dealing with high voltage here, and electrical shock may kill you, so take precautions, and make sure electricity is off before doing any cabling.

Having said that you’ll still need to check the live wire when the power is up, and you can do it with an electric tester pen as the one shown below.
Just make sure you are very careful doing so, and my model has an LED which will lit with the Live wire, but not the neutral one. It turns out the black wire is the neutral one, and the white/grey one is the live one as it should be.

Click to Enlarge

Click to Enlarge

Then  I tried to connect the mains live and neutral cables to L IN and N IN terminals, and do the same for the pump cables. You need to push the button on the terminals don’t but they are quite hard to push, and if only remove the small cover, the live IN and OUT terminal are quite hard to reach. I worked on a ladder, so it complicated slightly the task, but as you can see on the picture above, I managed to dislocate L IN terminal… After that I realize I forgot to pass the cable through the hole in the case, so I pushed hard to remove it, and everything came apart.
sonoff-th16-broken-terminalIt took me a while to find out how to reassemble it, as I first try to place the metal bit inside the tiny plastic terminal, before I realised I had to insert it in the soldered metal bit first as shown below.
sonoff-th10-th16-terminal-assemblyI then pushed the plastic bit back, and added the white button on top, and could finally complete cabling successfully.

Click to Enlarge

Click to Enlarge

The last steps were to clip back the top part of the case, and screw the small cover on, which nicely pushed the rather hard wires in place.
I completed the hardware setup by attaching Sonoff TH16 to the armature of the pump roof, and connected the temperature and humidity sensor and placing it as further as possible from the roof since it can get hot in summer.

Click to Enlarge

Click to Enlarge

The hardware setup took me about one hour mostly due to hard buttons, and average quality terminals.

Then I switched to software setup. I first tried to download the apk using the QR Core on the package (coolkit.apk), but the connection to the to server was unstable, and had to repeat the download again… unsuccessfully. So instead I went directly to Google Play to download eWelink app. I should have done that first, as the installation worked beautifully. Then I basically followed the instructions, which started with registration.

Click to Enlarge

Click to Enlarge

The login Window shows “Phone number/Email”, but once you click on Register, you’re only option is to input your mobile phone number. If you don’t like it, you can always install your own firmware on Sonoff TH16 instead. Once you’ve entered your phone number, you’ll be sent a 4-digit code for registration, and be asked to input your password. I did that, and since it took a while, I left the phone unattended for a while, and as you can see from the screenshots above I started at 11:18, but at 11:35 it was still trying to register, so I stopped that, and tried again with the same code, but no luck. So I repeated the task again with a new SMS code, and I could finally registered. At this point, you’ll get the an device window with a “+” sign to add a new device.

Click to Enlarge

Click to Enlarge

I did that and for the first step I had to climb back on my ladder to press the button on Sonoff TH16 relay for about 5 seconds, and check the LED was blinking regularly to make sure it was in pairing mode. The LED is not particularly bright so outdoor it’s not that easy to check. Anyway, after several attempts I managed, and click Next, inputted my WiFi router password, and after two or three attempts at pairing, I could finally successfully add the device which I named “Water Pump”.

Click to Enlarge

Click to Enlarge

It will show in the device window, and you can control the relay from there tap on the on/off button. You can also tap on the other part of “Water Pump” zone to enter a more detailed menu showing the temperature and humidity (if you’ve connected the sensor), as well as auto and manual modes, a share mode to let other smartphone control the relay (it worked well), as well as timer and countdown mode. Our water usage is pretty regular, so I’ve set a 20 minute timer starting everyday at 8:00 and stopping at 8:20. The water pump itself can be configured to work automatically based on the water tank level, but other people in the house don’t like that since it’s not always reliable, which I why I’m using a WiFi relay.

Click to Enlarge

Click to Enlarge

The temperature and humidity values were realistic and regularly updated. Auto mode allows you to set temperature and/or humidity thresholds to turn on or off the relay, and the Profile section of the app will allow you to send feedback, switching language, check the app version (updated twice from Google Play in one week).

I’ve been using the system for around a week and it works well in normal conditions. If I turn off my broadband provider’s modem, I won’t be able to turn on or off the relay manually even when WiFi is enabled, but the good news is that the timers are local, so even If I disable both Internet and WiFi, the relay turns on and off at the scheduled time. This obviously only works if the relay already got its time from the Internet earlier since there’s no RTC. I’ve also tried to turn on and off the relay out of my home, using my neighbour WiFi and I could control the relay from the Internet. It might not be so useful for a pump, but if you forgot to turn the lights off it’s a very convenient features. So while the setup itself did not go very smoothly, the relay has done its job well so far.

I’ve scanned the relay to check open TCP ports.

TCP port 800 (http) is open, but when I access it directly I don’t get anything, and capturing packets on my phone show some binary payload. I’ve not spend time on it, because there’s probably very little value in reverse-engineering the protocol, simply because of the 4-pin to flash your own firmware. Since it’s based on ESP8266 you could flash Arduino or NodeMCU firmware and write your own web interface, or instead try some home automation firmware like ESPeasy or ESPurna. Just don’t do it while connected to the mains, and instead power the board through the USB to TTL debug board needed for programming. ESPurna has been designed specifically for Sonoff devices, and I can see it supports Sonoff POW, which I have yet to review, so I’ll probably test it in the third part of the review to monitor power consumption of my office.

Sonoff TH16 and AM2301 sensors are be purchased for $8.60 and $4.0 respectively on Sonoff TH page. Shipping is not included, but only adds a few dollars with registered airmail shipping, and a bit more for EMS or DHL Express shipping.

  1. Harley
    November 18th, 2016 at 18:16 | #1

    Again, would really like to see a review of ESPEasy (ESP Easy) firmware on these:


    ESPEasy (ESP Easy) firmware gets rid of relians of externral cloud services.

    With original Sonoff firmware’s cloud services you rely on the internet being up.

    I prefer ESPEasy (ESP Easy) firmware since its nativly supported by Domoticz


    Highly recommend Domoticz home automation software to all tech-savvy.

  2. paul
    November 18th, 2016 at 19:13 | #2

    Looking forward to your test of the Sonoff POW. Their page showing the Android App to show & control monitoring cycle & cloud storage looks v. interesting and useful. However, with a spec of only 1% accuracy, and scale 16Amp/3.5KW, I fear it will be inaccurate to monitor the more usual low-power scenarios where I live, as I rarely use hi-power appliances like Microwave or fans. I see they offer to modify it to user requirements, which if it wasn’t for infrequent use of a 1.1KW Microwave cooker, the running my 92Watt Fridge-Freezer cooling cycle & rest of my running equipment adding upto 80Watts, it would be simple to alter their fixed value shunt resistor to scale max 200Watt for better low-power accuracy.

    Unless they can fit (readily available eg Franky’s ZY1270), more accurate current/voltage sensing, perhaps they could incorporate auto-switching between at least 3 shunt values – like in auto-ranging multimeters to give 3,500W, 350W, 35W auto-scale so accuracy is maintained over the whole range of domestic scenarios.

  3. November 18th, 2016 at 20:19 | #3

    just received today a sonoff rf with wifi and 433mhz+remote….play later with domoticz 🙂

  4. November 18th, 2016 at 20:38 | #4

    The installation is quite a pain, so I’m not going to remove Sonoff TH16 from its location right now. As mentioned in the post I plan to test ESPurna with Sonoff POW since it supports HLW8012 power sensors found in the device. In ESPeasy also supports HLW8012 I might test it instead. I only plan to test one firmware since it’s not really convenient to switch between firmware with this type of device.

  5. cyk
    November 18th, 2016 at 21:07 | #5

    cnxsoft :
    @Harley I only plan to test one firmware since it’s not really convenient to switch between firmware with this type of device.

    In fact it is, because ESPEasy allows you to upload an update or replacement image via the web interface.

  6. Drone
    November 19th, 2016 at 16:26 | #6

    China to World: “All your water pump BELONG TO US.”

  7. rebeL
    November 19th, 2016 at 20:54 | #7

    Why is it necessary for Sonoff to control it over internet cloud, I thougt it goes only locally over wifi?

  8. November 19th, 2016 at 20:58 | #8

    The cloud service makes it easy to control the relay from anywhere where you have an Internet connection. If you don’t need this, then yes, it’s better to control directly from the local network, but the default firmware and app does not allow that. As mentioned in the post, you can also easily flash your own firmware in Sonoff devices.

  9. paul
    November 20th, 2016 at 01:57 | #9

    When I composed my earlier post on this (no other posts on this when I started) I didn’t realise an internet provider connection was a prerequisite for ‘home automation’ to work. That is a serious weakness and deal-breaker for me. Home automation should be designed to be fault-tolerant, and capable of operating independant of factors outside the ‘Home’, in my case, even able to run for a while using local battery power. You mention can design ones own firmware… but I was admiring the look of their Android app… thinking it could save me much time designing my own.

  10. Rob
    November 20th, 2016 at 04:50 | #10

    @rebel @paul
    The IoT ecosystem is sadly heading in the direction with every provider mandating their own proprietary cloud service which requires them to punch holes out of/into your home LAN’s firewall. This of course means you have to trust the provider’s security which is typically a mystery and they have the ability to EOL your service/devices at their discretion. No local LAN web interface or API so you have to rely on their sh*ty, proprietary, mobile app which probably wants too much access to your device and drains the battery at an alarming rate.
    Sod that, I want control over access to my network, mix and match devices, and decide how or whether I present a common home automation portal to the internet. Fortunately as cnxsoft has said above you can flash other firmware on these devices which removes this dependency and implements an mqtt interface. Unfortunately this is not suitable for the consumer market and never will be and I can’t see any clear winners at the moment which will help define the standard and let’s be honest, none of this probably matters to your average consumer.
    Anyway rant over, there is an opportunity here for someone although the big players don’t appear to be getting it right so far with some making a land grab for users without a clear long term business objective other than locking them into your ecosystem.
    Note : None of this is specifically aimed at Sonoff by the way, they do appear to be making some good devices, at a reasonable price point which can be hacked to do what you want them to do …. just need a bit more compliance / certification 😉

  11. November 20th, 2016 at 10:21 | #11

    This reminds that some products from Nest became useless once they decided to stop supporting them. People paid $300 for it, and can’t use it anymore -> http://uk.businessinsider.com/googles-nest-closing-smart-home-company-revolv-bricking-devices-2016-4

    They should find a way to combines local network and cloud services, with the latter optional (opt-in) and maybe adding some extra features like remote access for people who want/need it. If the cloud service somehow disappears, the device should still run with basic functionalities.

    • Harley
      November 21st, 2016 at 19:50 | #12

      Yes what happen to Revolv is one reason, when manufacturer make the cloud service end-of-life, the hardware you bought can no longer be used. Its like you did not buy hardware at all but subscibed to an online service.

      I also think that you should always be able to control all relays (including all light and power switches) and read all sensors on your local network even when your internet is down.

      When you think about it, it just sounds stupid that you are today with these type of architecture not able to turn your lights on/off at home if your internet is down even when you yourself are at home and connected to the same local wifi as your devices.

      The dependence on cloud services for IoT devices that always require internet connection to work woukd make a nice blog post on its own I think.

  12. Rebel
    November 22nd, 2016 at 00:14 | #13

    I really like to try something like this: https://bitbucket.org/xoseperez/espurna
    but I have no experience with ESP8266-based hardware. What do I need to connect it with a WIN-PC, I hink this needs a 3,3V USB-Adapter-“Interface”. I prefere a easy and cheap way, otherway I sell my sonoff devices to ebay.

  13. November 22nd, 2016 at 09:56 | #14

    You need to solder a 4-pin 2.54mm header to the board, then connect a USB adapter (not sure 5 or 3.3V yet). This type of USB to TTL debug board costs about $1 or $2. You don’t need a windows PC, there are tools to flash firmware from Linux.

    I’ll write something about this in 2 or 3 weeks.

  14. rebeL
    November 22nd, 2016 at 20:17 | #15

    OK, thanks. I will wait patient.

  15. Tesla
    November 23rd, 2016 at 16:11 | #16

    Something is wrong with your div.crayon-toolbar.
    When the mouse pointer is over the code, then the toolbar drops down and hides the frst line of the code.
    It is really annoying.
    Can be done something about it please?

  16. November 23rd, 2016 at 16:53 | #17

    I’m not sure if there’s an option for that. Normally, you can click in the zone, and the toolbar will disappear.

    Edit: Yes, there’s an option for that. I’ve changed it. Let me know if it works for you.

  17. paul
    December 4th, 2016 at 22:38 | #18

    Am curious as to what the small green cube item in your 2nd photo down is, with the long USB cable… some type of power adapter ?
    The USB cable looks maybe 2M – do you know if for sale on Aliexpress ?

  18. December 5th, 2016 at 16:19 | #19

    It’s a power adapter. I think I got it with my Arduino clone -> http://www.cnx-software.com/2013/07/25/borderless-electronics-sells-9-arduino-compatible-starter-kit/

    The flat yellow cable is nowhere near as long as 2 meters, probably just one meter, or 80 cm.

    You can find many of this type of power supply for $1 on eBay -> http://www.ebay.com/itm/Mini-USB-5V-1A-Home-Travel-Wall-Charger-Power-Charging-Adapter-US-plug-for-Apple-/311487926897

    There are flat micro USB to USB cable 6 feet (almost 2 meters) on eBay too -> http://www.ebay.com/itm/6FT-Micro-USB-Flat-Noodle-Charger-Cable-Cord-for-HTC-Samsung-Galaxy-S4-S3-Note-/161760143846

  19. Freezing
    December 8th, 2016 at 21:21 | #20

    There is a bug of the ewelink 2.5.1, so you can not use the email to register ,and your device will connect to China server and not the US server.. now has been fixed in .. Please update your ewelink app , register a new account with email, delete the device from your phone account and add it again into your email account, them it will contact the US server.

    Next version ewelink will support facebook account to login so you don’t need to register a new account.

  20. Freezing
    December 8th, 2016 at 23:26 | #21

    New firmware and ewelink will support controling the device without the internet and don’t need the cloud server! It will support LAN control, just connect the sonoff and ewelink to the same wifi router , then you can control the sonoff without internet and cloud server

  1. No trackbacks yet.