Hacking the Sonoff Wifi Switch - Part 1

Making the Sonoff Easy to Program

The release of the ESP8266 has generated a lot of interest in the microcontroller community. Nodemcu and other cheap MCU's with built in wifi have really opened the door for consumers/makers to create cost effective IoT solutions. One significant reason ESP8266 based devices are taking off is due to the fact that Arduino libraries have been created and are well maintained.

Sonoff Wifi Switches, which are based on the ESP8266, are getting a lot of attention and for good reason. They are cheap and very hackable. I should mention that Sonoff has its own cloud service for controlling these wireless switches

Here is my take on hacking the Sonoff.

Overview

I have been playing around with a small DIY aquaponics system in my home office. So far its a success, but I would like to have more control. Currently, I have 2 Sonoff wireless switches (both with custom code) controlling grow lights and aquarium lights. I would like to control each of the pumps that move water from the fish aquariums to grow beds. This will allow me to try out some different fill and drain cycles.

JST type connectors will be used to make it easy to connect a USB to Serial UART adapter to the Sonoff.

A WORD OF CAUTIONThese 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.

This hack essentially removes the enclosures ability to insulate. Proceed at your own risk. 

Why not use an off the shelf solution?

Using a standard Alexa compatible smart plug or the "out of box" Sonoff devices limits you to very rudementary schedules and routines. I  have been using some off the shelf smart plugs for simple things with good success, but I need something a tad more sophisticated for this project.

Why use connectors at all?

I programmed my first Sonoff by soldering wires with female pin header connectors. Then the female connectors were connected directly to a USB to Serial UART adapter for programming. This worked, but you end up having to keep a pin out handy each time you want to connect things. I wanted something that would not take any effort.

I had thought of potentially connecting and mounting the required USB to Serial UART adapter inside the Sonoff enclosure. I think it could be done. However, this would double the cost of each switch.

Another option that seems attractive is using the "shield" approach. By attaching pin header rows to the enclosure it would be possible turn the USB to Serial UART board into a shield. It would be easy to attach and detach the USB to Serial UART board anytime you needed. It would expose pins that are connected to a circuit that draws power via a mains power source, so that would take some considering.

Goals

  • Convert the Sonoff Wifi Switch into a programmable device.
  • Make an easy method to connect each Sonoff to the programmer.
    • Allow the Sonoff to be programmed without requiring disassembly of the enclosure.
  • Extend the usefulness of the Sonoff by increasing the number of devices that can be controlled.

Prep

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

Steps

  • Disassembly
    • Open the packing and set the screws and end covers to the side.
    • To open the enclosure, use a pocket knife or utility knife to cut the small seal and pry the shell open.
  • Drill a 5/16" hole in the side of the enclosure.
    • I used a step-bit, but a 5/16" drill bit would work fine
    • It is necessary to drill the hole where there will be no internal components in the way. I use the last "f" in the Sonoff labeling as a reference. 
    • Please look at the next picture to see the hole placement that I used.

  • Insert a 5/16" rubber grommet into the drilled hole



  •  Cut 4 - hookup wires @ 2" for VCC, GND, TX, and RX
    • I used red = VCC, green = GND, white = TX, black = RX
    • This wires will be trimmed to as desired later.
  • Strip about 1/16" off each end of every wire
  • Solder the hookup wire to the Sonoff PCB
    • This is probably the clearest pin out of the PCB I have found Sonoff Pinout
    • Solder your hookup wires to using your color scheme to match the pinout
    • Using a PCB holder and a third hand makes the process very easy
  • Run your hookup wires through the hole you drilled
  • Place the PCB back into the top shell of the enclosure
    • Pay attention to where the button and LED go. The button must go through the small button hole. The LED fits into a cup that is only visible from the inside.



  • Cut the hookup wire to a length that works for you and attach the JST style 2.5mm pins to the hookup wires using a 2.5mm crimper.
    • I picked a length that would allow me to hot glue the connector to the enclosure.
    • Using cheap crimpers and connector kits can sometimes be frustrating. Spend sometime getting to know your crimper and pins.
    • If you don't have much experience with crimping pins, practice on some loose wire.
    • Stranded wire is preferred when using JST type pins, but I have never had an issue using solid hookup wire with JST type 2.5mm pins.

  • Decide your pin order and insert the pins into the JST 2.5mm female connector.
  • Hot glue the JST type female connector to the enclosure


  • Cut the extension cord at a spot that is convenient for your plans.
    • I cut mine 12" from the 2 prong plug
  • Mark the LINE wire with a marker.
    • The LINE wire is connected to the smaller of the two prongs
    • This will help you when connecting to the Sonoff terminal block
  • Strip about 1/8" on each wire
    • You may want to tin the ends of these wires to make it easier to get the wires into the terminal blocks. DO NOT TWIST the wire... it doesn't work well with the terminal blocks on the Sonoff.
  • Loosen the screws on the terminal blocks and insert the wires making sure that the LINE wire is inserted into the L labeled terminal.
    • The 2 prong plug connects to the input of the Sonoff.


  • Screw the end plates/caps on each end of the enclosure.
    • Don't go over board or you may strip out the plastic



Result

The result is a Sonoff that is ready to be programmed using a USB to Serial UART adapter and the Arduino IDE. I can hook up 3 appliances to a single Sonoff Wifi Switch.

...UPDATE

I found that wiring up a few of the Sonoff Wifi Switches with short cables was useful. I used the same 2 prong extensions with 3 sockets. I now have a mix of 6' and 1' switches.

Next Time

We will setup a cheap USB to Serial UART adapter to plug in to the Sonoff, program the Sonoff, and control an appliance.

Go to Part 2
Go to Part 3


Comments

  1. Very clean solution. I burnt a Sonoff with my first attempt :-)

    Now I am reprogramming them wirelessly with Sonota. You do not need to open the enclosure either!

    ReplyDelete
    Replies
    1. I have been using OTA on the Sonoffs and other MCU's, but I had not heard of Sonota. I will look into it.

      Delete
  2. You shouldn't do this.

    Mains powered electrical devices need to be double insulated or earthed. The Sonoff is double insulated and the plastic case is one of the insulation layers. By running low voltage wiring from the circuit board through the case you are bypassing that layer of insulation which changes the Sonoff from safe to unsafe.

    ReplyDelete
    Replies
    1. So how does a switch mode wall wart (ac adapter) pass UL?

      Delete
    2. Ken Taylor, of course you are correct. There is a risk created here and I can update the blog to reiterate that.

      The typical approach is to tuck any wiring soldered for programming purposes back into the plastic case, thus keeping the double insulation intact. OTA programming is also possible on the sonoff, so its possible the need to program directly via serial is not needed often.

      Thanks for keeping us honest.

      Delete
    3. Instead of a hole, the programming interface could stay inside the case and the case opened to reprogram. That requires undoing four screws every time the device is to be reprogrammed but doesn't compromise safety.

      Delete
  3. The line (i.e. hot) side is the smaller of the two prongs of a plug, the larger prong is neutral!

    ReplyDelete
    Replies
    1. Wow... The worst kind of typo. I will correct that immediately.

      Delete
  4. awesome. thank you for the effort here, this works great for me. I just used segments of female jumper wires soldered in and tucked inside the enclosure (thanks to all the comments above), which is just fine for updating the code whenever as that's not needed often. it's quite easy to get upload code; i haven't found need for the switch at all.

    ReplyDelete

Post a Comment

Popular posts from this blog

Hacking the Sonoff Wifi Switch - Part 2

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

Hacking the Sonoff Wifi Switch - Part 3 Alexa Smart Home