Archive

Posts Tagged ‘itead studio’

Some Sonoff TH16 and Sonoff POW Manufactured in December 2016 / January 2017 Are Being Recalled

March 1st, 2017 29 comments

Sonoff TH16 and Sonoff POW are inexpensive and useful wireless switches based on ESP8266 WiSoC. I’m using Sonoff TH16 to control a water pump, and Sonoff POW to monitor my office power consumption. I received mine in early November 2016, so they were probably manufactured sometimes in October. Does manufacturing month matter? Yes, it does, as ITEAD Studio has just issued a recall notice for both devices for a batch manufactured between December 2016 and January 2017.

The problem is that while the switches come with a 16A relay, the trace were not thick enough, and if you connect a device that draws enough power, they would heat enough to make the case slowly melt. The problem is that ITEAD Studio asked the factory to add “sufficient tin to the wires to ensure low enough impedances”, but they did not quantify it clearly, and that’s why the product is not safe to use.

If you are affected, the good thing is the company owed their mistake, and you can contact the company for a refund or replacement by providing a photo or video, as well as your order number. What they did not say exactly is how to identify a product with the defect, that is before it melts…

In order to avoid the issue in the future, they’ve “optimized their workflow”, and “are seeking for better solution to improve the product during the manufacturing process”.

Karl’s Home Automation Project – Part 1: Home Assistant & YAML, MQTT, Sonoff, and Xmas Lights

February 27th, 2017 24 comments

Karl here. I am here to write about my home automation project. First thing I want to say is that I am very cost conscious and I don’t mind putting in extra effort into the setup of things to keep costs down. I did invest a lot of time and had to do a lot of reading to get my project going. It took while and I received a lot of groans from my wife while testing. I am still in the process of tweaking things.

I started watching a series of videos on YouTube from Bruh Automation. He introduced me to Home Assistant. It got me really excited. He uses a Raspberry Pi as a server but I already had a Wintel Pro CX-W8 Smart TV Box which I use as a server. I run 3 Minecraft Servers, Emby Server, iSpyConnect DVR (2 IP Cameras), Unifi wifi controller, and now MQTT Server, and Home Assistant. Below is screenshot of mostly idle.

If it weren’t for iSpy it would be around 5-10% most of the time. Emby transcoding is the only thing that is stressful and it is not used much. The reason I mention this is because after purchasing a Raspberry Pi with power supply and case, you are not far off from getting a z8300 box. Only downfall is dreaded Windows update auto reboot. I finally looked into it and disabled it. If you decide to use a Windows box, I would make sure you are running 64bit windows. One advantage to using a Raspberry Pi is there is an image on Home assistant with the basics pre-configured and just need to write it to an SD card.

Server side Setup

I won’t go into too much detail on server side, as I installed Python, Mosquitto, and Home Assitant (I followed the guide on their site for Windows)

Python was a breeze to install and just ran the executable and went with defaults. I already had it installed for something else and I am running 3.5.2 64-bit. There are newer versions now. Mosquitto was the most difficult. I followed this guide but substituted Win32OpenSSL_Light-1_0_2j.exe approx 2MB. A k version is available now. Home assistant was easy and used pip.

Christmas Lights

It was a little before Christmas when I started researching home automation. I had been reading about these inexpensive Sonoff devices here on CNX and I found a project on Github for some custom firmware by arendst that enabled them to be controlled by MQTT. (While getting the link it looks like a new project has started with some additional features here). My wife really likes decorating for Xmas and we have 3 trees and lots of lights. She mentioned getting some timers and boom I had my opportunity and ordered them the same night. After receiving It took me a couple nights and I had a simple automation turning Xmas lights on and off at specific times and life was good. I got an extra one to play with until Xmas was over. I redeployed the rest  around the house after Xmas.

MQTT

I really had no idea what this was and it took me a while to grasp. You can use a cloud based MQTT if you would like, but I prefer to run my own. MQTT is a service that relays messages between devices. There are 2 main items topics and payloads. To be able to tell a switch to turn on you send payload “on” to a topic, for example, “cmnd/testbench/power”. The light turns on and it replys back to a topic “stat/testbench/POWER” confirming that the light is on and the message is received. Because we are sending “on” to the topic each device using MQTT will need its own topic. Topics are case sensitive. I made a batch file to subscribe to all topics for troubleshooting so I could monitor the messages. The # indicates all sub topics.

Sonoff

I picked the Sonoff basic but there are also different varieties that add additional features which are supported by arendst software.

Arendst  has been very active with this project and adding/tweaking daily. When I first flashed the device, I did find a defect and notified him and he had it fixed and uploaded within the hour. He has very detailed instructions on the Wiki. First step before flashing is soldering headers. (I link to bent headers…which I initially thought I made a mistake but turned out it was good. They are easy to straighten) A USB to TTL adapter is also needed to upload from Arduino IDE. I recommend one like this because it provides both 3.3 and 5V.  After downloading and setting Arduino up, I only set my WiFi password and SSID in the sketch. After it boots the first time, it connects to your wireless network. Find the IP address in your router, and pop the IP address in your browser to finish the configuration. Set the MQTT server credentials and topic and your done. I never setup credentials on the MQTT server so it accepts any login. Finally after everything is programmed you need to connect it to mains. Beware do not connect mains while TTL is connected.  I bought some extension cords locally. Cut them in half and stripped back a ¼ inch of the insulation. Extension cords use stranded wire so I tinned them with solder to avoid any stray strands from shorting out. Then I screwed them down on the terminals making sure polarity was correct.

Click to Enlarge

YAML

YAML is unforgiving. It is the formatting that you configure Home Assistant in. A single space will stop Home Assistant from starting. Luckily on this last update if you restart Home Assistant through the browser it will test the configuration file before actually restarting. I purposefully put an extra space on line 54 to show it is easy to find any mistakes.

Click to Enlarge

I also recommend Notepad++ for editing in windows. You can break your configuration down into different files but I like one. Notepad ++ allows you to collapse the parts you aren’t currently working on.

I recommend adding one thing at a time and restarting to make it easier to find errors. And making a copy of the last working config before adding more. In the config below there are 5 sonoff’s and an automation to turn the lights on and off at specific times. This is extremely basic. I also recommend setting up one new device and be conscious of naming. When you get your config working properly on your first new device I copy the config to a new blank text window and do a find/replace.

Below is the screen capture of collapsed parts, and and full config (minus personal info).

Notice the test bench is on later firmware and the MQTT topic is slightly different

Next Steps

So now I have a smart home, right? Not in my opinion. I can turn lights on and off with a schedule or with my smart phone or at the light by pressing the button on the Sonoff. To me this is not smart. Setting a schedule is OK, but then you have the lights on unnecessarily and wasting electricity. Only real option is to press a button on the Sonoff but what difference is that than flipping a switch. Taking your phone out takes way too long, and I feel like it is going backwards. Below are estimated costs so far. By far the Windows Box will be the most expensive part if you choose to go that way. You can re-purpose just about anything that runs Linux to be a server. One other option is to run Linux on an S905x.

Money Spent

Cost of server not included nor shipping.

Item Qty Price Total
Sonoff Basic 5 $4.85 $24.25
Headers 1 $1.50 $1.50
USB to TTL 1 $2.54 $2.54
Total $28.29

If you find this entertaining or want me to go more in depth on a specific aspect let me know in the comments. I have been finding my time setting it up very satisfying. I am able to do some hardware and software work. I hope this might get your interest in home automation going, and find out it is not hard nor expensive. I would like to state none of the products linked were provided by the sites. I purchased with my own money.

The plan right now is to do a 3 part post. In the next post, we will integrate some inexpensive motion sensors and door sensors using 433mhz, then finally modifying the sensors to include a light intensity sensor.

Sonoff SC WiFi Environmental Monitor mini Review

December 21st, 2016 6 comments

Yesterday I received two environmental monitors with Sonoff SC and vThings CO2 Monitor, and while I’ve plugged both, I have not had time to look into vThings documentation, but since I’m already using eWelink app for Sonoff TH16 wireless switch, setting up Sonoff SC just took me a few minutes, so I’ll report my experience with the device in this review.
sonoff-sc-usb-power-modemI powered Sonoff SC using the USB port of my modem router, and the green LED on the back of the device started to blink every 2 or 3 seconds. Then I started eWelink app in my Android phone and taped on the “+” icon to add a new device following the instructions here which are basically the same for all Sonoff devices.

Click to Enlarge

Click to Enlarge

Then you need to press the “Audio” button for about 5 seconds until the green LED blinks faster, at which point you can click Next, configure connection with your WiFi router, and complete registration by giving it a name, such as “Air Quality Monitor”.

Click to Enlarge

Click to Enlarge

Now Sonoff SC will show with your other Sonoff devices in eWelink app and show the air quality level, temperature, humidity and noise level. You can click on the > button to get to the prettier representation of the data as shown on the right screenshot above. The data was matching reality as the temperature was about 20 C at the time, and since it was still early morning, humidity was high.

Click to Enlarge

Click to Enlarge

I started to talk a bit loud, and Noise level changed from quiet to Normal. However when I turn on a headlight in to the top of the device, the light intensity was still at dusky… I took at screenshot a little later in the morning and the temperature had risen to 24°C, while humidity lowered to 53% normal, matching reality although probably not perfectly accurate due to the sensors used (e.g. DHT11).

Beside reporting data to the app, Sonoff SC can also be used as a smart hub to control other Sonoff devices. You can create “Scenes” by tapping on “…” icon in “All Devices” window, and add a condition (trigger device) using sensors from Sonoff SC or other Sonoff devices shown as “Air Quality Monitor” and “Water Pump” in the screenshot below.

Click to Enlarge

Click to Enlarge

However, while I could setup a trigger device (Sonoff SC) using the temperature data, I was not able to add an “Execute Device” despite having a Sonoff TH16 wireless switch registered with eWelink app and set in manual mode (e.g. not using external sensors to trigger it on or off).  I have a temperature and humidity sensor attached to Sonoff TH16, so maybe that’s why.. It might be only categorized as a “trigger device” and not an “execute device” despite also coming with a relay. I’ve contacted the company to see if there’s a solution.

Sonoff SC is sold for $19.99 + shipping directly on ITEAD Studio.

How to Use Sonoff POW ESP8266 WiFi Power Switch with MQTT and ThingSpeak

December 11th, 2016 11 comments

ITEAD Studio’s Sonoff is a family of cheap home automation products based on ESP8266 WiSoC, and I’ve already tested Sonoff TH16 wireless switch with a humidity and temperature sensor using the stock firmware and eWelink app for Android or iOS. It works, but up to recently it required a registration to a cloud service (the company will now allow use from the local network), and the source code is closed. So for the second device under review, namely Sonoff POW wireless switch with a power consumption monitor, I decided to install ESPurna firmware working on ESP8266 Sonoff devices and NodeMCU, as it’s open source, supports Sonoff POW natively, includes a web interface to control the device from the LAN, and includes an MQTT client.

MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe messaging protocol used to control IoT sensors and devices, and it’s a popular method to gather data from client to a MQTT broker to push the data to the cloud or a local database.

iot-sensors-mqtt-cloud

So typically, you’d have a bunch of sensor nodes (like Sonoff devices) communicating over MQTT to an MQTT Broker in your local network, which could be an OpenWrt router or a Linux development board like a Raspberry Pi, which in turns gets the data the the cloud to services like AWS IoT, Xively, or ThingSpeak. It’s also possible to use Cloud services to control MQTT devices remotely through the MQTT broker.

I eventually plan to use NanoPi NEO board to run both MQTT and ThingSpeak locally (not connected to the cloud) in order to monitor the power consumption of my small office, but since I’m all new to this, I’ve started experimenting by connecting a 30W light to Sonoff POW, and use a desktop computer running Ubuntu 16.04 for MQTT and ThingSpeak.

sonoff-pow-connection

Click to Enlarge

Click to Enlarge

Since I’ve already installed ESPurna firmware to the device, I disconnected the USB to serial board (important since Sonoff POW board has a hot ground), and connected it to the mains (220V in my location). That means we already have an MQTT client which first I had to configure.

Click to Enlarge

Click to Enlarge

Since it was the first time I connected a load to the device, I went to ESPurna’s status menu to check power usage was reported, and my 30 Watts light bulb was drawing 27 Watts. Close enough. I changed the hostname to sonoff-office, and setup two SSID in order to connect Sonoff POW to my local network in client mode, instead of using it in Access Point mode by default. You’ll need to tap on Update each time you modify the settings. Since the SSID must be entered manually, please note that SSID are case sensitive, e.g. CNX-SOFTWARE is different from cnx-software.

Click to Enlarge

Click to Enlarge

I wanted to calibrate the power using the 30W light bulb, so I entered 30W in AC RMS Active Power field, and tapped on Update, but the web interface reported “no changes”. I’m not sure how to use that part. Finally the most important part for this tutorial is to set the MQTT settings with MQTT IP address, and leaving other fields unchanged. However, you can change MQTT Topic field for example replacing /test/switch/{identifier} by /myiotstuff/{identifier}.

Now that our MQTT client is configured, I need to install mosquitto MQTT broker in Ubuntu:

mosquitto-clients is not really needed, but I’ll use it to test the MQTT broker a little later. Once you installed it, the MQTT Broker should already run automatically.

The last line of the log above shows a client connection from Sonoff POW. Now, we need to check the topic, and since ESPurna documentation is still work in progress, you could either check out the source code, or IMHO more fun, capture MQTT packet with tcpdump or Wireshark as I’ve done below.

Wireshark MQTT Capture - Click to Enlarge

Wireshark MQTT Capture – Click to Enlarge

Here we can see that Sonoff POW will send a Publich Message with the power level using the topic “/test/switch/sonoff-office/power29”.  “/test/switch” is the string we’ve defined in the web interface, “sonoff-office” the hostname we’ve given to Sonoff Pow, and “power29” indicates 29 Watts of power is currently used.

We can also start a client in Ubuntu 16.04 terminal window to check more MQTT topics with # wildcard for sonoff-office host:

We can use MQTT to get the IP address, firmware and file system version, hearbeat message, power use, and relay status (on or off).

It’s all good, but now we need to do something to draw the data, and possibly analyze it. I selected ThingSpeak for this purpose since it can be installed in the local network, or through their service in the cloud. By the end of my testing, I’ve noticed ThingSpeak has a new MQTT API, meaning it should be possible to connect your MQTT broker directly to it, but for this guide I use mqspeak instead as a bridge between MQTT and ThingSpeak. It may still be useful, as the open source version of ThingSpeak is not updated anymore, and lacks the MQTT API.

You’ll need Python 3 and pip3 to install mqspeak:

Once it’s done, we’ll need to create a config files as explained on mqspeak’s github repo, and I created /etc/mqspeak.conf with the following content:

Brokers are used to configure MQTT broker IP address and port, as well as the topic(s) to subscribe to, while Channels take care of ThingSpeak configuration with the channel’s Id and write API key, update rate in seconds (15s minimum), update type (see github for details), and fields defined in your ThingSpeak’s channel(s), which will create later on. I wrote one broker for the power consumption topic, and other for the relay status. However, I eventually ignored the relay status, as it’s not updated often enough and cause ThingSpeak’s channel to only be updated when the relay changes status, even if there are power updates in the meantime. A workaround is to use two different channels for ThingSpeak.

mqspeak connects directly to api.thingspeak.com, so if you are using ThingSpeak cloud services, the next step is to register an account and setup one or more channels.

Extra Instructions for a local installation of ThingSpeak

However, if you’ve installed ThingSpeak in Ubuntu 16.04 or other Linux operating systems locally or on your own server, you’ll need to change the server in the source code, and reinstall mqspeak.

  1. Get the source code:
  2. Modify mqspeak/sending.py to replace api.thingspeak.com using HTTPS with localhost (or other IP address/URL where you’ve installed ThingSpeak) with HTTP:
  3. Install mqspeak

An improvement would be to install a signed SSL certificate, like the one offered by LetsEncrypt and configure the rails server to use https instead. I have not setup ThingSpeak server to start automatically yet, so I have to start it manually for now:

End of instructions specific to local installation.

The instructions specific for the local installation of ThingSpeak are now done, and all instructions below are valid for both the local installation and cloud service. Now open a web browser, go ThingSpeak (cloud or local), and click on “Get Started Now” in order to register an account.

Click to Enlarge

Click to Enlarge

Once it is done, login and click on “New Channel”.

Click to Enlarge

Click to Enlarge

Give it a name, a description, create fields as needed, for example power-consumption and power-status, and click on Save Channel.  Update /etc/mqspeak.conf accordingly with the fields’ name, and channel Id.

thingspeak-api-keyNow select API Keys tab to copy and paste the write API key into mqspeak.conf.

Now we can start mqspeak:

ESPurna firmware will send a power update every 60 seconds (this can be changed in code/src/pow.ino), so you’ll see a new message pop-up every 60 seconds with your channels Id and write API key. I’ve let it run for about one hour, and got the follow chart in ThingSpeak after turning on and off the lights from time to time.
thingspeak-power-consumptionThat’s pretty cool, so it only shows the current power in watt, and we’d probably want to get power consumption in kW/h per day, week and month at some time, and I have yet to study how to do that, Exporting the data to excel would be a workaround if this can not be handled in ThingSpeak. ThingSpeak.com (but not the open source version) offers some Matlab processing of the data, so that’d be another options.

The next steps would be to install MQTT and ThingSpeak in NanoPi NEO board, enable HTTPS in ThingSpeak, autostart rails server and mqspeak at boot time, make ESPurna firmware publish the “Power” topic more often than every 6 second, and find some way to generate useful kW/h consumption charts from the data stored in ThingSpeak within ThingSpeak, or but exporting the data.

Sonoff SC WiFi Environmental Monitor Sells for $20

December 9th, 2016 8 comments

ITEAD Studio has done some good job with their Sonoff home automation devices based on Espressif ESP8266 WiSoC, as they are affordable, working as advertised with default firmware, and hackable with our own. The company has now added a new model, which may or may not be as useful, with Sonoff SC environmental monitor device that detects current temperature, humidity, light intensity, air quality, and sound levels, and sends the data to eWeLink app installed on your Android or iOS phone.

sonoff-scYou may be thinking it can be used as a home assistant since it really looks like a speaker and include some sort of microphone, but the top of the device is probably used to let air and dust go through.

Sonoff SC specifications:

  • WiSoc – Espressif ESP8266 Tensilica L106 processor @ 80/160 Mhz with WiFi
  • MCU – Atmel ATMega328 MCU
  • Connectivity – 802.11 b/g/n WiFi
  • Sensors
    • Sharp GP2Y1010AU0F dust sensor to detect smoke and dust
    • DHT11 temperature and humidity sensor; Range: 0 to 100 °C
    • GM5528 photo-conductive resistance
    • Electrat condenser microphone
  • Power Supply – 5V via micro USB port

esp8266-environmental-monitor

The default firmware and eWelink app also allows to set various smart scenes to trigger other devices, for example if air quality is too poor, Sonoff SC could turn on a fan connected to Sonoff wirelesss switch, or lights could be turned on when it becomes dark.

eWelink Environmental Report Card

eWelink Environmental Report Card

But you don’t have to use the default firmware if you don’t want too, as Sonoff SC’s ATMega328 controller can be programed through the Arduino IDE if you want to change the firmware, while ESP8266 is used for WiFi only. You’ll find the schematics (PDF) and Arduino code in the Wiki.

If you are interested, you can purchase Sonoff SC for $19.90 on ITEAD Studio website with shipping adding $7.33 to $17 depending on selected shipping method.

How to Build and Flash ESPurna Open Source Firmware to Sonoff POW Wireless Switch

December 3rd, 2016 35 comments

ITEAD Studio Sonoff family is comprised of various inexpensive ESP8266 WiFi power switch, and the company sent me two of their latest CE certified models with Sonoff TH16 + external temperature & humidity probe, and Sonoff POW to measure power consumption. I checked the hardware is the first part of the review, and used Sonoff TH16 to control a water pump with the stock firmware and Ewelink Android app in the second part. It works reasonably well, but it relies on the cloud, so if you lose your Internet connection or the service closed, you can’t control the relay manually anymore. Luckily, the UART pins are exposed on Sonoff switches so you can solder a 4-pin header and connect a USB to TTL to flash your own firmware.

Click to Enlarge

Click to Enlarge

Please don’t connect Sonoff devices to the mains when programming them, it’s very dangerous, instead the USB to TTL board will power the system, and will allow you to program the board safely. Later you’ll be able to update the firmware, if needed, over the network.

The next step is to select the firmware you want to use, and I’ve been advised two firmware for ESP8266, namely ESPurna specifically designed for Sonoff devices, and ESPEasy with a larger community of users. The latter may be usable to control the relay, but it has yet to support HLW8012 chip used to measure power consumption in Sonoff POW, so I decided to go with ESPurna.

That’s the description of the firmware from its bitbucket page:

ESPurna (“spark” in Catalan) is a custom C firmware for ESP8266 based smart switches. It was originally developed with the ITead Sonoff in mind. Features:

  • Asynchronous WebServer for configuration and simple relay toggle with basic authentication
  • Communication between webserver and webclient via websockets with secure ticket check
  • Flashing firmware Over-The-Air (OTA)
  • Up to 3 configurable WIFI networks, connects to the strongest signal
  • MQTT support with configurable host and topic
  • Manual switch ON/OFF with button (single click the button)
  • AP mode backup (double click the button)
  • Manual reeset the board (long click the button)
  • Visual status of the connection via the LED
  • Alexa integration (Amazon Echo or Dot) by emulating a Belkin WeMo switch
  • Support for automatic over-the-air updates through the NoFUSS Library
  • Support for DHT22 sensors
  • Support for the HLW8012 power sensor present in the Sonoff POW
  • Support for current monitoring through the EmonLiteESP Library using a non-intrusive current sensor (requires some hacking)
  • Command line configuration

I could not find firmware release for ESPurna, but Xose Pérez – the developer – has provided some basic instructions to build and flash the firmware to Sonoff. Those are not really detailed however, and it took me nearly a full day to successfully build and flash the firmware to Sonoff POW, mostly because I was not quite familiar with most of the tools used. So I’ve reproduced the step I went through in Ubuntu 16.04, and hopefully this can help people getting things done more quickly.

Let’s retrieve the source code, and enter the code directory first:

You can build the project with PlatformIO or the Arduino IDE. The instructions are for PlatformIO so that’s what I used. There are two ways to build the code with the project wither using Platform IDE for Atom and the command line, or simply using the command line. With insights, I ended up using the command line, but I’ll show both methods.

Setup PlatformIO IDE for Atom to build ESPurna

First, you’ll need to download PlatformIO IDE for Atom for your operating systems, and install it. For Ubuntu 16.04, I selected “Download .deb” for Linux and installed it through through Ubuntu Software program. Alternatively, after download, you can install it from the command line:

You can now start Atom program in Ubuntu dash, select Open Project, and browse for espurna/code directory to load the project we’ve just gotten from Bitbucket.

Click to Enlarge

Click to Enlarge

The tick button on the top left corner is to build the project, and the right button just under is to upload the firmware to the target board. But if we try to build the firmware now it will fail with an error about “espressif8266_stage”. That’s because we need to install Espressif 8266 (Stage) development platform. First we need to enable Developer mode in the IDE by going to the top menu to select PlatformIO->Settings->PlatformIO IDE, and checking “Use development version of PlatformIO“.

platformio_core_enable_development_version

Click to Enlarge

Now install PlatformIO shell commands from either a system Terminal, or PlatformIO IDE terminal (PlatformIO->Terminal->New Terminal):

and finally install ESP8266 development platform:

At this point you can click on the tick icon to build the default “node-debug” environment, a build output window will show in the IDE, and quickly disappear if the build is successful.

Setup PlatformIO via Command Line to Build ESPurna

If instead we want to use the command line we can install the latest pip version, the developer version of PlatformIO, and the staging version of ESP8266 development platform:

You can check the build environment is set properly by running the following command in espurna/code directory:

It will automatically download, build and install all dependencies and build for “node-debug” firmware for NodeMCU board. If it is successful, it will end as follows:

So I find the command line option much more easy and straightforward.

Build ESPurna for Sonoff POW

However, we are not using NodeMCU board here, but Sonoff POW, and there are two environments defined just for that:

  • sonoff-pow-debug – Build firmware to flash over serial
  • sonoff-pow-debug-ota – Build OTA firmware to upgrade the firmware over the network

The parameters for each environment are all defined in platformio.ini. First we need to build sonoff-pow-debug environment:

But it failed with an error message related to hlw8012 library:

I reported the issue on Bitbucket, but the main developer could not reproduce the issue. Eventually I found out that it could be a PlatformIO bug, as the system does not recursively checking for includes outside of main.ino. So I added <hlw8012.h> in the main.ino as follows:

and the build could complete:

Since we’ve already changed the code, you may also consider changing “#define ADMIN_PASS  fibonacci” in code/src/config/general.h to use a different default password. The password can also be changed in the web interface, but this makes sure you won’t have a device somewhere with the default password common to most users.

Flashing Firmware to Sonoff POW

Now that we’ve made sure the firmware could build, it’s time to flash it to the device.

First we need to setup some udev rules to allow flashing over serial:

Now connect the USB to TTL to a USB port on your computer, press the button (connected to GPIO0) on Sonoff POW for several seconds until both LEDs are off to make sure you are in bootloader mode, and start flashing with:

Success! Great. If you have your own firmware to flash it may be useful to know the actual command used to flash the firmware was:

Building and Flashing the ESPurna filesystem

Wait! We’ve just flashed the firmware, isn’t it all? Nope, as the webserver files are stored in another partitions, and compressed in a single index.html.gz file for better performance. The exact reasons why are further explained here.

We’ll need Node.js and gulp command line client:

Now inside espurna/code folder , we can check if building the file systems works with two commands:

Here my successful gulp attempt:

Finally, we can run the following (which also runs the two commands above) to flash the file system to the board, after entering bootloader mode by pressing the button:

Now that’s done.

A Quick look at ESPurna Web Interface

ESPurna firmware and filesystem has now been flashed to Sonoff POW. But does it work?

Click to Enlarge

Click to Enlarge

I can see a new SONOFF_POW_XXXXX access point, so that does look good. I can connect using the default password “fibonacci”, then go to my web browser to access http://192.168.4.1, and login again with admin/fibonacci credentials.

Click to Enlarge

Click to Enlarge

ESPURNA 1.03 interface goes to the status menu first, and there I can turn on and off the relay remotely, and check the power consumption in watts, which remains at 0 watt since I have not connected it to the mains yet. It’s also possible to turn the relay on and off with the button, and there an option to select whether to turn on or off the relay at boot time, which is great since I need it on at all times.

The web interface also allows you to change general parameters including the hostname and password, as well as enable or disable the HTTP API (disabled by default). The WiFi section is used to connect to up to 3 wireless routers, the MQTT section lets you configure an MQTT (Mosquito) broker, and the power section is used to calibrate the power monitoring device.

If you just intend to check the current power consumption and turn on and off the switch with your phone, you don’t have to do anything else. But I’d like to find some ways to draw daily, weekly, monthly charts of my office power consumption using either MQTT or the HTTP API. I’ll have to study how to do that, and that will hopefully be the topic of my next post about Sonoff POW.

Sonoff POW can be purchased on ITEAD Studio for $10.50 plus shipping, but is currently out of stock with the company manufacturing a third batch soon.

LeMaker HiKey 96Boards Board Sells for $29.70 (Promo)

November 29th, 2016 18 comments

[Update: The promo is over back!]

You’d think Cyber Monday should be over by now, but ITEAD Studio still has a clearance with real 70% discount, as 96Boards hardware compliant LeMaker Hikey board is now sold for just $29.70 instead of the usual $99 price.

96boards-discountA quick reminder of the specifications:

  • SoC – HiSilicon Kirin 620 octa core Cortex A53 processor @ 1.2 GHz with ARM Mali-450MP4 GPU
  • System Memory – 2 GB LPDDR3 @ 800 MHz
  • Storage – 8GB eMMC + micro SD slot
  • Video Output / Display – HDMI up to 1080p, MIPI-DSI interface
  • Connectivity – 802.11 a/b/g/n Wi-Fi, Bluetooth 4.1 LE (WL1835MOD module)
  • USB – 2x USB 2.0 host ports, 1x micro USB OTG
  • Camera – MIPI CSI interface
  • Debugging – UART header), unpopulated 10-pin JTAG header (back)
  • Expansion headers
    • 40-pin LS (Low Speed) Expansion connector – UART, I2C, 12x GPIOs, SPI, PCM, PWM, SYS_DCIN, 1.8V, 5V, and GND,
    • 60-pin HS (High Speed) Expansion connector – SDIO, MIPI_DSI, MIPI_CSI, I2C, USB 2
  • Misc – Power button, jumper for power/boot/user, LEDs for Wi-Fi/Bt, and 4x User LED
  • Power Supply – 8-18V @ 3A as per 96Boards specs via 4.5/1.7mm power jack. Hi6553V100 PMU
  • Dimensions – 85 x 55 mm

You’ll be able to run Android and Debian images provided by Linaro. The board is also one the rare development board to be officially supported by AOSP.

You may also be interested in LeMaker Guitar quad core ARM Cortex A9 board sold for $13.50 with 1GB RAM.

Thanks to Nanik for the tip.

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

November 18th, 2016 21 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.
voltage-tester
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.
sonoff-th16-closed
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.