Sonoff is very popular for DIY home automation, but you can make it even better by loading new firmware onto it. Learn how to connect a USB-to-Serial converter to ANY model of Sonoff, even ones that haven’t been released yet!
This episode isn’t just a series of steps to follow: it’s a tutorial on how to reverse engineer the connections to the ESP8266 / ESP8285 microcontroller in an unknown circuit, and put it into bootloader mode ready to receive new software.
The strapping pins set the boot mode of the microcontroller. Typical ESP8266 circuits have an arrangement similar to this:
In the Sonoff, GPIO15 is pulled low to 0V, and GPIO2 and GPIO0 are pulled high to 3.3V.
If GPIO0 is held low (0V) during startup, the ESP8266 will go into bootloader mode and be ready to receive a new program uploaded via the serial connection on the TX and RX pins.
You can often find the appropriate connections on the PCB by tracing out the pins on the microcontroller. The relevant pins on the ESP8266 and ESP8285 can be found in these locations:
With only a couple of exceptions, most Sonoff models include a 4-pin header on the PCB that exposes GND, 3.3V, TX, and RX.
You can use a USB-to-Serial converter to connect this header to your computer by USB, so that you can upload a new program. You can make an adapter cable using these connections:
It’s a simple cable to hand-wire, but that can be annoying so to save time I designed a simple adapter that you can use between a USB-to-Serial adapter that uses the common “FTDI” pinout and the Sonoff:
The adapter makes it very easy to connect to most Sonoff models. Make sure you use a USB-to-Serial adapter that runs at 3.3V because it’s not safe to connect 5V. Doing so could damage the ESP8266 microcontroller.
Over the last 12 months I’ve been working with identical twins Nick and Chris Fryer, who have Duchenne muscular dystrophy, on a variety of assistive technology projects. This is a field where home automation can have an enormous benefit and dramatically improve people’s lives.
Unfortunately Nick died recently, so Chris and I have been continuing his projects and also starting some new ones.
This is just a brief look at some of the things we’re working on. There will be far more technical detail in later videos.
Is home automation just about toys for lazy rich people? I don’t think so! It’s about using technology to extend our own capabilities and become just a little bit superhuman.
As humans we all have physical limitations. Some of us are healthy and strong, and some of us aren’t, but ultimately we all get older and find that it’s harder to do things that we used to do easily. I see technology as an enabler that can extend our abilities or help us overcome limitations in our bodies or our environment.
Way back in 2011 I did a conference presentation titled “Use The Force, Linus!” which was about using Open Source technology to give ourselves Jedi powers. Many of the examples I covered in that presentation were drawn from my home automation projects.
UPDATE:I’ve been asked by several people how they can get in touch with factories so they can go on tours. Please DON’T DO THIS unless you are serious about doing business with that company. Factories are not amusement parks, and the staff who work in them are busy doing their jobs. If you want to manufacture a product and you seriously want to do business with a factory, then it’s easy to arrange a tour. But don’t do it just because you want to have a look! That wastes their time.
ITEAD, makers of the super-popular Sonoff home automation devices, very generously allowed me to visit their head office, their factory, and their warehouse so that I could see how the Sonoff is made.
Come with me on a quick trip to China to learn a little about the amazing city of Shenzhen, see its enormous electronics markets, and go inside the Sonoff factory to watch them progress from a bare PCB to a finished product.
My home automation light switches have gone through a series of versions, starting with very complicated switches that all had Ethernet built in. Over time I’ve simplified the system so now the light switches themselves are electrically very simple: they’re just illuminated buttons on a breakout board with an RJ45 connector, and absolutely nothing else in them.
The switches connect to a pair of centralised light switch controllers over Cat-5 cable, so that it can detect when the buttons have been pressed and report events to MQTT.
In this episode I show some of the previous versions of my light switches, and then show how I built an Arduino based light switch controller.
There’s also a general introduction to the I/O breakout schema that I use at I/O Breakout. I’ll probably cover this in detail in a future episode because the same breakout shield will be used in other projects.
The light switches themselves are just illuminated buttons on a breakout board, mounted on a standard wall plate. The 4-button panel uses all 4 available data lines. The 3 and 2 button panels simply use fewer data lines. Click on the schematic for a larger version:
I didn’t spend much time in this episode explaining the current version of my light switches because I’m going to cover it in much more detail in the future. This episode is mostly about the controller.
I’ve been lucky enough to have worked with an electrician to totally rewire my house for home automation, so it works in a very different way to a normal house. This episode traces through how power arrives at my house, is distributed to a pair of sub-switchboards, and from there goes out to loads such as lights. It also covers the important pieces of the system including MQTT and OpenHAB.
General information about MQTT is available at the official MQTT site. The site doesn’t get many updates because the protocol standard itself is fairly stable and well established, but it’s a good reference site with links to many MQTT-related projects. See mqtt.org.
There are many MQTT broker implementations available, written in various programming languages and with different features and levels of performance. I use Mosquitto, which has been around for many years and has never let me down. Note that your MQTT clients won’t care what broker you use or what language it’s written in, provided it supports the features they need. Mosquitto is written in Java, but I typically connect to it from Arduino-based devices. See mosquitto.org.
OpenHAB is currently the main rules engine that I use, which also takes care of state management and provides an app for iOS and Android. I’m still running the v1.x release series, but v2 is out now which is a major rewrite. See openhab.org.
For a general purpose rules engine that communicates using MQTT, check out Node-RED. With a drag-and-drop editor based on Node.js, you can create rules right in your browser. I don’t currently use this, but I’ll probably replace my current home-brew rules engine with Node-RED some time in the future. See nodered.org.
Building a home automation system is a lot of fun, but there’s a serious issue you need to consider: what will happen when you’re gone? Will anyone else be able to figure out the system that you put together?
A couple of years ago, software developer Chris Yeoh passed away after a battle with cancer. Even with time to plan, his passing still left some systems in his house inaccessible to his wife and young daughter. His situation made me think long and hard about what would happen to my family if the same thing happened to me.
I have a couple of suggestions for ways to prepare your system for dealing with a future without you.
If you’re gone, and a random electrician is called in to figure out why the lights aren’t working, will they throw up their hands in disgust and walk away? The first thing they’ll do is open the switchboard to orient themselves, so put some obvious bootstrapping documentation right there at the switchboard.
When I first started taking electronics apart in the 1970s, it was normal for major appliances to have physical documentation inside them. If you take the back cover off an old black and white TV, it may have the complete circuit diagram glued inside for the benefit of service technicians. I’ve even opened up old TVs to find a complete factory service manual packed inside.
I keep a master document in Google Docs, and a hard copy on a clipboard hanging next to the main switchboard. Whenever I make a change to the cabling or anything else relevant, I write the changes in the hard copy. When a few changes have accumulated, I take the clipboard to my computer and edit my master copy. Then I print it out again, put it on the clipboard, and leave it with the switchboard.
My document contains:
Name and phone numbers of myself and my wife.
Name and phone number of our electrician.
WiFi SSID and password.
Name of my ISP, account ID, password, tech support phone number, and fault history.
Name of my cable TV provider, account ID, password, and fault history.
A plan of my house, with important locations (such as all switchboard locations) marked.
A definition of the labelling scheme for network connections.
A list of Ethernet ports and their uses.
A definition of the labelling scheme for power cables.
A list of relay outputs controlling loads in the house.
You can see a template version in Google Docs, and either duplicate it for your own use or download it as a Word doc or other format:
When you make something for your home automation system it’s usual to test the firmware, modify it until it’s working just right, and then walk away and forget it. The source code for your project is probably sitting in some obscure location on your computer, which nobody else can access. Or if they can access it, how do they know this particular Arduino is running the sketch called “SensorShield4MQTT” instead of the other one called “SensorInputsMQTT”? Or which versions of the libraries you compiled it with?
When I finish a project, I copy the source code and associated libraries onto a cheap USB memory stick or an SD card, and then put it inside the project box or attach it with a cable tie so that it always physically stays with the project.
Later, if I ever update the firmware, I also update the memory stick so that I know 100% for sure that it always contains the exact source code that is currently running on the device.
This has saved me hours of frustration on multiple occasions, and if anyone else ever wants to fix the systems I’ve built this should put a smile on their face.
I’d love to know if you have other suggestions! Please comment below.
Finally, please consider supporting a cause such as Free To Breath and help fight cancer.
Electric window motors allow your home automation system to open and close your windows as required. This could be useful for opening windows to allow ventilation, or to make sure all windows are automatically closed and locked when you leave home.
I recently got my hands on some AXA electric window motors that include a LIN bus interface so they can be linked to a home automation system, but I’ve never used LIN before so I needed to learn a bit about how it works. I designed my own LIN interface module that allows me to connect my laptop to LIN devices and manually send messages, and the module also allows an Arduino to control the window motors.
The LIN bus shield that I demonstrated in the video comes from some mysterious supplier in the Netherlands called MrX. I don’t know who designed it, but if you search for “HOME-LINBUS Arduino Shield” you may be able to find it on eBay or other places.
The flash memory chip in Sonoffs is 8Mbit, which is only 1MByte. Then if you want to do OTA (Over The Air) updates you need to limit your program size to less than half the available memory so that a new program can be uploaded alongside the old one. And if you use a SPIFFS (SPI Flash File System) to store non-volatile data outside your program, you lose even more memory.
You can replace the flash memory chip with a Winbond 25Q32FV in SOP-8 package, which is a 32Mbit (4MByte) chip. You can buy them on eBay for about US$3 for a pack of 10.
The original flash memory on the Sonoff is a Winbond 25Q08FV, which is the 8MBit (1MByte) version of the same chip.
Thanks to Pete Scargill for this idea! You can see more on his original blog post: “32Mb ESP01 and Sonoff”
Sure, you can just cut a power lead and screw it into a Sonoff, but it’s probably a bad idea. You need to consider how your Sonoff will be used, including physical protection (stop little fingers reaching the terminals!) and liquid protection from spills. You also need to make sure there is strain relief on the cables to prevent them being pulled out, and possibly exposing live mains connections. You can do it cheaply using a plastic project box, and with some cable ties around the cables just inside the box so they can’t be pulled out. The result can be a very neat setup that won’t look like a dodgy DIY cable, and should be safe for general use.
You can go even further and use an IP-rated (Ingress Protection) case and cable glands, to make your Sonoff waterproof and physically very strong.
In this video I gave a very simple explanation of the two-digit IP codes. There are also extensions to the code for other attributes. You can find more information and tables showing the specific meaning of the numbers at en.wikipedia.org/wiki/IP_Code.
Internet control is fun, but usually you also want some way to manually turn the output on or off without using your phone. You can modify a Sonoff to connect an external button across the pins of the built-in button, allowing you to toggle the output by pressing the button manually. The built-in button is connected to GPIO0, so when the button is pressed it pulls GPIO0 to GND. This is used during power-up to put the Sonoff into bootloader mode, and can also be used to toggle the output or do other actions.
Alternatively, you can connect an external button between GND and GPIO14 so that your software running on the Sonoff can detect when it has been pressed. Some firmware, including Theo Arends’s TASMOTA, supports this out of the box.
GPIO14 is exposed on the internal header used to upgrade the software on a standard Sonoff. There is already a pull-up resistor on GPIO14, so you don’t need any other parts. Just connect a button across the GPIO14 and GND pins, and you’re done!
Even better, TASMOTA has an option to support an external switch instead of an external button. The difference is that with a button, you want the output to change state each time you press and release the button. This means the firmware needs to treat both the button press and release (cycling from HIGH to LOW to HIGH) as a single event. But a switch just changes state (goes from HIGH to LOW, or goes from LOW to HIGH) and stays there because it latches in place. So your software needs to treat each level change as a separate event, and toggle the output. Once again, TASMOTA supports this out of the box.
As explained in the section about switches, the regular Sonoff exposes GPIO14 on the internal header that is used for flashing new firmware. The same header also provides GND and 3.3V connections, and the GPIO14 pin is provided with a pull-up resistor. This makes it super easy to connect anything that needs a single digital pin, such as an external switch / button or a one-wire sensor.
To make it even easier, the Sonoff TH10A and TH16A both feature a 2.5mm 4-way (TRRS) socket that is intended for connecting external sensors. The socket provides 3.3V, GND, and GPIO14: the same I/O pin exposed on the internal header of a normal Sonoff. That means you don’t even need to modify the board, you can simply plug in your sensor or switch externally.
The 2.5mm socket also has a 4th connection, but there are parts missing inside the Sonoff so it’s left unused. You can fix this easily by putting a solder blob across two pads on the Sonoff PCB, and optionally installing a pull-up resistor.
In the video I said that there is a 10k pull-up from GPIO14 to 3.3V. However, I didn’t notice that there are actually 2 pull-ups in parallel, so the effective pull-up on GPIO14 is 5k! You can still use a 10k pull-up on GPIO4 if you like, or you can use a 4.7k resistor if you want them to be about the same. 4.7k is common for I2C, but 10k is generally fine too.
There are many alternative firmware projects for the Sonoff. Personally I love Theo Arends’s firmware, but there are many others that may suit you better.