Hacking a Sonoff Basic Smart Switch

By Les Pounder. Posted

Smart homes and home automation are some of the biggest communities in the maker community. Could this cheap, simple smart switch from Sonoff enable anyone to make their home smart? Well, the only way to find out is to take it apart and take a look.
Made from a rigid workable plastic, the case for the Sonoff Basic can be easily worked with hand tools. But great care should be taken, as the case has been designed to reduce the chance of electrocution. It features cable strain relief that will grip the mains cable for the input and output terminals. These grippers should remain unaltered, as they are there for your safety.

WARNING

The Sonoff Basic is powered by 90 V–250 V AC electricity. Do not attempt to open or hack the device while it is connected to AC mains power. There is a serious risk of electrocution or death. Make sure that any modifications you do to the case leave it in a safe condition, where users won’t come into contact with electronics. If you’re unsure about working with mains voltages, only do so under the supervision of someone who is competent in this.

The Sonoff Basic is really just an ESP8266 with a connected relay. The ESP8266 runs on 5 V DC which is transformed from AC to DC and regulated via the board. The mainboard is split into two sections: the low-voltage section, and the high. On the low-voltage section, we see the ESP8266 chip, antenna, and a series of unpopulated GPIO pins – more on that later. The high-voltage side of the board has two large tracks of solder. These tracks connect the live and neutral input and output together. Neutral is connected at all times, but the live track goes via a 250 V relay which has been set to normally open (NO), and that means the default behaviour of the relay is to break the live connection.

The high- and low-voltage sections are separated by gaps in the board, giving the two sides enough space to prevent issues. To input and output the AC voltage, we see two screw terminals. These terminals secure the wires in place, and while they are adequate for the task, we really need to be careful and ensure that our wires are carefully twisted and inserted cleanly into the correct terminal. Even a single strand of wire bridging the two terminals could prove dangerous, so check and double-check before securing!

Hackability

We mentioned that the Sonoff Basic is just an ESP8266, an extremely cheap microcontroller that brings down the cost of creating Internet of Things (IoT) projects. The standard Sonoff software uses an app on your smartphone to enable control of the switch via the app. The app sends a message to the ESP8266 which triggers the GPIO pin responsible for the relay to trigger. This then connects the AC voltage input to the device connected to the AC output, turning said device on/off.

The standard software on the ESP8266 and the app is rather basic, but luckily for us, we can fix that! On the board, we can see five unpopulated GPIO pins between the push-button and a capacitor. These pins offer a direct connection to the ESP8266 and, using a CP2102 CH340G USB to serial adapter, we can load custom firmware onto the chip. But in order to do that, we need to solder a female header connection to those pins. It would be prudent to only use a female header, rather than a male header, otherwise we are adding unshielded metal pins into a high-voltage device. It might be OK 99% of the time, but never take the chance with high voltage. The pinout from the pin nearest the push-button is 3V, Rx, Tx, GND, GPIO14.

When flashing custom firmware onto the Sonoff Basic, DO NOT power the board using AC power – only use 3 V power from the CP2102. Anything higher will damage the ESP8266, and could potentially kill you! Connect 3 V and GND from the USB to serial device to the ESP8266 pins. Then, connect from the USB to serial Tx to Rx on the ESP8266, and then Rx from the USB to Tx on the ESP8266.

Sonoff Case

For this article, we chose to use Tasmota firmware, as it offered the most flexibility. We had to jump through a few hoops to make it work – chiefly, learn how to flash the firmware using the Arduino IDE – but when it did work, it was glorious! During the flashing process, we can specify the SSID and password for our WiFi, or set the Sonoff to boot in AP mode and enable us to connect and configure the device remotely. Nice! Tasmota also works with Home Assistant (hsmag.cc/wwsAkw), which can be run from a Raspberry Pi. This means we can control groups of Sonoff Basics. For example, we can create a group for ‘Lounge Lamps’, and turn on every lamp at the push of a button. We can even use remote sensors to control our home lighting and heating.

For under £10, this is a great, relatively easy-to-hack smart switch. The ease of use enables us to flash custom firmware that elevates the product to a much more interesting and usable device. Via Tasmota we were able to control the switch using its own web interface and via MQTT, a lightweight message system that works across many devices and languages. You do need to be careful, as this is mains voltage-powered, and always remember to only flash the device when it is connected to 3 V power only, not AC!

Let there be light

In this article, we used the Tasmota firmware, hsmag.cc/eSKeUY, as it offered the greatest amount of features versus ease of use. But there is other firmware available. For example, the ESPurna firmware, hsmag.cc/TiGJaJ, offers a similar feature set to Tasmota. It also offers integration with Google Assistant, IFTTT, and Alexa, so we can easily link up our smart assistants to control our home by voice alone. We also have access to ThingSpeak, an API that enables us to link real-world sensors to the cloud, collecting data, and then using it to power projects. For home users, we can use ThingSpeak to control our home lights, Christmas decorations, and heating based on keywords and sensor data. All of this is made possible by custom firmware for this £9 product.

But it doesn’t stop there! If you just need a simple on/off switch, a timed switch via a PIR sensor, then we can use the GPIO of the ESP8266 and a little MicroPython code to write our own code to control the Sonoff Basic. All we need to do is use the esptool.py command-line tool and flash the latest MicroPython for the ESP8266. Then write the code, and you have your own bespoke smart switch.

More features from HackSpace magazine magazine

Subscribe