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.

Wednesday, April 3, 2019

Wireless Communications Using 433MHz RF Modules

As a compliment to my previous project (TFT LCD clock), I decided to build another TFT/Arduino unit that would serve as a remote station.  I could then make use of commonly available 433MHz RF xmt/rcv modules and wirelessly connect between the two units.  I also wanted an excuse to experiment with these wireless devices by playing with antenna orientation, configuration (coiled vs straight), etc. to establish their maximum range and transmission accuracy.  The base station located in another room of our home would transmit time information every few seconds which, in turn would be received by the remote station located in my ham shack (currently a spare bedroom).

The RF units (STX882/SRX882) were purchased from banggood.com for ~$3.00.  Next, I located an excellent resource and code library focused on wireless interfaces (RadioHead) and used their ASK (Amplitude Shift Key) drivers for my project.  This type of modulation provides a simplified, one way channel for transmitting and receiving data with decent data integrity.
                                                                                     
433MHz Receiver and Transmitter Modules
Each RF module connects to its respective Arduino unit via a single digital I/O pin.  This allowed for easy initial checkout by directly hard wiring the transmitting Arduino to the receiving Arduino (ie. the RF interface was bypassed), then sending a test message.  After ensuring message data was properly dis-assembled by the receive station, I removed the direct connection and plugged in the RF modules.  It actually worked the first time!!!  I did not check for maximum range but it works fine for the moment with a separation of ~50ft (from one end of our home to the other) so I am quite pleased.  As shown in the following image, I also added support for an environmental sensor to display the room temperature: