Raspberry Pi - Sunrise / Sunset Timer for Christmas Lights
Overview
Turkey 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.Shopping List
Hardware
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.
Software
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.
Final Thoughts
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.
Merry Christmas!
Related Holiday Projects:
More Raspberry Pi Projects and ArticlesThis page lists the articles posted on this blog about the Raspberry Pi
Reader Comments (31)
I am also getting errors: Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.3:run
First, I know this project is 2 years old already and I am late to the game.
Second, awesome project! haha I wish I would have thought of this a long time ago.
Third, I want to use this to control the outdoor lights of a multi-tenant building. Currently, in my testing (which is just the pi turning an LED on and off at my desk), the output only comes on ~40 minutes after the scheduled sunset time - off seems to be pretty close to the sunrise time. I haven't been sitting here the whole time watching it to determine exactly what the delay is though. The current time and scheduled times are pretty much perfect, but the actions are off a bit. Any thoughts?
Thanks.
@All
I have updated the 1.0.1-SNAPSHOT build to work with Maven 3.
@Aaron
I'm not sure what would cause the delay if the sunset time is displayed correctly. Maybe the current time on the Raspberry Pi is a bit off? We are just calling into the Java Timer object and scheduling a future timer task to be invoked. We could use a more sophisticated scheduling library if needed. I have setup a unit here to test the sunrise and sunset times and have a second unit detecting the HI/LO changes on the GPIO pin and recording the times for comparison. So I'll let the run for a few days and see what results I get. Also please make sure you are using the latest 1.0.1 -SNAPSHOT build -- That's the one I am using here. It includes a service dameon allowing the program to run in the background.
Thanks, Robert
So sorry, I figured out my own problem. Bozo. Anyway, I'm starting from scratch for other reasons (adding an LCD to show the program output live), but I'm having problems downloading the .jar with the address in the article. Did you remove the precompiled jar for 1.0.0? Do you have one for the 1.0.1 snapshot? Sorry to be a pain in the ass.
And early Merry Christmas!!
hey I was thinking of using Pi for a lighting system and I am wondering Is there any way of getting the code to make it go on/off at what ever time I want and also do multiple times at once...basically I have 3 lights to plug in and want 2 to run on the same schedule and the other to come on at another times
wget https://github.com/downloads/savagehomeautomation/raspi-sspc/raspi-sspc-1.0.0.jar
I can't download SSPC~ .jar
Anyone help?
@June,
It looks like GitHub no longer supports the binary file downloads feature so that is why the link is dead.
You can download (git clone) the sources and build the latest version of this project from here:
https://github.com/savagehomeautomation/raspi-sspc/tree/1.0.1-SNAPSHOT
Thanks, Robert
@Robert
Thank you for your response.
But I do not know how to compile the source.
So...
Can u compile for me ?
Thank you for your compassion.
My Email is smartdaum@gmail.com
Thanks, June
Hi, is it possible to modify the power on/off times for 30 minutes before sunrise and 30 minutes after sunset?
@Ron,
Sure, just modify these lines of code to add or subtract the number of minutes you wish to trigger at:
Date today_sunrise = ss.getSunrise(latitude, longitude);
Date today_sunset = ss.getSunset(latitude, longitude);
Date tomorrow_sunrise = ss.getSunrise(latitude, longitude, tomorrow.getTime());
Date tomorrow_sunset = ss.getSunset(latitude, longitude, tomorrow.getTime());
https://github.com/savagehomeautomation/raspi-sspc/blob/master/src/main/java/com/savagehomeautomation/raspi/sspc/SunriseSunsetPowerController.java#L375
Thanks ,Robert
elemarg e3d3fd1842 https://webcaffe.ws/geschsabpunchthal