Here are two similar but also rather different WiFi controlled mains switching relay modules that both use the ESP8266 WiFi SoC. Both come with a nice case, can be reprogrammed with your own firmware and are exceedingly cheap for what you get.
I’m using these with MQTT and Node-RED so that they seamlessly integrate with the other WiFi and 433MHz remote mains devices that I already have. In day to day use there will be no way to tell which type of device is controlling a particular light or appliance which is just how it should be.
The Sonoff is designed to be used with Itead’s own mobile app but can easily be flashed with other firmware if you solder some headers on or use pogo pins to connect to the power and serial pads on the board. It’s a fully customised board with the ESP8266 chip integrated onto the PCB (not using a module) and has 1MB flash.
The relay is on GPIO12, active high
The green LED is on GPIO13, active low
The push button is on GPIO0*
As mentioned the programming header is unpopulated, it’s also unlabelled. If you refer to the picture on the right the connections are as follows:
Red wire: 3V3
Yellow wire: ESP8266 RX (connect to TX on your USB TTL converter)
Orange wire: ESP8266 TX (connect to RX on your USB TTL converter)
Black wire: Ground
I like this board, it’s all integrated on the one PCB, the isolation looks reasonable, the AC tracks are thick and/or have additional solder applied to increase the current handling capacity, there is a MOV for over voltage protection and it all looks well made. Itead state the max load is 10A and that seems reasonable.
Itead also make an E27 light bulb adapter called Slampher which uses a similar board but I haven’t tried one of those.
Electrodragon WiFi IoT Relay Board
The second board is a new one from Electrodragon. This has two independent relays compared to the one in the Sonoff and comes with headers already soldered on for programming. It only has the standard AT firmware loaded so reprogramming will be a must for anyone using these units.
Unlike the Sonoff which integrates everything onto the PCB the Electrodragon board uses a standard ESP-12F (4MB flash) module and a separate AC to 5V module (feeding an AMS1117 regulator to produce the 3v3). One advantage to this design is that by removing the power module and bridging the inputs and outputs for it, it would be easy to convert this into a low voltage relay board which might not be a bad idea.
Electrodragon don’t explicitly state the maximum current that their board can handle, only quoting the spec of the relays (10A @250V) but the long tracks down the side of the board don’t look anywhere near heavy enough to handle 20A (10A per relay) and I’d only be confident using this for low current applications.
Personally I’d also like to see a bit more isolation in a couple of places too (input to power module and output from one of the relays to the ground pour) but I’m not an expert on this and it might be acceptable. The build quality of the power module isn’t great either (see below).
One definite design problem with this module is that the case was clearly only ever designed for one pair of terminals at each end and they have tried to cram two sets in for the output of the two relays resulting in one that is partially obstructed potentially leading to crushing and disconnection or worse, shorting. There is no way that cover would go on with two sets of cables either and I’d not be happy using both outputs without modding the case.
Also on the board are two miniature push buttons connected to GPIO0 (for flashing) and GPIO2 and three on board LEDs (one for each relay and one connected to GPIO16) but none of these are accessible from outside the case.
There is also a footprint for a DHT11/22 temperature & humidity sensor but I’m not sure how useful this would be as mounting the sensor directly on the PCB won’t be much use inside the (unvented) case and right next to the power module and 3v3 regulator where it is going to be affected by circuit heat. I did wonder if the intention here was to monitor for over temperature situations but you wouldn’t use a DHT for that. Still, it is another easily accessible GPIO if nothing else and the sensor could be connected on leads and placed outside the case I suppose.
Relay 1 is on GPIO13, active high
Relay 2 is on GPIO12, active high
The user LED is on GPIO16, active high
BTN1 is on GPIO2
BTN2 is on GPIO0
DHT sensor is on GPIO14
The header is pre-populated and labelled on the PCB, it gives access to power and serial as well as access to several more GPIO pins and the ADC.
Helpfully the pins are labelled and give access to:
RX (connect to TX on your USB TTL converter)
TX (connect to RX on your USB TTL converter)
I’m not really as keen on this board, some design decisions that seem a bit odd to me and I do wonder if they have just added the second relay in an attempt to “one up” the Sonoff. There is some potential there though, the access to the extra GPIO pins and the modability for low voltage use are plus points.
Here are the boards from both units, the green one is the Sonoff, red is the Electrodragon.
MQTT Firmware options
As my home automation is all MQTT based all I am interested in is being able to turn the relay on/off via MQTT. Initially I was going to use a simple version of my own then I came across this from Theo Arends on Github which is available in both native C and Arduino flavours and supports OTA updates which is a very nice feature to have.
What I am actually using at the moment is ESPEasy, this has a wealth of features built in and supports GPIO control via MQTT or a REST interface and supports lots of sensor devices and is all configurable through a web interface including OTA updates. As the name suggests it is very easy to use.
Once you have a USB to TTL converter attached programming either of these modules is as straightforward as with any ESP8266 device, hopefully it should be obvious that the AC should not be connected while messing around with these things like this or things won’t go well!
Power up while holding down the button (Sonoff) or BTN2 (Electrodragon) and the ESP8266 will go into programming mode.
I use esptool.py so uploading is something like:
esptool.py –port /dev/ttyUSB1 write_flash 0x00000 ESPEasy_R78_512.bin
Once flashed, power cycle the device and check for a new wireless access point named ESP_0, connect to that and then browse for any web page and you should be redirected to the setup page (or just enter http://192.168.4.1) which will list nearby WiFi networks, select the one you want to use, enter the password and click connect. The device will restart and then show you its IP address on the network you just selected.
Back on your WiFi network, connect to the new IP address for the device and you will get the System Info screen, click Config and on that screen you need to set the following at a minimum:
Unit nr: This must be unique to the device
Protocol: Select OpenHAB MQTT (this is just standard MQTT, it doesn’t matter if you aren’t using OpenHAB)
Controller IP: Set to your MQTT broker address
Set port,user,password if required for your broker
Now go to Tools, Advanced and set: the Subscribe Template to the the topic you want to use to control the device, eg. %sysname%/# where %sysname% gets replaced with the name you entered in the config page.
Not necessary if you are not using any sensors etc. but you can also change the Publish Template to set the topic for information coming back from the device.
You can now control the device via MQTT, for example you can set GPIO 12 high (turns the Sonoff relay on) by sending a message of 1 to the topic sonoff1/gpio/12 or set it low by sending message of 0
If you don’t speak MQTT you can use the equivalent REST interface like this: http://sonoff1/control?cmd=GPIO,12,1
So two very useful and very cheap boards, for the reasons mentioned above I prefer the Sonoff but both boards seem to be reliable so far and I can see more being deployed here. I’ve not fully explored the sensor side of ESPEasy yet but what I have seen so far is very impressive, definitely one to watch.