** Update**
I've now implemented this using an Energie Pi Controlled mains Socket - see my blog at Home Automation using ENERGENIE - ENER002-2PI
For details on the Energie socket see ENER002-2PI - ENERGENIE - RF CONTROLLED MAINS SOCKETS, FOR | CPC
One of the things I wanted to do with my Pi from the outset was to be able to use it as a timer, to turn a light on and off at night when I was out, as a simple form of home security. Being something of a newbie when it comes to electronics, my biggest problem was how to wire it up to be able to turn the power on and off.
The Pi relays are rated at a maximum of 20v and 5amps - therefore I cannot connect the Pi directly to the 'mains powered' light !!!
What I am planning on doing is investigating using a to a commercial relay which can cope with the current. When I've done that I shall update this blog again with details of the how the Pi can safely control a mains powered device.
However, the rest of the blog is still valid as it explains how the relay on the PiFace is operated and will be used for the Timer application, when completed.
I've written a java program to control the light switching on and off which does the following;
- Calculate a random wait time between two values that I have supplied (2 and 20 minutes at the moment)
- Wait for that period of time to elapse
- At the end of the wait time, toggle the relay - switch it on if it was already off, and vice-versa.
- repeat the above ad infinitum
The result is that the light will keep turning on and off in a random pattern making it look as if someone is using the room even when I'm not there !
Java Code
I've already written a document about using Java on the Pi - see http://www.element14.com/community/docs/DOC-54112/l/java-on-the-raspberry-pi. The code that I used for this project is attached to this document (e.g. timeswitch.zip)
Running Automatically
My intention is really to have this as a 'black box' function so that the TimeSwitch application will run as soon as the Pi is switched on. This is accomplished quite easily - all I need to do is write a script to run the java application, and then add it to the initialisation script.
The script to run the java application is as follows ;
cd /opt/pi4j/examples java -classpath .:classes:/opt/pi4j/lib/'*' com.phe.datetime.TimeSwitch
To run at start up I have edited /tec/rc.local and added the following line
/opt/pi4j/examples/timeswitch.sh > /opt/pi4j/examples/timeswitch.log
This will execute the java app at start up and also pipe any log messages to timeswitch.log.
Top Comments