Thursday, April 25, 2019

Sonoff Smart Switches

The Sonoff series of smart switches (produced by Itead) are based upon the ESP8266 and ESP8285 microcontrollers.  They are Wi-Fi compatible thus making them useful for interfacing remotely with iOS and Android devices via a number of commonly available apps such as Blynk, eWeLink, etc.

Basic R2
For basic switching operations, these units are designed to work right out of the box using their standard, built in firmware.  All you need is to load an app onto your phone for controlling it and you're all set.  The app sends a command to the Sonoff which in turn activates a relay on the board, thus controlling the power delivered to a load.  However, I wanted to go one step further and use custom firmware to make the device self contained (no app required).

I purchased a basic model (Sonoff Basic R2) for ~$7.00 and begin experimenting with different code libraries freely available on the web.  The goal was to turn an external device hooked to the Sonoff on and off at a predetermined time each day.  I found a library that contained code for accessing an NTP (Network Time Protocol) server.  This allowed me to fetch the current time off the web.  Next, I hard coded specific times for turning my device on and off.  The drawback to using fixed times is they can only be changed by altering the program and recompiling it.  For now this will work fine, but down the road I might use an app on my Smartphone to program an on time and off time on the fly.  This, of course would require a one-time modification to the program.

In order to load (flash) the compiled code onto the Sonoff board I needed an interface between the computer and the ESP chip.  This was because the USB end uses 5.0V while the ESP circuitry uses 3.3V.  I purchased an FTDI based USB to TTL interface board from Amazon, transferred my code and was up and running in no time.

Neat little devices.

Update (June 2019)
After using my Sonoff for a while with hardcoded on/off times embedded in the firmware, I finally decided to experiment with the Blynk app on my Smartphone to allow for dynamic changes to the on/off parameters.  Blynk comes with coding examples already written for specific applications so I used one suited for my needs and simply copied and pasted the appropriate sections into my existing program.   I also made sure to store any changes to the on/off parameters into non-volatile memory to ensure the Sonoff would work when power was lost and then restored.  After loading the newly compiled code into the Sonoff chip, it ran w/o any hiccups.  So easy!  Now I can change the on/off times if needed w/o reprogramming the chip and the Sonoff will respond accordingly.

No comments:

Post a Comment