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.









Go to Part 2

Go to Part 3 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 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.



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

Utility Knife

Wire Cutter\Stripper Combo - http://amzn.to/2BHKDHH

Small Needle Nose Pliers - http://amzn.to/2GVL3Kc

Soldering Iron - http://amzn.to/2nRfdWi

Solder

Tip Cleaner - http://amzn.to/2sdpZLQ

Rotating Circuit Board Holder - http://amzn.to/2E79r9H

Third Hand for Soldering - http://amzn.to/2E52kTg

Drill - http://amzn.to/2E5eXtt

Step-Bit - http://amzn.to/2BI0E0f

Hot Glue Gun - http://amzn.to/2GTHhkz

Precision Screw driver set - http://amzn.to/2nIjonN

Phillips Screw Driver

2.5mm Pin Crimping Tool - http://amzn.to/2EpegPe (this works, but is not a great tool)

Marker Parts and Pieces

Sonoff Wifi Switch(s) - Single http://amzn.to/2nKhhzZ 8-Pack http://amzn.to/2BJNC2v

2 Prong Extension Cables (mine have 3 sockets. purchased at Lowes for around $1.40 ea)

5/16" Rubber Grommets - http://amzn.to/2GUtkmg

JST Type 2.5mm Female Connectors - http://amzn.to/2GUqQV8

22 Gauge Hookup Wire (preferably stranded) - http://amzn.to/2ElF9U2 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.