Wiring and setting up
The Sonoff is based on an ESP8266 controller, which we erase and reflash with Espurna firmware and configure to connect to our local mqtt broker.
Unscrew your new Sonoff and and wire up a USB-to-Serial converter by propping the header pins into the serial holes (make sure to use 3.3V). Hold down the button on the Sonoff as you plug it in (to enable the boot mode), then execute:
esptool.py --port /dev/ttyUSB4 erase_flash
Repeat by pulling the USB connector out, holding the button and pushing in again, then execute:
esptool.py --port /dev/ttyUSB4 write_flash --flash_size 1MB --flash_mode dout 0x00000 itead-sonoff-pow-r2...firmware.bin
Alex has built Espurna firmware with Platform.IO following the xoseperez/espurna/wiki/PlatformIO instructions, applying this change to the settings. The backup binary images are in DoESLiverpool/somewhere-safe/tree/master/Network/Sensors/Espurna/Sonoff-R2.
It is possible to do Over-The-Air updates through the Espurna webUI when Alex makes new binary images with the latest features.
One feature we are waiting for is a [https://github.com/xoseperez/espurna/issues/1697](button relay lock) that is waiting on a pull request. This will disable the relay switch in cases where we want to purely measure the power draw, but never turn the device off (eg the fridge).
Pull out the USB and connect again. The Sonoff is now running the Espurna software. There will be a blue flashing light (to signify wifi hotspot mode) and the button will toggle the relay state.
Connect to the wifi hotspot of the form "ESPURNA-XXXXXX". The Wifi password will be "fibonacci". Then go to the page "http://192.168.4.1" and fill in the credentials user: "admin", password: "fibonacci".
You will be forced to change this password, so set it to "bubbl1n0". Powercycle the Sonoff again by pulling out the USB and connecting again and login to the wifi and webpage with this new password.
You now have access to the Espurna WebUI. Test the switch/relay works to verify you are logged onto the device you think you are. Note down the name of the device, which is of the form "ESPURNA-XXXXXX" where the XXXXXX is the last 6 hex characters (3 bytes) of the mac-address, which will have been printed out during the esptool.py flashing.
If you have problems saving because "passwords are different", try moving to a different browser where the default password is not cached.
On the MQTT tab, set Enable: "Yes", Broker: "mqtt.local", then click "Save".
On the Sensors tab, set Read interval: "1 seconds", Report every: "1", then click "Save".
On the Switches tab, set Boot mode: "Always ON", then click "Save".
On the Wifi tab, set Network SSID: "DoESLiverpool", Password: "decafbad00", then click "Save". Say "Yes" to the dialog asking to reboot and reconnect.
Connect to the DoESLiverpool wifi and type:
mosquitto_sub -h mqtt.local -v -t "+/ip"
to see the new Sonoff/Espurna name and ip number. You will need it go get to the WebUI in order to change settings, schedules or upload new Firmware.
If you've messed up the connection to the mqtt broker and it doesn't connect in order to give out its ipnumber, you can still find your device using a command like "sudo arp-scan 10.0.38.0/24" (or similar).
Put the Sonoff back in its box, write the 6 hex digits on the box, or on the side of the device in indelible ink (or even better, use a vinyl lettering).
When you come to connect it to a mains cable, remember that there is a Live Brown bear under the Neutral Blue sky on the Green/Yellow Earth.