We are now ready to download. At this stage one can either go straight ahead and flash a Sonoff switch or one can first flash an ESP8266 module. I did the latter, partly because my switches had not yet arrived (just arrived as I type this!) but also as a precaution as the flashing of the Sonoff switch is a one way step as the original firmware is not publicly available as far as I am aware. I have a couple of NodeMCU boards. These are easy to connect, having their on-board USB to serial converter. However the nodemcu reset method does not work with this system. So leave the Tools>Reset Method set to “ck”. Do the normal manual flash setup by holding the Flash button (GPIO 0 to ground) while pressing and releasing Reset (Reset to ground). I am not sure if this has a timeout or maybe I didn’t hold GPIO 0 low for long enough but I did need several tries, including doing this while the Arduino IDE is compiling!

If you want to check the responses - the relay output is D6 on NodeMCU boards. On ESP12’s this is GPIO 12. The LED output is D7 (NodeMCU) or GPIO 13 (ESP12’s).

Sonoff switch.

WARNING: I have to say “do not connect to mains under any circumstances where the enclosure is open”. Note that the PCB (at least on the Sonoff Basic (in-line switch) only has ‘single isolation’ distances between the low voltage section and the mains. So one should treat any part of the Sonoff circuit as mains voltage. The Sonoff-Tasmota GitHub does show connection of a temperature and humidity sensor to a Sonoff S20. I would not do this because of isolation concerns. – so if you want to do this get an ESP12 or NodeMCU module and set this up separately with a proper double isolated or earthed power supply.

The Sonoff S20 plug-in switch is a good starting point as it does not need any mains wiring. This can be opened by removing one screw (under the security seal) and prising the case open. The photo below shows where the tags are. Squeezing the case at these points helps.

USB serial converter

My favourite converter is the FTDI version. However this does not have adequate capability to supply the Sonoff 3.3v needs. The FTDI specification says maximum 50ma. The next best alternative is one using the CP2102 chip. This however has a 100ma limit that is still not enough. Clearly many are using this converter direct but there are also reports of loading failing. I would limit the time it is connected as it will warm up under the load. Do this at your own risk. The ideal solution is to have a 3.3v regulator as well, e.g. an AMS1117 3.3. I made up a small PCB to enable this. See Programmer for Sonoff devices.

My sequence for programming is as follows:

Open the Arduino IDE.

Under Tools check the settings are as on the Wiki.

Make any edits required to user_config.h . I set the Wifi SSID and password and MQTT broker address and timezone/daylight savings details.

Click ‘verify’ to check it compiles OK.

Plug the USB serial converter (on its own) to the PC. Note the Port number.

Now disconnect the USB serial lead from the PC and connect it to the Sonoff switch. Check the ground and 3v3 connections are the right way around (ground is connected to the ground plane on the Sonoff PCB).

Hold the programmer so the contacts are secure while also pressing the button.

Now plug the USB lead into the PC, check the port number is correct (in Tools), then click download.

I continue to hold the button throughout the programming because I don’t want to disturb the connections.

When done you should see a screen as below:

The Sonoff needs a couple of bits of information to connect to our system: the local network wifi SSID and password, and the Pi IP address. There are several ways to do this, one of which is to modify the config.ino file as noted earlier. If you have a smartphone you can (after reassembling the switch) press the Sonoff button 4 times fairly quickly to put it into a web-server mode. The LED will flash. I had to try several times to get this to work. Then, on your smartphone look for the new Sonoff network and connect. A webpage will appear where you can set the data required. The Pi IP address goes into hostname. I also changed the second SSID and password to something long and basically unusable.

Alternatively it can be setup via the serial connection just after loading. Open the Arduino Serial Monitor (under Tools).

Commands to enter:

SSId yourWiFiSSID

Password yourWiFiPassword

MqttHost 192.168.x.y (PI IP address)

You can also enter SSId1 and Password1 followed by something long and unusable to effectively disable this.

Now you can box up the Sonoff switch, open Node-Red and the Node-red dashboard and click the switch button and look at the debug tab that should now include the responses from the Sonoff. So we have another major step achieved - our first switch being driven from a PC/smartphone.

Up to now we have not mentioned security. There is the possibility to use encrypted communications. This is quite complex to set up and probably more appropriate where a cloud based broker is being used. There is also the option to add usernames and passwords for all the connected devices and disallowing anonymous users. This is pretty simple to set up. And so now to Security.