Tuesday 13 September 2016

Sonoff Wireless Switch

All for less than a fiver!
This little baby is an ESP8266-powered wireless mains relay from ITead. Although it comes with firmware installed which will suit most people, that requires an internet connection as it needs to talk to the Amazon cloud. Luckily for us, some people have done a lot of hard work to provide several flavours of open replacement. I went with the Arduino one and version 1.6.11 of the IDE.

Preparation

  1. Get the ESP8266 core for Arduino by following the instructions. I used the git version.
  2. Get the Arduino version of the Sonoff firmware and read the prerequisite section of its wiki.
  3. I also had to specify version 3.1 of MQTT in PubSubClient.h for correct interoperation with mosquitto on Raspian.
  4. Edit the sketch's user_config.h for your LAN.
  5. Under the IDE tools menu, set Flash size: "1M (64K SPIFFS)"
  6. Solder a pin header onto the board and connect an FTDI cable to the headers.
  7. Holding the Sonoff button down, plug the FTDI cable into the host and upload the firmware.

Setup

  • The Arduino serial monitor comes in handy for troubleshooting, set the baud rate to 115200.
  • The Arduino IDE allows specification of debug level, under the Tools menu, to get firmware debugging messages on the serial port. (Set this prior to upload, obviously.)
  • I had to enable 802.11b networking on my router, before the Sonoff would connect.
  • I installed mosquitto on a Raspberry Pi, and enabled rsyslog on it. I also installed mosquitto-clients on my laptop to keep an eye on the MQTT stream.
  • On power-up the Sonoff creates a temporary access point, allowing direct configuration of networking parameters, if you've made a mistake, at IP address http://192.168.4.1. (This configuration page is also available during normal operation, at whatever address you've configured for it.)
  • Once this firmware has been flashed, it can be upgraded OTA. (I installed nginx on my laptop and had it serve files from my ~/www directory to facilitate this.)