RF24Gateway - TCP/IP over RF24Network v2.1.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
8from datetime import datetime
9import syslog
10import time
11from urllib.error import HTTPError, URLError
12from urllib.request import urlopen
13
14######### Configuration #########
15
16scheduleON = 21 # The hour (24hr clock) to turn the system on (21:00)
17scheduleOFF = 6 # The hour (24hr clock) to turn the system off (06:00)
18sensorIP = "10.10.2.4" # The IP of a sensor node running one of the server examples
19
20#################################
21
22requestON = "http://" + sensorIP + ":1000/ON"
23requestOFF = "http://" + sensorIP + ":1000/OFF"
24lightState = 2
25
26while 1:
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 result = 0
33 # Connect to our sensor at 10.10.3.44:1000/ and request OFF
34 try:
35 response = urlopen(requestOFF, None, 15) # 15 second time-out
36 result = response.getcode()
37 except HTTPError as e:
38 syslog.syslog("HTTPError = " + str(e.code))
39 except URLError as e:
40 syslog.syslog("URLError = " + str(e.reason))
41 except Exception:
42 import traceback
43
44 syslog.syslog("generic exception: " + traceback.format_exc())
45 # Log errors and successful results to /var/log/syslog
46 if result == 200:
47 syslog.syslog("Light off at %s\n" % datetime.now())
48 # Only change the light state if successful
49 lightState = 1
50
51 # Else, check to see if the system should be on
52 elif currentHour >= scheduleOFF and lightState != 0:
53 result = 0
54
55 try:
56 response = urlopen(requestON, None, 15) # 15 second time-out
57 result = response.getcode()
58 except HTTPError as e:
59 syslog.syslog("HTTPError = " + str(e.code))
60 except URLError as e:
61 syslog.syslog("URLError = " + str(e.reason))
62 except Exception:
63 import traceback
64
65 syslog.syslog("generic exception: " + traceback.format_exc())
66 # Log errors and successful results to /var/log/syslog
67 if result == 200:
68 syslog.syslog("Light on at %s\n" % datetime.now())
69 # Only change the light state if successful
70 lightState = 0
71
72 # Wait 1 minute between retries and status checks
73 time.sleep(60)