Programming an ITEAD Sonoff Wireless Smart Switch (ESP8266)

I purchased some of these wireless smart switches from ITEAD, but didn’t like that you had to use their app and cloud server to use them. They use an ESP8266 microprocessor, so I wanted to see if I could re-program it to load my own firmware.

You can also pick up a similar module from Banggood.com.

NOTE: Someone asked me if I have the original firmware dump for the for this device, unfortunately I do not. If you flash your own firmware onto this device, you do so at your own risk.

UPDATE 2016-09-07: There is a related post, on how to upgrade the flash from 1MB to 4MB

This is what you get in the box:

sonoff-program-4

And opening up the case, you find two sets of unsoldered pins, the horizontal ones is used for a 433Mhz RF module, which I did not purchase. The vertical one brings out the RX and TX pins of the ESP8266 along with 3.3V, GND and a GPIO pin.

sonoff-program-3

I soldered on two pin headers, but the top vertical is not required to re-program it.

sonoff-program-2

Then, you can hook it up to a USB to serial adapter.

sonoff-program-1

In this orientation, the bottom pin is pin 1, and the connections are made as follows:

Sonoff J1 Wire USB To Serial
1 : VCC-3V3 Orange 3V3
2 : E-RX Green TXD
3 : E-TX Yellow RXD
4 : GND Black GND
5 : GPIO14 Not Connected Not Connected

To place an ESP8266 into program mode, GPIO0 must be LOW during power up. The schematic shows that GPIO0 is pulled HIGH with a resistor and that the switch (S1) is connected between GND and the same resistor.

So by holding in the tactile switch that is used to turn the lamp on/off before powering on the device, you can place the ESP8266 into programming mode.

The steps to program the device
  1. Disconnect the AC power, power it only from the USB to Serial for safety
  2. Hook up the USB to Serial as shown above
  3. Hold down the switch
  4. While holding down the switch, plug the USB to Serial into your PC
  5. Let go of the switch
  6. Use the Arduino IDE to select the correct USB port and upload your program.
ESP8266 pin usage
ESP8266 Pin Function GPIO Connected to
9 – MTMS GPIO GPIO14 J1 Pin 5
10 – MTDI GPIO GPIO12 Relay (HIGH to turn on)
12 – MTCK GPIO GPIO13 LED (LOW to turn on)
15 – GPIO0 Flash GPIO0 Tactile switch (LOW when switch is pressed)
25 – RDX UART RXD GPIO3 J1 Pin 2
26 – TXD UART TXD GPIO1 J1 Pin 3

You can use the table above to determine which GPIO’s to use when creating your own program to read the switch and to turn the relay on/off.

GPIO14 is available on the J1 pin header, and you can also use the other GPIO’s in the table above (although they’re a bit more tricky to use).

I’ve also managed to get the analog input pin to work (required some fine soldering), which I’ll detail in another post.

20 thoughts on “Programming an ITEAD Sonoff Wireless Smart Switch (ESP8266)”

  1. Hi, i incidentally broke my button – down right leg. (ot outher side of board and close to diod. Can you give me point to resolder it ?
    I can’t get it from schematic. Thank ypu

  2. On the SV, I found the GPIO 2, it’s on the back, the resistor between flash chip and serial header.
    Could you help me to find the analog pin 6?

    1. On my Sonoff the Analog pin was not connected to anything, but I did manage to solder a wire directly to the ESP8266 chip and bring it out to an unused header pin. When I get a chance I’ll make a post showing how to do this.

      1. Yes, adding connections to the chip directly looks fiddly. Great, well written and helpful post, thank you. So far I have found the SONOFF is pretty unstable, not sure why. I’m still on standard flash memory (upgrade chips ordered) but it is behaving as though the power supply is insufficient and the regulator gets pretty warm. The same (arduino) code works fine on a WeMos D1 mini, so I suspect the SONOFF power supply. Do you find yours to be stable?

        My application is simple, a couple of config files in a 64k SPIFFS volume, NTP client, basic webserver serving a couple of hand-coded HTML pages and a simple scheduler to allow programming and execution of timed events on the relay. I suspect you have already done all of this.

        1. Thanks Ian.

          I’ve not run mine for long enough to notice any stability issues – I’ve got two sitting in half finished projects at the moment, just can’t seem to find the time to finish it!

          Although it won’t explain why it works on the Wemos D1 mini and not the Sonoff, maybe try add a yield() to your main loop if you have not yet done so? In some cases the delays in the main loop are not long enough to service all the internal buffers and can cause a crash or watchdog reset.

          1. Hi Andre, thanks for the reply. I have had a better look at this including adding a better capacitor to the 3.3V line, but no change. What I actually suspect is the NTP routine, which on the Sonoff remains enabled as there are no pins for the RTC that the Wemos has connected. The code is the same on both with the exception that time sync is from the RTC every 5 mins on the Wemos and 24 hours from NTP, whereas the Sonoff goes out to NTP every 5 mins. I have changed the sync interval to an hour, which in theory will reduce the risk. I added another yield() into the loop but that didn’t alter anything as there is very little else happening. I saw a way of retrieving the last crash reason after boot of the 8266 so will implement that and write it to Flash so I can get some more information – not quite brave enough to have the serial connected yet while on mains power, my interpretation of the schematic suggests that ought to be ok. If I can rule out the power supply as a cause then that’s really good news; it has to be worth writing a simple sketch to simply loop between reboots and just pull the time on boot to allow meaningful logging.

          2. Thanks for the feedback Ian. I should really start mine up again and let it run to see how stable it is. My plan is to use the Sonoff to switch on an outside security light when it gets dark, log temperature/humidity with an DHT22 and monitor light level using an LDR (sending this to an MQTT server). I’ve built the project on breadboard and it works – just need to find the time to put it all back into the Sonoff case and mount it!

  3. I’ve spent some of this afternoon walking through my NTP code – I’m guilty of cut and paste here as it is one of the built-in examples and seemed to work. I suspect though, that I didn’t check which version I took though as the ESP8266 version seems to use delay() rather than a while(…) and mine has while(…). The upshot is that it can take up to 1.5 seconds for a NTP packet to be received before the routine times out and I’m having a reasonable guess that this is occasionally enough to run the ESP out of resources without a Yield(). I added a yield() at the top of that while(…) loop and I’ve seen flawless operation since, so have set my NTP revisit to every 6 hours after noticing surprisingly little drift over each hour so far. For fun I added a telnet server that can be used to see the serial debug stream, but of course that doesn’t show bootloader messages!

    My application is currently a simple single channel scheduler to switch outside lights on and off. I’ve created a couple of simple web pages hosted by the Sonoff to allow direct controls and programme times and also use the Sunrise library (deprecated but works well) to calculate sunset and turn the lights on at dusk if I enter ‘-2’ in the hour field. ‘-1’ is used to give a ‘never’ condition (since the hour() can never be -1).

    The whole thing is a branch of my first 8266 project, which has gradually grown from a wifi Voltmeter into a complete remote control system for our (433MHz wireless) central heating and (also 433MHz) remote sockets around the house. I didn’t use a DHT, having never tried a Dallas OneWire temperature sensor, I used one of those instead, plus buttons and a SSD1306 OLED display for a HMI. I ought to document it really as I’m sure others would use and improve it.

    Next will be a Sonoff central heating controller, as it cunningly has the same external connections as many common wired heating thermostats, including the Drayton one my sister has at her house. I’ll let you know if I’ve fixed my code and will look forward to your next installment. 🙂

    1. 36 hours with no reboots, so I think it is OK now. Thanks for the hint on yield(), it was a good reminder to check the code properly.

      The other one now has a DS18b20 temperature sensor, the central heating scheduler, ‘app’ page and config page to allow router password entry via a hosted hotspot page when wifi doesn’t connect.

      If you look at the free pins on the 8266EX, you might agree that it’s probably possible to get at two end ones that currently look unused:

      – GPIO4 (pin 16)
      – GPIO5 (pin 24)

      I may have a look at this as adding two would give some more expansion options, for example i2c for an OLED display, etc.

  4. The screw terminals as there is only 4 or 5 mm of depth for the mains wires to go. I tinned the wires but remain concerned that the wires are not firmly fixed. Any suggestions?

  5. I am totally newbie and I got stuck for few days by getting start,
    I do the connection to USB, without press the button, I got green led flash (I able to connect to sonoff by mobile), but I am not able to communicate by terminal in anyway, than I tried flash mode, same as well, does original firmware not able to communicate in terminal?

  6. Hello, Andre
    and thanks a lot for this documentation, good understandable even for me non native.

    you said “I soldered on two pin headers, but the…”

    Did you already have a look at the RF header?
    I purchased 7 Sonoffs until now, all still original yet,
    wondering if one could find an upgrade path, as soon many cheap 433 MHz tx/rx modules are sold around at dx.com, bg, ali, ebay…
    but in case of finding a fitting module one would have to read the original firmware and re engineer it either, no?

    If guided a little, I had some spare time to take a look at, but being a technician with a little hard/software skills on one hand, I’m a rather a noob in MCUs and protocols and so on…

    kind regards, mICHael .

    1. mICHael, I had wondered the same a while ago and after a bit of googling I came upon someone who actually had done this… but sadly can’t find the link.
      I suspect that if you do not want to change the firmware, just add an RF module….. in principle you would not need to change the firmware as most likely the firmware between WiFi modyle, RF module and WiFi plus Rf module is the same… just the hardware different… but unless you had one of the original transmitters available you wouldnt know what the codes are, unless someone wouldnt mind sharing his or her codes.
      Ofcourse if you want to reflash, you can add your own routine for the RF receiver

Leave a Reply