I’m still quite enamoured of the Sonoff, from ITEAD Studios, despite the fact that I can’t get their eWelink to work with a tablet (the app refuses to register you unless you have a phone number — which my tablet doesn’t have, of course — and the excellent suggestion from Jon Chandler in the support forum, “use Oogleg Voice” may well work, but only for ‘merkins living in merika). Sniffle. I really shouldn’t be surprised, though; previous experience has shown that anything connected to female sheep usually leaves me with a cold, wet arm or a body to bury (occasionally both).
Anyway, playing around with the unmodified device showed it as coming up on an IP address of 10.10.7.1 and happily assigning my laptop 10.10.7.2, but that’s as far as I got. All attempts to connect to it from the laptop failed miserably and I was also unable to get the Android “Smartconfig” app to do anything with it (if you’ve had success with any other method of getting the Sonoff to work with an Android tablet, please do leave a note in the comments section below).
So, I disconnected the wee beastie, ripped it apart and set about adding some pins to the existing programming header. A couple of warnings are in order here before I go any further, though:-
- Your warranty is null and void if you start screwing around with your hardware (but you knew that already, right!?!)
- You need to switch the unit off, unplug it from the mains and remove any cables connected to it before you start work (hopefully you knew that, too …if you didn’t, give yourself a sharp rap across the knuckles with wooden ruler and stop reading this right now!).
- You should let the unit stand for half an hour or so to let the capacitors on the mains side of the PSU discharge before taking the top off.
- DO NOT under any circumstances, touch any part of the PCB or component with a finger, soldering iron or USB-to-TTL adapter until all of the above instructions have been complied with.
ITEAD Studios have designed the board of the Sonoff with a socket for a programming header.
The available schematic and the board itself don’t quite match up. The schematic shows this as a five-pin header, but the PCB very obviously only has four holes. What are we going to be connecting? Well, we need pins for ground, receive data, transmit data and +3v3. We need the latter simply because we do not want to attempt connecting another device (PC/Laptop/UFO-anti-gravity-generator, or whatever) to something which is already connected to the mains (do NOT do that! I mean it!). At any rate, four pins is all we need and four pins is what we’ve got. We also need something that I’d been missing for a while …a 3v3 compatible USB<->TTL adapter (I finally gave in and ordered a couple a while back).
Adding the pins is fairly straightforward, but you should note that the ground pin is the one closest to the camera in the shot above, not the one with the squared-off hole. The squared-off hole is the 3v3 connection. If in doubt, get your meter out and test. Before you put the pin header in place it’s also fairly easy to see the “star” connections from the ground connector to the ground plane around it. The pin on the opposite end of the connector (next to the “J1” label and the big switch) is the 3v3 connector.
The long row of holes across the bottom of the picture is the connector for the 433MHz remote control board which is fitted to the Sonoff “RF” models, but missing on the base model.
With the pin headers in place, it’s not very easy to see either the silk-screen around pin 1 or the “J1” label, so it might be a good idea to label the ground pin using something like an indelible marker pen so that you’ll know which way round to connect the USB<->TTL converter leads the next time you take the top off.
Now that we have the header pins in place, we need some firmware to burn to the ESP8266 on the board. I’d like to point you towards Theo Arends’ Sonoff-MQTT-OTA-Arduino package on GitHub. It’s a drop in replacement which has a very versatile interface, allowing control of the relay, as well as status reporting and some configuration, from both MQTT and from the Arduino-IDE serial console window. In addition, the button on the Sonoff will also provide manual control of the relay, start SmartConfig mode, start OTA update mode and force a reset to defaults, depending upon how many presses or how long you hold it down. Changes to the relay state triggered by the button also automatically generate an MQTT state-change message, so your application should always be able to track state of external switch events. Neat! Theo also maintains a non-Arduino version if any of you masochists out there prefer fiddling with the SDK.
To update your Sonoff, make sure that no mains cables are attached, plug your 3v3 USB<->TTL converter cables onto the newly installed header pins (+3v3 to the pin next to the switch, ground to the pin next to the row of holes for the 433MHz module) and then hold down the switch button while plugging the USB connector into your computer. Burn Theo’s firmware to the board using whichever method you are familiar with.
Note that the USB converters don’t usually supply enough current to run the ESP8266 when it fires up the radio (and it definitely won’t supply enough current to power the relay coil), so don’t be surprised if you see a couple of start-up messages followed by a crash or watchdog reset; just disconnect your USB cable and put the unit back together again (including all of the covers), before applying mains power. Once it’s all back in one piece and powered on, you can check basic functionality by pressing the button. A single press should produce an audible click from the Sonoff as the relay toggles state, followed by two brief flashes of the LED. If you have “mosquitto_sub” available (or one of the Android MQTT monitor apps), you can fire it up and monitor the topic “stat/#” to see output from your newly updated Sonoff (assuming that you set up your own MQTT server details in the firmware defaults). The mosquitto_sub command would be:-
mosquitto_sub -h Your.MQTT.Server -t sub/#
Use mosquitto_pub to publish an MQTT message to the cmnd/sonoff topic to have your Sonoff toggle the state of the relay:-
mosquitto_pub -h Your.MQTT.Server -t cmnd/sonoff/power -m "toggle"
Hopefully you’re now in business and your Sonoff is happily clicking away as you play with MQTT.
Update – If you’re having trouble compiling Theo’s package (CFG_Default not defined, etc), I’ve put together a very slightly modified package with the function prototypes broken out into separate header files which should compile cleanly for most people. It also has a minor pre-processor tweak which will reset the MQTT_MAX_PACKET_SIZE and MQTT_KEEPALIVE values to Theo’s recommended settings of 1024 and 120, without the need to modify the original PubSub library file.
Next …the next article in this series is a tutorial on using Theo’s new firmware.