Random Nerd Tutorials

Reprogram Sonoff Smart Switch with Web Server


In this post, you’re going to learn how to flash custom firmware in the Sonoff device, so that you can control it with your own web server. I recommend that you read my previous post to get familiar with the Sonoff. We also have additional resources that describe how to flash a custom firmware to the Sonoff device using an FTDI programmer and the Arduino IDE. 

If you don’t have a Sonoff yet, you can get one for approximately $5 – visit Maker Advisor to find the best price.

First, watch the step by step video tutorial below

Safety warning

Make sure you disconnect your Sonoff from mains voltage. Then, open the box enclosure.


Sonoff pinout

The Sonoff is meant to be hacked, and you can see clearly that these connections were left out, so that you can solder some pins and upload a custom firmware. That’s the pinout.


I’ve soldered 4 header pins, so that I can easily connect and disconnect wire cables to my Sonoff device.


Preparing your 3.3V FTDI module

You need an FTDI module to upload a new firmware to your Sonoff. Use the schematics provided as a reference.

Warning: uploading a custom firmware is irreversible and you’ll no longer be able to use the app eWeLink.

I’ve added a toggle switch in the power line, so that I can easily turn the Sonoff on and off to flash a new firmware without having to unplug the FTDI module.

I used hot glue to glue the ends of the wires together. This prevents you to make wrong connections between the FTDI and the Sonoff in the future.


Boot your Sonoff in Flashing Mode


To flash a new firmware to your Sonoff, you have to boot your Sonoff in flashing mode. Follow this 4 step process:

1) Connect your 3.3V FTDI programmer to your computer

2) Hold down the Sonoff button


3) Toggle the switch to apply power to the Sonoff circuit


4) Then, you can release the Sonoff button

Now, your Sonoff should be in flashing mode and you can upload a new firmware.

Opening the Arduino IDE

You should have the ESP8266 add-on installed in the Arduino IDE – If you don’t have the add-on installed, first follow this tutorial on How to Install the ESP8266 Board in Arduino IDE.

You can upload the full sketch to your Sonoff (replace with your SSID and password):

Preparing your Arduino IDE

Having your Sonoff device still in flashing mode.

  1. Select your FTDI port number under the ToolsPort > COM14 (in my case)
  2. Choose your ESP8266 board from ToolsBoard > Generic ESP8266 Module
  3. Select Flash Mode: “DOUT”
  4. Press the Upload button

Wait a few seconds while the code is uploading. You should see a message saying “Done Uploading”.


If you try to upload the sketch and it prompts the following error message:

warning: espcomm_sync failed
error: espcomm_open failed

It means that your Sonoff is not in flashing mode. You’ll need to repeat the process described in section “Boot your Sonoff in flashing mode” described earlier in this guide.

Final circuit

After uploading the code, re-assemble your Sonoff. Be very careful with the mains voltage connections.

It’s the exact same procedure as shown in the introductory guide.


ESP8266 IP Address

Open the Arduino serial monitor at a baud rate of 115200. Connect GPIO 0 of your ESP8266 to VCC and reset your board.

After a few seconds your IP address should appear. In my case it’s

esp ip address


For the final demonstration open any browser from a device that is connected to the same router that your Sonoff is. Then type the IP address and click Enter!


Now when you press the buttons in your web server you can control the Sonoff switch and any device that is connected to it.

Sonoff tutorials list:

Wrapping up

That’s it for now, I hope you had fun learning about the Sonoff device. Make sure you subscribe to my blog, because I’ll be posting more tutorials about the Sonoff.

Do you have any questions? Leave a comment down below!

Thanks for reading,


MicroPython Programming with ESP32/ESP8266 eBook

Learn how to program the ESP boards with MicroPython, including projects, tips, and tricks! The registrations are open for a limited time, so SIGN UP NOW »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our course even with no prior experience!

Leave a Comment:

Add Your Reply

⏰ Registrations are open for "MicroPython Programming with ESP32 and ESP8266" eBook