Project - TX RX Hack

The Sonoff S20 plug-in Mains relay module is compact, tidy, looks good, has pass-through earth, comes in many plug-in Mains configurations, has an easily accessed gpio00 button, a blue LED that echoes gpio12 relay status, a green gpio13 LED that can provide user information and feedback... https://www.itead.cc/wiki/S20_Smart_Socket

It's main drawback is that it doesn't have any accessible gpio's for adding sensors etc.
So this project overcomes that S20 drawback, by making the unused TX & RX gpio's 1 & 3 readily available, offering potential for easily plugging in sensors and modules.

NOTE: 
These instructions were originally created for the Sonoff S20 because it suited my needs, but I have also applied the same hack to the Sonoff SV (low voltage module) and Sonoff TH (each costing less than a fiver).

This same hack should also be applicable for many other ESP-8266-based modules if they have facility for 3.3v, 0v, TX, RX pins to connect a UART for re-flashing firmware...
(obviously assuming due care and consideration for physical differences and constraints)

The hack does not burn any bridges by preventing access to TX and RX, it merely extends the internal UART pins via a plug-on extension to an externally accessible 4-pole jack socket... thus an external UART (FTDI adapter) can easily be connected at any time for reflashing if needed.
(which makes this hack also suitable for any other firmwares - perhaps even more so if they require more frequent TX RX programming access)

A major benefit of Wifi-Basic is that the serial connection is only needed to initially flash the firmware, then all subsequent programming and control and future OTA updates can be done using wifi, making the serial pins redundant, and therefore available for us to take advantage of.
This project brings out the TX, RX and power pins to an externally accessible 4 pole jack socket, making gpio1 and gpio3 available for easy use.
If 2 available gpio's are not enough they can be used to provide I2C bus access, giving potential for adding multi-io port expanders, multi A2D's,
RTC, OLED displays, or almost anything that could be reasonably wished for.

The Sonoff TH10 and TH16 models already have a 2.5mm 4 pole jack socket, but only gpio14 is made available without doing some soldering.
There are a few different Sonoff 1-wire temperature sensors available with corresponding 2.5mm jack plugs, but these can be very over-priced.
If you want to use the Sonoff AM2301 it needs to be configured in Basic as a DHT21 temperature sensor module.
So if convenience is more important than other factors, then this could be a reason for opting to use a 2.5mm socket - but pay attention to this...

WARNING
The Sonoff 4-pole jacket socket wiring convention has a serious potential problem which WILL cause the +3.3v to be shorted to 0v GND if a standard stereo 3-pole jack plug is inserted into the 4-pole socket.

The 3-pole TRS (Tip, Ring, Sleeve) jack plugs are physically the same size as the 4-pole TRRS plugs, but where the 3-pole TRS only has one Ring between its Tip and its double-width Sleeve, the 4-pole TRRS plugs have half their Sleeve width 'stolen' for use by a 2nd Ring which must be squeezed in next to it.

Unfortunately, Sonoff's use the additional 2nd Ring for the 0v, and use the neighbouring half-sized Sleeve for the +3.3v supply, so consequently if a 3-pole TRS plug with double-sized Sleeve is inserted, it will short-circuit the 4-pole socket +3.3v Sleeve to the 0v 2nd Ring.
If the thought of short circuit does not bother you, perhaps it should... because considering the close internal proximity of lightly insulated low voltage to the live MAINS voltage, ANY burnt out insulation might risk more than just a dead Sonoff. 
https://www.youtube.com/watch?v=3ugxq0FMjoQ


Alternative Convention
A safer convention to avoid such problems is to use the Tip for +3.3v and the Sleeve for 0v (see on right).
This keeps the supply rails completely separated and only uses the 2nd Ring for optional use of gpio3 (RX).
So as well as being safer, it also offers a legitimate reason for using a 3-pole plug in the 4-pole socket if only gpio1 (TX) is required, because it will merely tie the unused gpio3 RX input to 0v without doing any damage.

The more commonly available 3.5mm 4-pole jack socket size was chosen because although a bit bigger, it makes available a
much greater range of alternative adapters and components and cables etc, and greater choice usually also means cheaper prices.

I bought a handful (20) of 4-pole 3.5mm jack sockets from ebay for less than £3, but if you are impatient you can probably find something locally...
(3.5mm 4-pole jack connectors are commonly used with Android selfie sticks, and also some I-phone Lightning audio adapters, so cables with appropriate connectors may often be found in supermarkets and general stores when you know what you are looking for)
 


Some 4-way screw connector adapters are a very useful accessory, making it quick and easy to knock up sensor dongles without soldering, or to conveniently connect an external UART, etc. I got 10 for a fiver from ebay, but they were also available for about a quid each (make sure it's the 4 screw 4-pole version).



The RX and TX holes have been un-populated on all the Sonoffs that I've used, so you'll probably need to solder in some header pins. You'll also need a bit of removable header socket to plug onto the pins, I soldered mine to small offcuts of stripboard, but you could probably dab the 4 wires directly onto the header socket.

Using the jack TRRS terminology and referring to the wiring diagrams above and right...
Tip connects to VCC (+3.3v), 1st Ring to TX (gpio1), 2nd Ring to RX (gpio3), Sleeve to GND (0v),

The wires need to be routed clear of the button, LEDS, and mounting screw hole, so you may need to have your jack socket protrude externally in order to get sufficient internal clearance.

Obviously the hole for the jack socket needs a bit of care - for my first one I filed a slot, and the next two I drilled, but I must confess that I gouged a pcb which required some awkward nano-surgery to repair - so for my last one I simply poked a hole using a hot soldering iron, but the hole still needs de-blobbing and trimming afterwards. So speaking from experience, I strongly recommend undoing the 2 screws and removing the pcb before attacking the hole - it only takes moments, but could save hours.

It also warrants some care to avoid problems when hot-gluing the jack socket into place,
I applied glue to the pcb first, then inserted the jack socket and positioned carefully while allowing the glue time to set.
It may be prudent to then try fitting the top cover back in place to check that the button still operates ok.
When happy all is ok, apply a layer of hot glue to anchor the socket firmly in place, keeping the screw hole clear.

The best way of testing your handiwork is to plug in an external UART and flash a Firmware Update to the S20... because if that succeeds, you know you will have TX, RX, +3.3v & GND and gpio0 button all available and working.

In the event of a problem, bypass the hack by unplugging the header plug from the header pins, and try to do an Update Firmware flash directly to the pins to check if the S20 is actually ok.

It is easy enough to add pullups to an external plugin module when needed,
but it is also easy to add them internally if preferred, as seen here on the left.
I have done two of my S20's with internal pullups, and two without.

A good value for internal 'fit and forget' pullup resistors would be 4.7K which should be suitable for use with 1-wire sensors or I2C devices.

Alternatively, the small DS3231 RTC module seen in the picture below already has two 4.7K I2C pullups included, and is small enough to fit into the S20 or the SV or TH.
NOTE:  It has a 5-pin header but only 4 pins used, so needs making a 5 pin version of the header adapter.


The Sonoff SV (far right) uses a low voltage (not Mains) supply which makes it well-suited for use in vehicles, so the addition of the RTC could offer handy time-keeping ability for modules that are 'on the go' and don't have internet access to a time server.
 
 
Just to give an idea of things, the picture on the right shows the Sonoff SV without any pins yet fitted, plus an unmade header plug with stripboard below it.
Also shown is an assembled header plug with 2 pullups, above an S20 which has had its pins fitted.


When done, the UART can quickly and easily be plugged in externally for any subsequent re-flashing if and when needed.

NOTE:  Birds can perch on EHT power-lines without being fried because they offer no alternative path for current to travel through.
Your USB supply may not be so well isolated, so unplug your S20 from the Mains before you Flash it, else the Flash might be more than expected.
  https://www.youtube.com/watch?v=CB5UXvMTNVo&t=39s
  https://www.youtube.com/watch?v=oBJyyEAw-6g
  https://www.youtube.com/watch?v=2IT6_ItjDbE
Use this if you 'cock up' your ESP IP address configuration and are unable to re-connect to fix the problem.
If Wifi-Basic finds the RX and TX pins linked together at startup, it will default up as IP address 192.168.4.1
It just causes a temporary return to the default address so you can connect to reconfigure the IP address.
The same is achieved whatever way RX and TX are linked together, but it doesn't get much easier than this.
Remember to remove the emergency TX-RX link afterwards or it will keep booting back to 192.168.4.1


IR Emitter LED
The drawing on the right shows how easy it is to add IR emitter capability (red wire denotes the anode).
A jack extension cable could permit targeting the IR signal delivery to specific equipment locations.
Or use an "IR emitter cable" (just over a quid from ebay). 
Don't confuse the "IR emitter cable" with an "IR receiver cable" which can look very similar.
Jack 3.3v power is available if you do want to connect an IR receiver, perhaps for an IR controlled socket.
Create remote IR Extenders by transmitting IR codes over wifi from IR receiver nodes to IR emitter nodes.
Using a 50ohm to 100ohm LED current-limiting resistor will protect the gpio output, but will reduce range.
For greater range, use jack power for a transistor circuit to drive multiple IR LEDs... (CCTV illuminator ?).