<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>PiFace Home Timeswitch - turning a light on and off</title><link>https://community.element14.com/products/raspberry-pi/w/documents/11085/piface-home-timeswitch---turning-a-light-on-and-off</link><description /><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>PiFace Home Timeswitch - turning a light on and off</title><link>https://community.element14.com/products/raspberry-pi/w/documents/11085/piface-home-timeswitch---turning-a-light-on-and-off</link><pubDate>Fri, 08 Oct 2021 05:29:49 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:6c10412d-e172-432b-a759-3049499c2316</guid><dc:creator>PEarle</dc:creator><comments>https://community.element14.com/products/raspberry-pi/w/documents/11085/piface-home-timeswitch---turning-a-light-on-and-off#comments</comments><description>Current Revision posted to Documents by PEarle on 10/8/2021 5:29:49 AM&lt;br /&gt;
&lt;p style="margin:0;"&gt;&lt;strong style="font-size:14pt;"&gt;** Update**&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;I&amp;#39;ve now implemented this using an Energie Pi Controlled mains Socket - see my blog at &lt;a class="jive-link-blog-small" href="/products/raspberry-pi/b/blog/posts/home-automation-using-energenie---ener002-2pi"&gt;Home Automation using ENERGENIE - ENER002-2PI&lt;/a&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;For details on the Energie socket see &lt;a class="jive-link-external-small" href="http://cpc.farnell.com/energenie/ener002-2pi/rf-controlled-mains-sockets-for/dp/SC13490" target="_blank" title="http://cpc.farnell.com/energenie/ener002-2pi/rf-controlled-mains-sockets-for/dp/SC13490"&gt;ENER002-2PI - ENERGENIE - RF CONTROLLED MAINS SOCKETS, FOR | CPC&lt;/a&gt;&lt;/p&gt;&lt;hr /&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;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.&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;The Pi relays are rated at a maximum of 20v and 5amps - therefore I cannot connect the Pi directly to the &amp;#39;mains powered&amp;#39; light !!!&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;What I am planning on doing is investigating using a to a commercial relay which can cope with the current. When I&amp;#39;ve done that I shall update this blog again with details of the how the Pi can safely control a mains powered device.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;&lt;span style="font-size:12pt;"&gt;&lt;strong&gt;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.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;I&amp;#39;ve written a java program to control the light switching on and off which does the following;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Calculate a random wait time between two values that I have supplied (2 and 20 minutes at the moment)&lt;/li&gt;&lt;li&gt;Wait for that period of time to elapse&lt;/li&gt;&lt;li&gt;At the end of the wait time, toggle the relay - switch it on if it was already off, and vice-versa.&lt;/li&gt;&lt;li&gt;repeat the above ad infinitum&lt;/li&gt;&lt;/ul&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;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&amp;#39;m not there !&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;&lt;strong&gt;Java Code&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin:0;"&gt;I&amp;#39;ve already written a document about using Java on the Pi - see &lt;a class="jive-link-external-small" href="/products/raspberry-pi/w/documents/11074/java-on-the-raspberry-pi" target="_blank"&gt;http://www.element14.com/community/docs/DOC-54112/l/java-on-the-raspberry-pi&lt;/a&gt;. The code that I used for this project is attached to this document (e.g. timeswitch.zip)&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;&lt;strong&gt;Running Automatically&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin:0;"&gt;My intention is really to have this as a &amp;#39;black box&amp;#39; 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.&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;The script to run the java application is as follows ;&lt;/p&gt;&lt;pre&gt;cd /opt/pi4j/examples
java -classpath .:classes:/opt/pi4j/lib/&amp;#39;*&amp;#39; com.phe.datetime.TimeSwitch
&lt;/pre&gt;&lt;p style="margin:0;"&gt;To run at start up I have edited&amp;nbsp; /tec/rc.local and added the following line&lt;/p&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;pre&gt;/opt/pi4j/examples/timeswitch.sh &amp;gt; /opt/pi4j/examples/timeswitch.log&lt;/pre&gt;&lt;p style="margin:0;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin:0;"&gt;This will execute the java app at start up and also pipe any log messages to timeswitch.log.&lt;/p&gt;
&lt;div class="migration-injected-attachments"&gt;&lt;div style="font-weight:bold;margin:15px 0 5px 0;"&gt;Attachments:&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;table style="border:0;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td colspan="2"&gt;&lt;span class="_se_attachment" id="attid_https://www.element14.com/community/api/core/v3/attachments/113300"&gt;&lt;a href="https://community.element14.com/cfs-file/__key/communityserver-wikis-components-files/00-00-00-01-69/timeswitch.zip"&gt;community.element14.com/.../timeswitch.zip&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;
</description></item></channel></rss>