Contributions

Contributions are appreciated and used to host this site. Thank You for your support.

Feature Products
  • Raspberry Pi with Java: Programming the Internet of Things (IoT)
    Raspberry Pi with Java: Programming the Internet of Things (IoT)
  • Raspberry Pi 2 Model B Project Board - 1GB RAM - 900 MHz Quad-Core CPU
    Raspberry Pi 2 Model B Project Board - 1GB RAM - 900 MHz Quad-Core CPU
  • Premium Clear Case for Raspberry Pi 2 Model B Quad Core and Raspberry Pi Model B+ (B PLUS)
    Premium Clear Case for Raspberry Pi 2 Model B Quad Core and Raspberry Pi Model B+ (B PLUS)
  • Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter with EZmax Setup Wizard
    Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter with EZmax Setup Wizard
  • Getting Started with Raspberry Pi
    Getting Started with Raspberry Pi
  • Raspberry Pi User Guide
    Raspberry Pi User Guide
  • PiBrella LED Add On Board
    PiBrella LED Add On Board
  • Diversitech® WS-1 - Wet Switch Flood Detector
    Diversitech® WS-1 - Wet Switch Flood Detector
  • Onsite Pro FS1NPTW Whole Home Wireless FloodStop with 1 Inch Valve
    Onsite Pro FS1NPTW Whole Home Wireless FloodStop with 1 Inch Valve
  • Floodstop Washing Machine Valve Shutoff Kit
    Floodstop Washing Machine Valve Shutoff Kit
  • Onsite Pro FS3/4H Washing Machine FloodStop with Straight Valves
    Onsite Pro FS3/4H Washing Machine FloodStop with Straight Valves
  • Floodstop Individual Water Appliance Additional Water Sensor XS-01
    Floodstop Individual Water Appliance Additional Water Sensor XS-01
  • Aqua Managers - FS 1 1/4-NPT - Floodstop for Water Heaters Water Leak Detection System - White - 1.25 in. pipe
    Aqua Managers - FS 1 1/4-NPT - Floodstop for Water Heaters Water Leak Detection System - White - 1.25 in. pipe
  • Furman MP-20 Power Relay Accessory, 20 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
    Furman MP-20 Power Relay Accessory, 20 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
  • Furman MP-15 Power Relay Accessory, 15 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
    Furman MP-15 Power Relay Accessory, 15 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
  • Metra 70-2002 Radio Wiring Harness for Saturn 00-05
    Metra 70-2002 Radio Wiring Harness for Saturn 00-05
  • Metra Reverse Wiring Harness 71-2002 for Select 2000-2005 Saturn Vehicles OEM Radio
    Metra Reverse Wiring Harness 71-2002 for Select 2000-2005 Saturn Vehicles OEM Radio

Entries in power (2)

Wednesday
Dec052012

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.  
Of course you may find other useful applications for this project beyond holiday lighting.  This article will demonstrate how simple it can be to use a Raspberry Pi to control 110/240 VAC circuits.   Additionally the software (open source) will be provided to control the circuit 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

(click to enlarge)

 

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:

>> Electronically Monitor Christmas Tree Water Level

 
 


More Raspberry Pi  Projects and Articles

http://www.pislices.com

This page lists the articles posted on this blog about the Raspberry Pi