Hacking the Sonoff Wifi Switch - Part 2

Programming the Sonoff Wifi Switch

In my last post, I showed how to mod the Sonoff Wifi Switch for easy programming using Arduino (or any other ESP8266 programming utility). In this post, I'll show how I setup my physical programming hardware and get custom code onto the Sonoff using the Arduino IDE.

Go to Part 3


Programming the ESP8266 based Sonoff is relatively simple. It will require a connection between the Sonoff and the programmer, which in this case is a PC, Raspberry PI, or Mac. A simple, cheap USB to Serial UART adapter will connect the computer to the Sonoff for programming.

A WORD OF CAUTION: These devices are designed to work on your home mains power. You should understand the implications of connecting a device you altered to mains power. Do not ever work on a device that is connect to a mains power source. Do not attempt to program the Sonoff while it is connected to a mains power source.


  • Make an easy method to connect each Sonoff to the programmer
    • Allow the Sonoff to be programmed without requiring disassembly of the enclosure
  • Attach a switch for easily putting the Sonoff in flash mode
  • Program and test the Sonoff using Arduino
    • Connect to local wireless
    • Test the builtin LED
    • Test the relay


I have linked the tools and components I used to build this project. I personally own each item linked below. 

Tools I Used

Parts and Pieces

Assembly Steps

  • Pick 4 female jumper wires. 4" - 8" is a good range to work with
    • You may want to use the color code from the last post to keep things in order
  • Cut one end off of each jumper and strip about 1/16" off
    • Keep one female header connecter on each wire
    • These female connectors will attach to the USB to Serial UART adapter pins
  • Solder the SPST switch on the GND wire
    • It doesn't really matter where you cut the GND wire. Just pick a spot that will leave you enough length to strip and solder the wire to the SPST
  • Crimp a JST 2.5mm pin to each stripper wire and insert into the male JST type connector
  • Connect your wires to the USB to Serial UART adapter
    • Remember that in serial communication TX connects to RX and RX connects to TX
    • Refer to the last post to if you used the color code that I did for RX, TX, VCC, and GND

Connection Steps

The Sonoff needs to be placed in flash mode before it can be programmed. The button on the Sonoff must be pressed when power from the UART adapter is attached. I was able to do this by plugging the USB cable into the computer, holding the Sonoff with the button pressed and the UART adapter in the same hand, and then plugging the USB cable into the UART adapter with the other hand. At that point you can release the button on the Sonoff.

This is clumsy, so I think its worth the effort to put a switch on the GND to make the process less of a contortion act.
  • Connect the USB to Serial UART adapter to the Sonoff using the JST type connector
  • Switch the SPST switch to the off position
    • You will know that the Sonoff has power if the green LED turns on
  • Connect a USB cable between the computer and the USB to Serial UART adapter
  • Press and hold the Sonoff button then switch the SPST to ON
  • Release the Sonoff button and you are ready for programming
    • The Sonoff LED should remain off when in flash mode

Arduino Steps

The Arduino IDE needs a couple things configured prior to testing custom code on the Sonoff.

I am making an assumption that you have programmed with Arduino before. If you haven't go have a look at one the many detailed tutorials out there.
  • Open the Arduino IDE
  • Go to File > Preferences and locate Additional Board Manager URLs:
  • Paste this into the field http://arduino.esp8266.com/stable/package_esp8266com_index.json and select OK
    • This will allow you to download the ESP8266 module via the Arduino Library Manager

  • Go to Tools > Boards:... > Boards Manager
  • In the Search field enter esp8266 and Install esp8266 by ESP8266 Community
    • I have been using version 2.3.0, because 2.4.0 has been giving me headaches with other esp8266 libraries.


I used a simple code written by Rui Santos at http://randomnerdtutorials.com. It sets the Sonoff up as a web server that you can connect to with your web browser. Then you can interact with the LED and the RELAY. It's an easy test that will let us know things are in working order.

  Rui Santos
  Complete project details at http://randomnerdtutorials.com

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

MDNSResponder mdns;

// Replace with your network credentials
const char* ssid = "YOUR SSID";
const char* password = "YOUR PASS";

ESP8266WebServer server(80);

String webPage = "";
int gpio13Led = 13;
int gpio12Relay = 12;

void setup(void){

  webPage += "<h1>SONOFF Web Server</h1><p><a href=\"on\"><button>ON</button></a>&nbsp;<a href=\"off\"><button>OFF</button></a></p>";

  // preparing GPIOs
  pinMode(gpio13Led, OUTPUT);
  digitalWrite(gpio13Led, HIGH);
  pinMode(gpio12Relay, OUTPUT);
  digitalWrite(gpio12Relay, HIGH);


  WiFi.begin(ssid, password);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {

  Serial.print("Connected to ");
  Serial.print("IP address: ");

  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");

  server.on("/", [](){
    server.send(200, "text/html", webPage);

  server.on("/on", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio13Led, LOW);
    digitalWrite(gpio12Relay, HIGH);

  server.on("/off", [](){
    server.send(200, "text/html", webPage);
    digitalWrite(gpio13Led, HIGH);
    digitalWrite(gpio12Relay, LOW);

  Serial.println("HTTP server started");

void loop(void){

Program the Sonoff

REMINDER: Do not plug the Sonoff into your mains while programming

  • Insert your SSID and password into the code
  • Use the following settings to program the Sonoff
    • Board: Generic ESP8266 Module
    • Flash Mode: DOUT
    • Upload: 115200
    • I have found all other defaulted settings to work fine for me
  • Open the Serial Monitor and then Upload the code
    • If all goes well, you should see the LED's on the UART adapter blinking as data is being transferred to the Sonoff in addition to the progress in the Arduino console.
    • If you have problems, I would check your TX - RX connections and that your programming setting in Arduino are correct.
    • I found that if you program with Flash Mode set to DIO, it will appear to program fine, but then the ESP8266 will not run the code properly.
  • In the Serial Monitor you should see that the Sonoff has connected to Wifi and is ready for HTTP connections

  • Open a web browser and use the IP presented in the Serial Monitor to navigate to the Sonoff web page. In my case its

  • Now, without the Sonoff plug into your mains, select the ON button. You should hear the RELAY click and see the LED turn on.
  • Select the OFF button. You should hear the RELAY click and see the LED turn off.
  • If you intend to program several Sonoff Wifi Switches you should label the Sonoff after it is programmed.


You now have a hacked Sonoff Wifi Switch that you can program with any custom code you choose. I have used various methods to control the Sonoff, including MQTT with Node-RED and Blynk with great results.

Next Time

For me, the most enjoyable method of controlling the Sonoff has been using Alexa. In my next Sonoff post we will use Alexa to enable voice commands and simple routines to control the Sonoff.


Popular posts from this blog

Creating Alexa Skills for IoT and Nodemcu - Part 2 AWS IoT

Hacking the Sonoff Wifi Switch - Part 3 Alexa Smart Home