RF24Gateway - TCP/IP over RF24Network v2.0.0
TMRh20 - Pushing the practical limits of RF24 modules
Loading...
Searching...
No Matches
pyClient.py

Once RF24Gateway and RF24Ethernet are configured, standard tools can be used to interact with the sensor nodes.
Example of scheduled LED/Lighting control using a Python script.

1#!/usr/bin/env python
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######### Configuration #########
14
15scheduleON = 21 # The hour (24hr clock) to turn the system on (21:00)
16scheduleOFF = 6 # The hour (24hr clock) to turn the system off (06:00)
17sensorIP = "10.10.2.4" # The IP of a sensor node running one of the server examples
18
19#################################
20
21requestON = "http://" + sensorIP + ":1000/ON"
22requestOFF = "http://" + sensorIP + ":1000/OFF"
23lightState = 2
24
25while 1:
26
27 # Get the current hour
28 currentHour = datetime.now().hour
29
30 # Check to see if the system should be off
31 if (currentHour >= scheduleON or currentHour < scheduleOFF) and lightState != 1:
32
33 result = 0
34 # Connect to our sensor at 10.10.3.44:1000/ and request OFF
35 try:
36 response = urllib2.urlopen(requestOFF, None, 15) # 15 second time-out
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 # Log errors and successful results to /var/log/syslog
47 if result == 200:
48 syslog.syslog("Light off at %s\n" % datetime.now())
49 # Only change the light state if successful
50 lightState = 1
51
52 # Else, check to see if the system should be on
53 elif currentHour >= scheduleOFF and lightState != 0:
54 result = 0
55
56 try:
57 response = urllib2.urlopen(requestON, None, 15) # 15 second time-out
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 # Log errors and successful results to /var/log/syslog
68 if result == 200:
69 syslog.syslog("Light on at %s\n" % datetime.now())
70 # Only change the light state if successful
71 lightState = 0
72
73 # Wait 1 minute between retries and status checks
74 time.sleep(60)