1
2"""
3TMRh20 - 2015
4A simple example of controlling a RF24Ethernet based sensor node via a python script
5Turn a light on in the evening and off in the morning according to a schedule
6"""
7
8import urllib2
9from datetime import datetime
10import time
11import syslog
12
13
14
15scheduleON = 21
16scheduleOFF = 6
17sensorIP = "10.10.2.4"
18
19
20
21requestON = "http://" + sensorIP + ":1000/ON"
22requestOFF = "http://" + sensorIP + ":1000/OFF"
23lightState = 2
24
25while 1:
26
27
28 currentHour = datetime.now().hour
29
30
31 if (currentHour >= scheduleON or currentHour < scheduleOFF) and lightState != 1:
32
33 result = 0
34
35 try:
36 response = urllib2.urlopen(requestOFF, None, 15)
37 result = response.getcode()
38 except urllib2.HTTPError, e:
39 syslog.syslog("HTTPError = " + str(e.code))
40 except urllib2.URLError, e:
41 syslog.syslog("URLError = " + str(e.reason))
42 except Exception:
43 import traceback
44
45 syslog.syslog("generic exception: " + traceback.format_exc())
46
47 if result == 200:
48 syslog.syslog("Light off at %s\n" % datetime.now())
49
50 lightState = 1
51
52
53 elif currentHour >= scheduleOFF and lightState != 0:
54 result = 0
55
56 try:
57 response = urllib2.urlopen(requestON, None, 15)
58 result = response.getcode()
59 except urllib2.HTTPError, e:
60 syslog.syslog("HTTPError = " + str(e.code))
61 except urllib2.URLError, e:
62 syslog.syslog("URLError = " + str(e.reason))
63 except Exception:
64 import traceback
65
66 syslog.syslog("generic exception: " + traceback.format_exc())
67
68 if result == 200:
69 syslog.syslog("Light on at %s\n" % datetime.now())
70
71 lightState = 0
72
73
74 time.sleep(60)