In this article...
While out of the box they can integrate with Nest, Alexa, Google Home and even IFTTT, the reason that Sonoff devices are so popular with makers is that they're designed to be hacked - in a good way! Itead makes it super easy for developers to load their own firmware onto most Sonoff products, turning them into incredibly customizable devices.
And that's where this guide comes in. I'm already using several Sonoff devices as part of the home automation system that makes up our Smart RV. The Sonoff SV devices are particularly useful because they can switch 12V DC - which is what all the lights in our RV use.
Sonoff S31 Smart Plug
But today I'll be flashing the Sonoff S31 Smart WiFi Plug, one of Sonoff's newest devices.
What really sets it apart is the power monitoring. It can report, in real-time, on how much power is being used - as well as current, voltage and even power factor (useful for us given our power comes from an inverter with a VA limit). While that's interesting to many DIY-geeks, it's particularly of interest to me in our RV where we're often reliant entirely on our batteries and solar for power.
Previously I had been relying on the Sonoff POW R2 which also has this capability, but that unit is hardwired meaning I had to cut the plug off each device to connect it. With the Sonoff S31, it's as simple as plugging it in to a wall outlet!
But before we plug it in, I'll be flashing it with Tasmota, open source software written by Theo Arends and licensed under the GNU General Public License v3.0.
Tasmota unlocks full control over the Sonoff devices, but more importantly allows us to integrate them with our Smart RV home automation system, based on Home Assistant, MQTT and NodeRED under the hood.
For many people, keeping their data local and avoiding 3rd party cloud integrations is a matter of privacy. While that's true for us, it's not the whole story - when we're camping off-grid in our RV, we want to make sure our home automation systems still work even if we don't have internet connectivity.
I ordered two Sonoff S31 smart WiFi plugs from Amazon, and a few days later they arrived, each in its own grey box.
On the back of the box we can see the specifications - the Sonoff S31 supports up to 15 Amps at 120V AC. It can connect to WiFi via 802.11 b/g/n - no 5GHz support here.
The box is well packaged and simple - containing the Sonoff S31 smart plug itself, plus a small instruction manual and a piece of paper stamped to show it had been through Quality Control.
On the back of the Sonoff S31, we can also see the symbol showing it is UL Listed in both the US and Canada, unlike the Sonoff POW R2 (and others) which lacked this and had only the CE stamp. I'm really pleased to see the Sonoff S31 is now UL Listed and it gives me a lot of confidence when deploying it in my home!
One end of the Sonoff S31 is home to a small momentary push button that that toggles power on and off - this continues to work with the Tasmota firmware, but can be configured too if desired. There are also a couple of LEDs which indicate WiFi and power status.
Warning: Do not attempt to disassemble this device while it is connected to AC power. This blog post is for research purposes only and if you choose to act on anything herein you do so entirely at your own risk. If at any point you don't feel completely confident, STOP and consult a professional.
To flash the Sonoff S31 we first need to gain access to the PCB inside. The screws are hidden, and to get to them we have to remove some plastic covers.
First, pop the grey cover off the end. You may be able to do this with your fingernails, but I found it easier to use a small penknife.
With the grey cover removed, slide out the two white plastic pieces - they're fit into guiderails and should slide smoothly out, revealing three screws behind.
Using a small screwdriver, remove the three screws. I found them to be incredibly tight, so be careful not to strip the screwheads. I didn't see any evidence of thread lock though, so just take your time.
Once the screws have been removed, you can pull the PCB out of the white casing.
Here's a high-resolution image of the Sonoff S31 circuit board in case you're interested.
On on end of the device you'll see a daughter board, containing the pins needed to flash the Tasmota firmware onto it.
Connect Flashing Wires
In the corner of the daughter board, you'll see six GPIO pads. There are actually two pairs of RX / TX pins - you want the pair closest to the VCC pin.
There aren't any holes (or much space!) to solder headers on, so I chose to simply solder some short jumper wires onto the pads. Once it has been flashed the first time, it's easy to update the firmware over-the-air (OTA). Make sure you read the pins carefully - don't accidentally solder the ground on the 4th pin down like I did at first! Fortunately I spotted it quickly and moved it.
I used my Hakko FX888D soldering station to attach the wires - it's a bit messy, but was hard to do with a camera lens right in front of me!
For flashing devices via Serial over USB, I like to use my Moyina FTDI USB adapter. It has convenient headers and a toggle to switch between 3.3V and 5V. For this, make sure it is set to 3.3V.
Warning: If you connect 5V to the Sonoff S31 pins, you will damage the device.
Before you connect it to the Sonoff S31, make sure you have the drivers installed. Windows and Mac OS should both install them automatically when you plug it in, but you can download them from the FTDI Chip website if necessary.
Connect the FTDI USB adapter to the Sonoff S31 as follows (I've included the color wire I used to make it easier to interpret the pictures):
|Color||Sonoff S31||FTDI USB Adapter|
It's pretty straightforward - just map VCC and GND to their respective pins, and switch TX and RX.
Preparing for Flashing
To install the Tasmota firmware, you'll need to download the firmware itself and a tool to flash the firmware with. I personally prefer to use esptool, so that's what we'll use here. If you haven't already downloaded and installed esptool, you can follow the directions in the esptool GitHub repository.
To keep things simple, we'll be installing the "standard" Tasmota firmware - simply called tasmota.bin. There are lots of other variants too, but these are beyond the scope of this document. Download your chosen file to your computer.
For the purposes of this guide, I'll be using Tasmota version 7.2.0 and then I'll show you how to update to the latest version using an over-the-air (OTA) update later. You can skip this and go straight to the latest version, but it's good to know how to update via OTA for when you need it later.
The button in the middle of the board is the toggle button that was covered by the grey end cap. It is connected to
GPIO 0 and this button needs to be held down before plugging the USB adapter in, and for several seconds afterwards. This forces the device to start in serial bootloader mode.
Once you have installed esptool and downloaded the Tasmota firmware, you're ready to start.
Flashing the Firmware
While holding down the button on the Sonoff S31 connected to
GPIO 0, plug the FTDI USB adapter into a spare USB port on your computer. Keep holding the button down for several seconds - you should see some lights flashing on the FTDI adapter. Count to 5 seconds after plugging it in and you should be safe to let go of the button.
We'll be running a series of commands using the Terminal on Mac and Linux, or the Command Prompt on Windows.
The instructions below are from my Mac, but the process on Windows is similar. First you need to find out the name of the USB device. On Mac, we can do that using the command:
And look for one that looks similar to
/dev/tty.usbserial-AD0JKNK3 (the suffix at the end of yours will be different to mine). On Windows, you want to look in Device Manager for the device name which should be something like COM Port 5. You should replace
<port> with the name of your port in each of the following commands.
I always start by double checking that esptool can communicate with the device, by running the command:
esptool.py --port <port> flash_id
If successful, it will print data including the chip type and MAC address on the terminal. The device will then hard reset, but if you aren't still holding the button then it will have booted into normal mode rather than entering the serial bootloader. Don't worry - just pull out the USB plug, wait a few seconds, then hold down the
GPIO 0 button and reinsert the USB plug, counting to 5 before releasing the button.
You'll need to hold down the button and reset the device by unplugging it and reinserting it after each command, so you'll have plenty of chance to practise!
If you got an error on the screen, double check your wires are correctly attached and that the device name is correct in your command.
At this point, the Sonoff S31 is running Itead's original firmware. Before we replace it, you can back it up now if you want. I don't typically do this as I have no intention of using Itead's firmware, but if you want to then run the command:
esptool.py --port <port> read_flash 0x00000 0x10000 s31backup.bin
This will write the Itead firmware to a file called
s31backup.bin and reset the device.
Before installing the Tasmota firmware, we'll erase the flash with the command:
esptool.py --port <port> erase_flash
Don't forget to reset the device while holding down
GPIO 0 again.
The final step is to write the Tasmota firmware onto the device:
esptool.py --port <port> write_flash -fs 1MB -fm dout 0x0 tasmota.bin
You'll need to make sure that
tasmota.bin exists, or change the path to wherever you downloaded the Tasmota firmware.
If all went well, you've now flashed Tasmota onto your Sonoff S31. Nice work!
If it didn't work, then double check your wiring is correct and all your wires are soldered and attached securely. Failing that, did you remember to press and hold the
GPIO 0 button while inserting the USB plug?
Setting up Tasmota
Before reassembling, I like to check everything is working, and the easiest way to do that is to set up Tasmota with some basic settings.
First, unplug and reinsert the FTDI USB adapter, but this time do NOT hold down the
GPIO 0 button. This will just provide power to the device so it can boot up.
After a few seconds, check on your phone or computer for a WiFi access point, named starting
tasmota- and followed by 4 digits.
Click on it to connect to the Tasmota WiFi, and after a few seconds, a screen should pop up.
Fill in your WiFi SSID name and password in the boxes named AP1 SSId and AP1 Password respectively.
Click Save, and the Sonoff S31 will reboot.
Rejoin your main WiFi network. You'll need to look up the IP address of the Sonoff S31 - I find the easiest way is to connect to my router and find the list of clients. In my case, the new device had been assigned the IP address
192.168.0.135. Navigating to this URL in a browser will show you the main Tasmota screen.
Since the Sonoff S31 is only connected with 3.3V power, I don't recommend clicking the Toggle button to switch the relay on / off just yet - soon, don't worry!
Click into Configuration and then Configure Module, then select
Sonoff S31 (41) from the Module type dropdown list. Click Save and the Sonoff S31 will restart.
That's the basic configuration complete. We'll get it reassembled before we do anything else.
Reassembling the Sonoff S31
Reassembling is simply a case of reversing the disassembly steps.
Carefully unsolder the flashing wires from the pads on the Sonoff S31 circuit board. Reinsert the board into the white plastic casing, and reassemble it. Make sure the white plastic guides are seated correctly in the rails when you slide them back on - there is a right way and a wrong way to put them in.
Take your time reassembling and make sure all the screws are tight and everything fits correctly - there's going to be a lot of power running through it when it's connected!
When it's fully reassembled, it's time to test it out.
Plug it into an empty wall outlet and wait a few seconds while it boots up and connects to the WiFi. If you browse to the IP address of the device in your browser, you should see the same screen as before.
This time, pressing the Toggle button should turn the relay ON. Pressing it again will turn the relay OFF. You can also press the button on the side of the Sonoff S31 itself to toggle the relay on and off - the web page will update automatically.
If you plug something in (I've been using a small electric heater), you can see the power consumption on the screen too.
Tasmota is incredibly powerful firmware with a lot of configurability, so if you haven't already done so, I highly recommend reading through the Tasmota documentation. With everything from customizable button behavior to MQTT messaging, I'm looking forward to the Sonoff S31 being an integral part of our Smart RV home automation system.
OTA Firmware Update
Wouldn't it be a pain if each time you wanted to upgrade the firmware to get access to a new piece of capability, you had to disassemble the entire device and solder on some wires?
Fortunately, there's an easier way. Tasmota supports over-the-air (OTA) updating, allowing you to upload new firmware to the device via WiFi.
In your browser, load up the Tasmota main page by visiting the device's IP address. Click on the Firmware Upgrade button.
Here you have two choices. You can either upload custom firmware (e.g. that you've downloaded from the Tasmota GitHub Releases page) using the Upgrade by file upload option.
Alternatively, you can use the Upgrade by web server option to automatically download and install the latest firmware directly from the Tasmota downloads page (or any other URL you insert here). Enter the URL you want to use (or leave the default for the latest Tasmota version) and click Start upgrade.
The Sonoff device will restart, and when it does, it will be running whichever firmware you selected - in my case, I chose to upgrade to the latest version using the Upgrade by web server option. It might take a minute or so for the device to restart, but give it time!
I love that it's so inexpensive that I can deploy several of them around the place, conneccted to different devices. I'm very reassured to see that the Sonoff S31 is now UL Listed in both the US and Canada. And being able to fit two next to each other on a double outlet is great too!
I use Tasmota to communicate with our home automation system via MQTT. Using its messages, I can capture real time power usage in addition to being able to turn on and off devices remotely. Plus, with the full power of Home Assistant's automations and even more capability with NodeRED, it's easy to create timers, conditions and automations.
The Moyina FTDI USB Adapter is the easiest way I have found to flash devices using a serial bootloader, and has saved me a lot of time vs other methods I've tried in the past.
I hope this guide was useful, and let me know in the comments how you plan to use your Sonoff S31 smart WiFi plug!