Imagine yourself on an autumn night, taking a walk with a girl of your dreams. You walk past your house and you feel that she’ll be yours for the rest of your life, but you’re just a single cheesy one-liner away of taking her home with you. Then it strikes you! You say: “girl, you turn me on just like my iPhone turns on the lights at my back porch!” – then you press a button and bam! She jumps onto you and asks you to marry her. She thinks you’re a genius. But what she doesn’t know is that you only bought a cheap Chinese relay, a USB programmer and a bunch of wires to impress her. Here’s how to do this in detail:
- Sonoff Basic relay (Amazon UK) (Amazon US) (manufacturer*)
- a CP2102 USB/UART converter with 4 female-female jumper wires (Amazon UK) (Amazon US)
- a 2.54mm male pin header connector (Amazon UK) (Amazon US)
- a screwdriver
- a computer with a USB port and a Wi-Fi card – we’ll use a Windows PC in this guide
- working Wi-Fi – you’ll need to know your network name and your password
* ships from China, so you’ll probably have to wait a few weeks to get it delivered
- NodeMcu Flasher tool for Windows (32-bit) (64-bit)
- Supla firmware for Sonoff devices (from here)
- Supla app – available for iOS, Android from their respective stores (website) – there’s also a Windows Phone version, but you have to compile it yourself
- A Supla Cloud account – you’ll need to check your e-mail after registering to confirm the account creation
One thing that I know from my experience, is that pretty girls love relays – I’ve chosen the Sonoff basic relays, because they are cheap and capable of being controlled by Wi-Fi. We’ll connect that to a Supla Cloud free web-based home automation system, which also features the ability to be controlled from a smartphone app. If already feel a bit lost – don’t worry, just take a look at this fancy diagram:
Of course there’s a lot more stuff going in the background, such as authentication, etc. But all clear now, eh? All right. But…
The plot twist:
I mentioned Supla Cloud, which isn’t natively supported by the Sonoff relays. They have a firmware that uses a cloud-based home automation software that’s called Ewelink, which more or less serves the same purpose as Supla. Why not use it? Well, for starters it’s easier to keep Supla secure, as you can set up your own local open-source Supla server on a cheap Raspberry Pi and not worry about your home automation relying on the security of a Chinese server. Then there’s the default OTA firmware update mechanism which is compromised according to this source. Supla let’s you disable the OTA function. But the essence of the whole plot twist is this: we’ll need to manually upload the Supla firmware to our Sonoff relay. Fortunately, that’s what the CP2102 converter is for. It will allow us to upload the binary firmware files to the relay.
Time for updating the Sonoff firmware! First, let’s deal with the hardware part of the process. Get your CP2012 converter and 4 wires and look closely at the connectors – you’ll need to connect one end of your wires to RXD (green wire in my case), TXD (red), GND (black) and 3V3 (white).
Your male pin header connector probably came in the shape of long strips which can be broken down into smaller pieces. Let’s tear away a 5 pin piece from this. Let’s number the pins 1-5 from left to right, and connect your wires like this:
- PIN 1: 3V3 wire (white in my case)
- PIN 2: TXD wire (red)
- PIN 3: RXD wire (green)
- PIN 4: GND wire (black)
- PIN 5: not connected to anything
Disassemble your Sonoff device (it’s easy – just 4 screws holding the covers, take the device out of the casing. You may want to solder the pin connector to your Sonoff board and then connect the wires, but if you feel courageous enough (remember what you’re here for) or just plain lazy, you can just first connect the wires and then hold the connector with your hand during the firmware update process.
Now, the software. Download the Supla firmware for Sonoff devices, unpack the .zip file to a folder of your choice, connect the CP2102 converter to the USB of your computer and then open the NODEMCU firmware flasher. Go to the Config tab and do the following:
- Check the top two checkboxes on the left side
- Click the first cog icon, point the file explorer to the folder where you unpacked the Supla firmware to and choose the boot_v1.5.bin file. Click the second cog icon, point it to the same folder and select the sonoff_ds18b20_user1.1024.new.2.bin file.
- Type 0x00000 in the first memory address field and 0x01000 in the second one.
It should look more or less like this:
If it does go to the Advanced tab and set the following options:
- Baud rate: 115200
- Flash size: 1MByte
- Flash speed: 40Mhz
- SPI Mode: DOUT (if the flashing won’t work you might want to try different options here)
Go to the operation tab, it should auto-detect your COM port (mine was COM3). Connect your wires to your Sonoff device like on the picture below. Be sure to hold the black button on the relay while doing that, otherwise it won’t enter the flash mode, which we need for the firmware upload!
If all is ready, click the Flash button. The progress bar should go all the way two times – each time for one of the .bin files it was supposed to upload. If it doesn’t, be sure that the device is in firmware upload mode (LED shouldn’t be flashing), that the pins are sitting firmly inside their holes and the wires are connected correctly.
Now the Supla firmware is uploaded – disconnect the pins from your Sonoff, as it’s time to configure the relay!
Log in to your Supla Cloud account. Note the variables that are displayed on the first screen you see:
Please note those down – you’ll need them in just a little bit. Connect your CP2102 coverter to your Sonoff again, but this time do it without holding the black button. This will power on your relay in its standard operating mode – green LED should be blinking out of its mind. Right after flashing the firmware it doesn’t know which Wi-Fi network to connect to yet, so let’s help it a little bit. There should be a new Wi-Fi network showing up, named SUPLA-something, please connect to it.
Open your favorite web browser and go to http://192.168.4.1 to display the Supla configuration panel. Type in your home Wi-Fi network name and password and the Location Identifier and Password from the supla-dev variable section (the yellow one) of your Supla Cloud account. Leave Firmware Update at No to be extra secure and disable the OTA update function.
Click save and if you did input everything correctly, your device should be connected to the Supla Cloud servers now and can already be accessed from there. Now you can disconnect the CP2102 and put your relay back in its enclosure (don’t screw it on yet, though). Now if you want to control anything using your Sonoff relay you need connect the wires of the device of your choice (I did that with my balcony lights first). I’d suggest buying a two-wire extension cord, cutting it in half, stripping the wires and connecting it to the relay.
Make sure that you don’t operate on the device while it’s connected to live electricity, don’t connect the wires while Sonoff is still connected to your computer and just be extra carefull while doing that! You will be operating on something that will be connected to about 230V of live current, which can potentially kill you. Pretty girls don’t like dead people. If they do, you should not consider them pretty anymore. If you’re not sure what you’re doing please contact a qualified electrician to do that for you!
That said, connect the extension cord like so:
- The part that goes to the male plug: blue wire to Sonoff input N (neutral), brown wire to Sonoff input L (live)
- The part that goes to the female plug: blue wire to Sonoff output N, brown wire to Sonoff output L
Plug the device of your choice to the female end and plug the male end to a power outlet. If it doesn’t explode and the green LED flashes at least once that means it should be working fine.
You can now download the Supla app for your phone. Open it and enter your supla-client credentials (green section after you log in to Supla Cloud):
Tap the green button and voila – you should be able to control your Sonoff from here! You can also press the physical black button on your relay to toggle it on or off.
The final notes:
Since it’s just a fancy on/off switch, you can use it with a whole variety of home appliances – I myself used it with a kettle, balcony lights, speakers and power outlet extenders. Please remember that the relay is not water resistant, so it’s a really bad idea to expose it to water or high humidity.
This of course can also be connected to a home automation server with a web server. All of that Apple HomeKit enabled. We’ll look into setting up one of those on a Raspberry Pi Zero W in our next guide!