Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
script.py
Go to the documentation of this file.
1 import webiopi
2 import datetime
3 
4 GPIO = webiopi.GPIO
5 
6 LIGHT = 17 # GPIO pin using BCM numbering
7 
8 HOUR_ON = 8 # Turn Light ON at 08:00
9 HOUR_OFF = 18 # Turn Light OFF at 18:00
10 
11 # setup function is automatically called at WebIOPi startup
12 def setup():
13  # set the GPIO used by the light to output
14  GPIO.setFunction(LIGHT, GPIO.OUT)
15 
16  # retrieve current datetime
17  now = datetime.datetime.now()
18 
19  # test if we are between ON time and tun the light ON
20  if ((now.hour >= HOUR_ON) and (now.hour < HOUR_OFF)):
21  GPIO.digitalWrite(LIGHT, GPIO.HIGH)
22 
23 # loop function is repeatedly called by WebIOPi
24 def loop():
25  # retrieve current datetime
26  now = datetime.datetime.now()
27 
28  # toggle light ON all days at the correct time
29  if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
30  if (GPIO.digitalRead(LIGHT) == GPIO.LOW):
31  GPIO.digitalWrite(LIGHT, GPIO.HIGH)
32 
33  # toggle light OFF
34  if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
35  if (GPIO.digitalRead(LIGHT) == GPIO.HIGH):
36  GPIO.digitalWrite(LIGHT, GPIO.LOW)
37 
38  # gives CPU some time before looping again
39  webiopi.sleep(1)
40 
41 # destroy function is called at WebIOPi shutdown
42 def destroy():
43  GPIO.digitalWrite(LIGHT, GPIO.LOW)
44 
45 # a simple macro without argument
46 # returns ON;OFF hours
47 @webiopi.macro
49  return "%d;%d" % (HOUR_ON, HOUR_OFF)
50 
51 # a macro with two arguments
52 # set ON, OFF hours
53 # returns ON;OFF hours
54 @webiopi.macro
55 def setLightHours(on, off):
56  global HOUR_ON, HOUR_OFF
57  HOUR_ON = int(on)
58  HOUR_OFF = int(off)
59  return getLightHours()
60 
def loop
Definition: script.py:28
def getLightHours
Definition: script.py:48
def destroy
Definition: script.py:28
def setLightHours
Definition: script.py:55
def setup
Definition: script.py:15