A While ago I setup a Pi to control some quite cheap 433Mhz RC mains switches I got from Maplin [ code N78KA]. The controllers were connected to Lamps to automatically light up various rooms in the house after dark.
The RC switch comes with a simple 4 way hand controller and initially I was just planning to hack this. However looking on the internet I found a few examples where people had used Arduino’s and Pi’s with cheap RC transmitters to ‘mimic’ the signals generated by the hand controllers, to operate the remote units. I could not find any examples of people who had used the exact Maplin RC switches I had, but reading around it looked like the
codes were relatively common between products. After a very frustrating day of trial and error I decided to apply a bit intelligence and got the oscilloscope out and started comparing the serial data coming from the Pi and that produced by the hand controller. Quite quickly I realised that the data from the Pi was correct but back to front, having correcting this I was able to activate the remote switches at will !
From here I wrote some simple Python code to control light positioned around the house, planning to tidy it up. Well that was over a year ago ……
The main drive to produce a mk2 version was to avoid me having to manually alter the timings as the length the day changed. It had always been a plan to sort this out with a light dependent resistor (LDR) but I just never got around to it.
Having decided that I could not face another autumn with the family moaning about the house being dark I decided now was a time to get this updated. In addition to auto correcting for the daylight I also wanted to improve the status feedback. The mk1 version simply had a single LED which flashed every few seconds to show the Python code was still running.
For the display I landed on a miniature OLED display I had picked up from a JAM earlier in the year. You can see the details for this on my last blog posting. As part of the ‘upgrade’ I decided to start from a clean installation of the latest Rasbian . This necessitated trying to re-learn how to get the RC serial libraries working again as I had not written it down the first time around [there is a lesson here], somehow in this process I ended up with different library [https://github.com/lexruee/pi-switch-python] which turned out to be a whole lot easier to work with than the one I used first time around.
The next thing to sort out was adjusting for the changing daylight hours. Using an LDR to detect darkness was fine until my daughter left the light on in the room where the Pi was….. I wanted a self-contained control unit so running wires from a remotely mounted LDR attached to a window was not any option. The solution I landed was to use sun rise and set times, initially I looked at getting these from the web, but that assumes a good long terms stable wifi link, which is a little unrealistic in my experience. After a bit of searching I found an quite old [2004] bit of s/w called sunwait. I think it was originally developed to be used with cron, but can be run from the command line to give sun rise / set times for any given latitude and longitude. The the output is a fixed format so it is possible to reliably grab the target information without too much difficulty. I have written a page in the ‘random Pi notes’ section of my site which describes how to install and basic usage of sunwait.
Having tested the individual bits of code I pulled them all together. The resulting application and a number of the test programmed I used to figure out how bits worked can be found on my git-hub. I have tried to comment the code so it is understandable – not least as I struggle to otherwise. Currently the code allows for each controller channel to define an on time relative to sun-set, an off time and a random element. The timings are adjusted daily, in-line with changes to sun rise and set times. In addition, when the code is first run it does a basic check to see if it has missed any comments so it will recover straight away from power cuts which are quite comment where we live. The code is still a little work in progress although stable it needs a tidy up and a cleaner way to setup the timings.
Hardware wise I have manged to get everything into an old Pimoroni Pibow case. Pi wise it
is running on an early [no mounting holes] Pi B. I find applications like this a great way to use older hardware which is bit slow to experiment with but still more than fast enough for an application like this. It also helps to keep the costs down as I had all the bits already.
Links to Instructions / help
Install instructions for sunwait
Setting up the RC433 switch library
Link to my Git-Hub – you can find my controller Python code here
Maplin RC controller product codes – N78KA [ 1 off] N38HN [ 5 off]