SONOFF: a remote switch to turn appliances on or off


Can you imagine turning something off or on remotely? You can turn the heating on, or turn it off if you’ve left it on through carelessness, you can also open or close blinds if you have automatic ones, or make your house at a suitable temperature by operating on the air conditioning from anywhere. Well, that’s what a sonoff can do.

For example, one of the ones you can find in the market is the Itead Sonoff. This is a device based on the ESP8266 module, which is sure to ring a bell if you are a maker and create DIY projects with Arduino. This is a very popular WiFi module to which we have already dedicated a special Hwlibre article. To that module we have added a relay to act on the connected devices and thus be able to turn things off or on by WiFi.

What is a sonoff?

A Sonoff is a smart WiFi switch for remotely switching on and off an electrical device. This device allows you to be connected to the Internet through a WiFi router in your home or office. From any device connected to the network and anywhere, you can interact with it to control your devices.

Many commercial sonoffs have their own app for Android or iOS that allows easy control. For example, the Sonoff Basic has the eWeLink app. In addition, many of these apps also allow you to set timers to be activated at a certain time. For example, this allows you to program the radio to turn on and off on holidays when you are away from home, or to raise the blinds and make it appear that the house is occupied to prevent theft…

In addition, some commercial Sonoffs implement functions so that you can connect them to sensors and have the devices turn on or off depending on temperature, sound, presence sensor, humidity, etc. This can be useful for a garden irrigation system, to heat the house properly, etc. Even some models designed for domotic houses have compatibility with Amazon Alexa / Echo, Google Home, etc.

Other accessories

In addition to the sonoff, there are other devices you may be interested in, such as WiFi plugs, WiFi bulb sockets, WiFi switches, WiFi-RF gateway, SC-WiFi station, etc., which are perfect complements for your WiFi relay or sonoff.

Make your own Sonoff with Arduino and an ESP8266 module (Easy)

Despite being a very simple and cheap device, you may not always be interested in buying one. If you are a maker and you like to create your own DIY projects, instead of buying the commercial sonoff, you can create it yourself. That would mean using a relay module for Arduino and a WiFi module. That way you will have your project connected and ready to operate on the relay that will interrupt or connect the high voltage device.

Another possibility is to hack into a Sonoff that has already been made to provide new functionality. For this, the best option you have is the Itead Sonoff. For example, the , a device from a Chinese company oriented to makers. The device is very well documented and so it can be easily hacked to modify it and adapt it to your projects. Being based on an ESP8266 makes everything easier, and includes its own control app for mobile devices.

Creating a simple Sonoff with Arduino

Maybe it’s easier for you to create your own Sonoff with Arduino than to modify the firmware of the Itead. Using the Itead and other ready-made devices is very simple, but from just using them to modifying their firmware there is a big difference. So you may be interested in a simple way to have a Sonoff created by you.

What do you need?

All you need for this project is:

  • Arduino ONE, or it would be worth another plate.
  • Protoboard or PCB if you are going to solder.
  • ESP8266 Module
  • Relay module
  • Wiring for connections
  • PC with Arduino IDE and cable for programming
  • Electrical device you want to manage by WiFi

Step by step construction

Arduino connection with ESP8266 and Relay

To start building your home Sonoff, follow this step-by-step guide:

  1. Once you have all the elements, you must connect the scheme properly as shown in the image. Remember that where I have put “Appliance” is where it will be connected what you want to control: stove, fan, TV, light bulb, … and the plug should feed the appliance. As you see, what you do is interrupt one of the wires that go to the device and interpose the relay to act as a switch. If you have doubts about the connection, you can consult our manuals:
    1. How to connect ESP8266 to Arduino
    2. How to use the relay with Arduino?
  2. Now the next thing is to program in Arduino IDE to make it work properly. If you are a beginner you can also download the PDF of our programming manual for Arduino. A simple example of code to control the relay and the red, green and blue LEDs would be

#Includes SoftwareSerial.h

#define DEBUG true

SoftwareSerial esp8266(2,3);
void setup()

void loop()

     int connectionId =;
     int pinNumber = (*10;
     pinNumber += (;
     digitalWrite(pinNumber, !digitalRead(pinNumber));
     String closeCommand = "AT+CIPCLOSE=";
String sendData(String command, const int timeout, boolean debug)
    String response = "";
    long int time = millis();
    while( (time+timeout) > millis())
        char c =;
    return response;

Once introduced to the Arduino IDE and programmed through the serial port, everything should work correctly. Although you can use the serial monitor, you may want to create a simpler web interface or a small app for iOS or Android. In the case of the HTML code it is easier and it would be something like that, that way, from your browser you could manage to turn on or off the LEDs and the device connected to the relay:

<title} Control with Sonoff</title
<button id="10" class="led"A</button
{;button id="11" class="led"BLUE LED</button
{;button id="12" class="led">LED GREEN</button
{;button id="13" class="led"RED LED</button
{;script src="jquery.min.js">red
{;script type="text/javascript">
$(".led").click(function(){ var p =
$.get("http://XXX.XXX.X.X:80/", {pin:p});

You can write it in any text editor, and save it with the name control.html. Remember that for it to work properly, you must replace http://XXX.XXX.X.X:80 with the appropriate IP in your case, that is, the one assigned to the ESP8266 within your WiFi network . On the other hand, if you want to replace the device label A with the name of the device you are using and make it more intuitive, feel free to do so…

Web interface control

Now, if you open this .html control with any web browser you will be able to manage your Sonoff. Clicking on the buttons you’ll see how your circuit acts.

Modifying the Sonoff Firmware (Advanced)

Another operation you can do, although it is not recommended for most users because of its complexity, is to modify the firmware of the Itead sonoff. It’s possible, but it involves many steps and it’s complicated if you don’t really know what you’re doing. To give you an idea I leave you with what you would need and the essential steps, as well as links to the firmware and complete guides in case you dare after reading the essential steps…

Using ESPurna

What do you need?

To use the Itead Sonoff and program it, you will need the following items:

  • Itead Sonoff WiFi Basic
  • FTDI or TTL (USB/Serial Pin) adapter and cable
  • Screwdriver
  • Other elements needed for your project
  • A computer for programming

Modify the Itead Sonoff WiFi Basic firmware (Basic steps)

Itead sonoff circuit

To modify the Itead Sonoff you must acquire one and ‘string’ it by following these steps

Before handling the device, make sure it is disconnected. It is not recommended to manipulate the relay or circuit when it is connected to the network, since you will be working with AC at 220v and it is not a harmless DC circuit that works at low voltages .

  1. Remove the plastic cover of the Sonoff to access the internal circuit. You can use a screwdriver to remove the side covers first and then pry off the joint that joins the two parts of the main housing until you can remove it.
  2. If you look at the circuitry in front of you, you can see the parts of the ESP8266 module very clearly:
    1. The electrical elements around the relay are from the AC/DC converter.
    2. The black square component is the relay that works at 5v (control part) and 220v (output).
    3. In the center you have some serial connection pins. You can use them to program the microcontroller or to connect more elements. On the GPIO14 you can connect actuators or sensors.
    4. You will also see a button next to the pins. You can press it to change the modes you can set.
    5. The LED that indicates the operation and the mode or state it is in.
    6. And two green connectors on each end of the PBC. One is an input and one is an output. The one closest to the relay is the AC input, i.e. where the device is connected to the power supply. The other is the output where you can connect the device you want to turn on or off. Remember that it admits up to 10A, that is, a consumption of 2.2kW for 220v.
  3. Now you will work with the connection pins to charge your program. For this you will need the FTDI or TTL, allowing you to connect these pins to a USB of your computer. But beyond connecting it, you must remember that the integrated microcontroller has two modes of operation, a UART mode and a FLASH mode. The UART mode allows you to load the program and the FLASH mode to run it. To switch to UART mode to write the program you need to set the GPIO0 (LOW) and GPIO2 (HIGH) pins to certain states. The loading of the program will be done by the pins marked as RX and TX. Of course you have to use also the 3v3 and GND pins, as well as the GPIO0 button, the LED or GPIO13 and the relay on GPIO12.
  4. Once you have made the connection properly and you have your USB cable to connect it to the PC, you can start with the actual programming. Remember that to the FTDI you must cross the RX and TX connections, that is, the RX from the Itead to the TX from the FTDI and vice versa.
  5. After that, it would be a matter of using Arduino IDE to create the necessary code for the control you want to make (you should select the ESP8266 board as the microcontroller device to be recorded). You can also use the firmware developed by Xose Perez. It is called ESPurna and it is specific for Sonoff WiFi. With its control panel you can control network and security parameters.
  6. At the end, when you already have it, you can return the GPIO0 and GPIO2 pins to High states respectively so that it returns to FLASH mode and runs your program.
  7. Once finished, you can make the appropriate connections in your Sonoff to connect the devices you want and give them power. From the app you will be able to control everything…

Using Tasmota

To do it with Tasmota instead of ESPurna, the generic steps to do it would be

  • First you must open the Itead Sonoff’s case as above.
  • Then you should solder the wiring or pins so that you can make the power connections and the USB to UART TTL adapter.
  • Go to Arduino IDE and in Tools you should select the ESP8266 board instead of Arduino UNO or the default one. Install the new board.
  • Now download Tasmota and install it properly to integrate it with Arduino IDE.
  • Now it is time to program and configure your project as you want and record it on the board… Once you have finished you can connect the components to the sonoff as in the case of the ESPurna.

Leave a Comment