The Sonoff brand is a whole series os IOT devices that uses the esp8266 WiFi processor internally. The devices are cheap and are easily hacked. The simplest device is the Sonoff WiFi Smart Switch, which is a small device that can turn on or off 110/220 Volt 10 Ampere remotely. So you can do stuff like have your ceiling fan remotely controlled by your phone, or switch on/off anything that is connected to mains power. But the software that comes with it isn’t very flexible and can not be integrated to a home control system like NodeRED, OpenHAB or any other home automation system.
Fortunately the Sonoff devices are easily hacked, where you can put a better firmware like ESPEasy on it and integrate the device with any system. ESPEasy has an extensive list of extra modules that you can connect, like temperature, barometric pressure, humidity, light sensors, PIR sensors and a long list of other things.
But most modules connect with I2C, which is a serial interface for interconnecting ICs. Unfortunately, the Sonoff Switch only has 1 extra pin available, making it impossible to connect more advanced sensors. But there is a way of fixing that. Here’s how to add that to add an I2C connector to your $5 Sonoff and open up a whole field of stuff you can do.
Let’s upgrade the firmware!
To be able to upgrade the firmware, you have to open the enclosure and find the 5 holes in the center of the PCB and solder a female header for easy programming. As you can see in the picture, I’ve already soldered in a 5-pin header. (Click image to enlarge). I have marked the pinouts in the picture.
To be able to program the new firmware, you need an USB to TTL Serial Adapter. You need an adapter that is 3.3 volt compatible. Connect the 3.3 Volt on the USB adapter to the 3.3 Volt input on the Sonoff. Do the same for ground. RX on the adapter should go to TX, and TX on the adapter goes to RX. Yes, they are crossed over. Too young to remember RS232 and Null-cables you whippersnapper?
But don’t connect mains power! You should never have the Sonoff device connected to mains power when upgrading firmware! The USB Serial Adapter provides 3.3 Volts needed by the esp8266. Doing it wrong is a good way of getting a punk hairdo or maybe wings and a harp…
Now, all you have to do is to download the firmware and follow the instructions here to install it. The size of the memory on the Sonoff WiFi Switch is 1024k as standard. Either do the upgrade with the available memory, or maybe you want to increase the memory of your Sonoff? Read on…
Upgrading The Memory
If you want to make your Sonoff future-proof, there is a way of upgrading from 1MB of memory to 4MB. This is handy as the ESPEasy firmware evolves and get’s larger. Or maybe you want to write your own code and need the space? If you plan to do OTA, you need double the memory of the size of your program. You can do wireless updates without having to resort to using the USB to Serial adapter after the first install. This isn’t a must, because ESPEasy works with just 1MB, but it’s better to have 4MB.
I prefer to isolate plastics and other heat sensitive parts with heat resistant Capton tape. I use a hot air soldering station to remove the old memory IC, but you can use a soldering iron. Just fill the tip of the iron with a big blob of solder and heat all of the legs on one side of the IC at the same time while bending the chip up carefully with a screwdriver. Then you do the same thing on the other side.
The replacement IC is called Winbond W25Q32FVSIG and you can get 10 of them for around $3 on eBay. Make sure that the dot-marking indicating pin 1 is lined up correctly and solder on the new IC. Now you have a 4MB Sonoff! Naturally, if you flashed the firmware before upgrading you need to flash the firmware on the new memory chip.
Sonoff WiFi Smart Switch with I2C
Now let’s add I2C to the Sonoff Switch.
You’ll find the esp8266 IC on the underside of the PCB. We need to solder two small wires to the pins on the esp8266 to get I2C. Unfortunately the pins are not connected to anything on the PCB, so we need to solder the wire directly to the esp8266 IC.
The processor doesn’t have hardware I2C support, so you can use two free pins to connect, but usually (and in ESPEasy), GPIO4 and GPIO5 are used for I2C connections. Here I have soldered a thin wire to pin 24, which is GPIO5(SCL).
After you soldered the wire, make sure to glue the wire to the PCB. Hot glue works fine. This keeps the wire in place and doesn’t snap when routing the wire later. Then I soldered a wire to pin 16, which is GPIO4(SDA) and hot glued it to the PCB. You now have the hardest part done!
You really need a good magnifier glass or preferably a microscope designed for working with SMD components.
If you use thin wire, you can just take it up between the enclosure and the PCB. I added a 4-pin female header connector and soldered a 90 degree male header that I connect to the programming pins. Then I just soldered the 3.3 Volt, GND, the two I2C wires. Everything you need to connect a module using I2C.
Configuring ESP Easy
Now it’s time to try it out. After you’ve done the initial setup of the Sonoff device, select “Devices” in the setup menu. Then just add the sensor you connected to your Sonoff. In this case I have connected a BME280 Temperature, Barometric Pressure and Humidity sensor.
- Over The Air updates. A way of programming the esp8266 via WiFi instead of having to upgrade with a USB to Serial adapter cable. ?