Google Calendar Raspberry Pi and Lights On Lights Off

Google Calendar Raspberry Pi and Lights On Lights Off


Summary

Combining these ingredients:
  • an Elro "Wireless Home Control" set
  • a Raspberry Pi mini-computer
  • some soldering
  • some programming and
  • Google Calendar

... Im able to have my lights switched on and off automatically based on a schedule I create in Google Calendar.

Introduction

For about �15 you can buy an Elro set with 3 switches and one remote control. With the remote control you can switch on and off lights or other stuff plugged into of the three switches. 



With the remote control, you can control up to four switches. For each switch there is an On button on the left side of the remote control, and an Off button on the right side of the remote control.

When you open the remote control unit, you can see the HX2262 is the central (and only) chip. This is a standard remote-control chip.
On the printed circuit board, each button has three contacts:
  • The center contact of all buttons is connected to 0V and thus Pin 9 of the HX2262
  • On-buttons: The left contacts are connected to Pin 12 of the chip. The right contacts are connected to Pin 6, 7, 8 and respectively.
  • Off-buttons: The right contacts of all  are connected to Pin 13 of the chip. The left contacts are connected to Pin 6, 7, 8 and respectively.
So, to turn Switch A on, connect the chips Pin 12 and 6 each to 0V (or Pin 9). To turn it off, connect Pin 6 and 13 to 0V (or Pin 9). 
To turn Switch B on, connect Pin 12 and 7 to 0V (or Pin 9). In schematic form:

OnOff
Switch A    12 to 0V, 6 to 0V13 to 0V, 6 to 0V
Switch B12 to 0V, 7 to 0V13 to 0V, 7 to 0V
Switch C12 to 0V, 8 to 0V13 to 0V, 8 to 0V
Switch D12 to 0V, 10 to 0V13 to 0V10 to 0V

Logic in the logic

With some reverse engineering, you see the logic of the Elro remote controller keyboard and the DIP-switch is as described in the picture below.



The pin-out of the HX2262 is like this:


So:

  • the DIP switch drives A0 - A4
  • switch A, B, C or D drive A5 - A8
  • On resp. Off drive A10 resp A11

Note: pressing a button connects two lines to Ground / 0V / Vss (and not to Vcc).

Remotely controller the remote controller

To remotely control the switches, I decided to re-use the existing remote controller: Instead of mechanically pressing the buttons, I used transistors (BC547) to make the desired contacts. 

The transistor were driven by the GPIO output lines of the Raspberry Pi (or "Raspi"), the 35 $ mini-computer. See http://www.raspberrypi.org/

The transistors were connected like this:

  • C: connected to the desired Pin of the HX2262 chip
  • B: via 1k resistor connected to the Raspi GPIO line
  • E: connected to ground (0V) of both the HX2262 and the Raspi

When you look at the BC547 from the front (so that you can read the text on it), the order of the pins are C-B-E.

Attention: to control one switch you need 2 GPIO lines (one for On, and one for Off), and two resistors and two transistors per GPIO line. So in total, per switch you need four transistors and four resistors.

Programming the Raspis GPIO

Programming the Raspis GPIO is very easy in Python. First import the GPIO module (see http://pypi.python.org/pypi/RPi.GPIO/). Then write some python code. Example for turning Switch A on (GPIO0 used):


import RPi.GPIO as GPIO
GPIO.setup(11, GPIO.OUT)
print("Turn it on: press button ...")
GPIO.output(11, True)
import time
time.sleep(1)
print("... and release button.")
GPIO.output(11, False)


Note

  • you have to simulate pressing a button, so set the GPIO line high, wait a second, and then make it low again.
  • you have to run this as root ("sudo python turnOn.py"); apparently you need root rights to use the GPIO Python module


Example for turning Switch A off (GPIO1 used):


import RPi.GPIO as GPIO
GPIO.setup(12, GPIO.OUT)
print("Turn it off: press Off button ...")
GPIO.output(12, True)
import time
time.sleep(1)
print("... and release button.")
GPIO.output(12, False)

Now you can call these two scripts to turn On and Off your light.

The User Interface: Google Calendar

To set up a schedule for your lights, you need a nice user interface that can be controlled from computers, phones, tablets, etc. A very clever industrial designer advised me to use ... Google Calendar.  In Google Calendar you can create a separate agenda for your lights, which info can be easily read via a (secret) URL. See http://support.google.com/calendar/bin/answer.py?hl=en&answer=37648 how to get that secret URL.

I chose the URL for the iCalendar format, because I found it easier to parse. I wrote a python script to get the calendar, and see if the light should be On. If so, turn it on. If not, turn it off.

Conclusion

The result is that I set a schedule in Google Calendar, and have the lights On/Off according to that schedule.

In logical order / in Pictures:

Set Lights On/Off Schedule in Google Calendar:





The Raspi will get the Google Calendar info, and will steer the Elro remote controller via the add-on board:



The result is the light being On or Off:



PS:

Heres the python script to get and interpret the Google Calendar info:



#!/usr/bin/python -OO
# Copyright 2012 Sander Jonkers
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# This program will read from a Google Calendar to determine if the light should be on.
# Any Agenda item (one, or two, or more) during the current time will mean "lights on".
# No Agenda item means "lights off"


from time import gmtime, strftime



# Useful: http://strftime.net/

# The Google Calendar calendar ics items are in GMT / Zulu time. So first find that time for this system, in the same string
# format as the Google calendar ics 

currenttimeZulu = strftime("%Y%m%dT%H%M%SZ", gmtime())
print " Current time in GMT / Zulu time is:",currenttimeZulu
# Gives: 20120715T114225Z


# the secret (yes!) URL for the agenda. See http://support.google.com/calendar/bin/answer.py?hl=en&answer=37648 how to find your URL
iCalAgendaUrl = https://www.google.com/calendar/ical/g3qgmhi6blablarucr6bces%40group.calendar.google.com/private-b6ff5b3c6579d8blabla7ec60/basic.ics



The interesting lines in the basic.ics

BEGIN:VEVENT
DTSTART:20120715T130000Z
DTEND:20120715T140000Z
SUMMARY:middagverlichting
END:VEVENT



import urllib2

req = urllib2.Request(iCalAgendaUrl)
response = urllib2.urlopen(req)
print "FYI: Agenda retrieved"
# the_page = response.read()
# print the_page

LightsOn = False # Default is lights off

print "Start parsing the Agenda"
for thisline in response.readlines():
# print thisline
regel = thisline.rstrip()
if regel.startswith(BEGIN:VEVENT):
# print "BEGIN:VEVENT gevonden!!!", regel
starttime = None
endtime = None
summary = None
if regel.startswith(DTSTART):
# print "DTSTART gevonden!!!", regel
starttime = regel.split(:)[1]
# print starttime
if regel.startswith(DTEND):
# print "DTEND gevonden!!!", regel
endtime = regel.split(:)[1]
# print endtime
if regel.startswith(SUMMARY):
# print "DTSTART gevonden!!!", regel
summary = regel.split(:)[1]
# print starttime

if regel.startswith(END:VEVENT):
# print "END:VEVENT gevonden!!!", regel
if (starttime < currenttimeZulu) and (currenttimeZulu <= endtime):
print starttime, endtime, summary
print "This event says the light should be on!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
LightsOn = True
else:
dummy = 1
# print "*This* event says the light should be off"
# Attention: do NOT set LightsOn to False (=Off)!!!!


if LightsOn:
print "Lights should be on"
else:
print "Lights should be off"

def TurnLightsOn():
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) # Needed in GPIO version 0.3.1

GPIO.setup(11, GPIO.OUT)


print("Turn it on: press button ...")
GPIO.output(11, True)

import time
time.sleep(1)

print("... and release button.")
GPIO.output(11, False)


def TurnLightsOff():
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)

print("Turn it off: press Off button ...")
GPIO.output(12, True)

import time
time.sleep(1)

print("... and release button.")
GPIO.output(12, False)


import os, sys

if os.popen(whoami).readline().strip() == root :
print "OK, youre root"
root = True
else:
print " Attention: please run as root with sudo. This scripts GPIO which need root-rights."
print "Now exiting ..."
sys.exit(1)
root = False


if LightsOn:
print "Turning Lights On ..."
TurnLightsOn()
else:
print "Turning Lights Off ..."
TurnLightsOff()

print "Finished"

HTH




download file now