OverviewTurkey day (Thanksgiving Day for those reading outside the US) is over and according to decades of holiday tradition, it's time to hang the exterior Christmas lights on the house. In the past I have always used simple mechanical timers to automatically turn on and off the lights based on fixed times. This year, armed with my Raspberry Pi, I wanted to create a simple DIY Raspi Christmas Lighting Timer that would control the power to the Christmas lights automatically based on sunrise and sunset times.
The wiring for this project is quite simple. All we need are two wires to connect from the GPIO (P1) header on the Raspberry Pi to the PowerSwitch Tail power controller. Using male-to-female jumper wires make the task truly plug-n-play ... we don't need to solder or stip any wires.
The PowerSwitch Tail power controller works by turning on the high voltage AC circuit when a DC input of 3-12 VDC (3-30ma) is detected on its input terminal. When no input voltage is detected, the high voltage AC circuit turns off.
Fortunately the PowerSwitch Tail power controller input can be triggered directly by the Raspberry Pi's TTL 3.3 DC voltage supplied on its GPIO pins. This means that no additional transistors, relays, or other circuitry is needed to perform the switching.
In this project, we will use Raspberry Pi P1 header pin #12 (which is GPIO #1 in the Pi4J/WiringPi pin numbering scheme) as the positive control signal and we will use Raspberry Pi P1 header pin #6 for GND (ground). See the wiring diagram below. Insert the female end of the jumper wire on the Raspberry Pi GPIO header pins and then insert the male end of the jumper wire in the PowerSwitch terminal and tighten the captive screw to secure the wire in place. Make sure that your Raspberry Pi is not plugged in while attaching wires to prevent accidental shorts.
Below are photos of the completed and assembled project. Click any image to enlarge.
The software that determines sunrise and sunset times and controls the GPIO pins is Java based, therefore you must install a Java runtime to use this software.
To install the OpenJDK, simply enter this command via the Raspberry Pi's console:
> sudo apt-get install openjdk-7-jdk
(If you would prefer to install the Oracle JDK, please click here and read this article.)
The source code is provided as open source software and you can download the entire project from Github.
To run the software, a pre-compiled JAR is made available so that you don't have to compile the software on your own. Simply use the following command on the Raspberry Pi console to download the software to the current working directory on your Raspberry Pi.
> wget https://github.com/downloads/savagehomeautomation/raspi-sspc/raspi-sspc-1.0.0.jar
This software program also uses the Pi4J Project to manipulate the GPIO pin states on the Raspberry Pi; therefore, you must also download this prerequisite library to your working directory.
> wget https://github.com/downloads/Pi4J/pi4j/pi4j-core-0.0.3-SNAPSHOT.jar
To launch the program, use the following command:
> sudo java -classpath classes:./'*' SSPC
When the program starts it will prompt you for your longitude and latitude. These coordinates are used to calculate the sunrise and sunset times for your specific geographical location. You can use this web page to determine your specific longitude and latitude: http://itouchmap.com/latlong.html
At this point the program will continue to run and it will turn the power controller ON or OFF depending on the current time and it will schedule the next power event on the next available sunrise or sunset time.
Re-entering the longitude and latitude coordinates every time would be annoying, so you can also use command line arguments to start the program with specified coordinates. You could include this in a shell script to launch the program with the proper command line argument every time.
> sudo java -classpath classes:./'*' SSPC -longitude=-73.985538 -latitude=40.748362
Also note that while the program is running, you can manually force the power controller power state ON or OFF using the "on" or "off" commands via terminal.
The "help" command will display all the supported command options provided by the software:
Just leave the program running and it will automatically turn ON and OFF the power controller at the sunrise and sunset times for your location.
As you can see this is a very simple project to build and the software can be easily modified if you need some unique behavior. You can additionally take steps to include launching this program with a startup script if you want to ensure that it is automatically restarted each time the Raspberry Pi boots up.
This is an excellent example of an embedded Java application running on the Raspberry Pi and making use of the Pi4J project to control the hardware. This Java project includes a Maven build so you can easily re-build the project directly on the Raspberry Pi or your desktop if needed using Maven commands.
I hope you enjoyed this article and maybe thought of a few creative modifications that you can do in your home. Please add a comment if you find this project helpful or would like to share any modifications that you add to it.
Related Holiday Projects:
This page lists the articles posted on this blog about the Raspberry Pi