-
Notifications
You must be signed in to change notification settings - Fork 0
/
rainsensor.py
executable file
·105 lines (93 loc) · 2.88 KB
/
rainsensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
"""
Raspberry Pi rain sensors
start name server with:
python -m Pyro4.naming -n 10.2.5.107
"""
import time
import datetime
import argparse
import logging
from collections import defaultdict
import Pyro4
import RPi.GPIO as g
import pymysql
# pylint: disable = invalid-name
SLEEP_TIME = 5
logging.basicConfig(
level='INFO', format='%(asctime)s : %(message)s')
logger = logging.getLogger(__name__)
class RainSensor(object):
"""
Rain sensor class
"""
def __init__(self):
"""Sets up the output dict and GPIO pin mode"""
self._running = True
self.rs = defaultdict(list)
g.setmode(g.BCM)
self.gpio_nums = [2, 3, 4, 5, 6]
for i in self.gpio_nums:
if i == 2 or i == 3:
g.setup(i, g.IN)
else:
g.setup(i, g.IN, pull_up_down=g.PUD_UP)
def running(self):
"""Returns false if the daemon should be terminated"""
return self._running
def get_rain(self):
"""Returns the rain sensor values in a dict"""
for i in range(0, len(self.gpio_nums)):
self.rs[(i+1)] = g.input(self.gpio_nums[i])
print(self.rs)
return self.rs
def update_rain_info(sensor, time_value):
"""
Log the rain sensor information
"""
host = '10.2.5.32'
db = 'ngts_ops'
user = 'ops'
logger.debug('Fetching rain sensor measurements')
rs = sensor.get_rain()
bucket = (int(time_value)/60)*60
tsample = datetime.datetime.utcnow().isoformat().replace('T', ' ')[:-7] # remove microseconds
qry = """
REPLACE INTO rpi_rg11_rain_sensors
(tsample, bucket, rs01, rs02, rs03, rs04, rs05)
VALUES
(%s, %s, %s, %s, %s, %s, %s)
"""
# forcing final three sensors to be 0 for now
params = (tsample, bucket, rs[1], rs[2], rs[3], rs[4], rs[5])
with pymysql.connect(host=host, db=db, user=user) as cursor:
logger.debug('Updating database: %s : %s', qry, params)
cursor.execute(qry, params)
def rain_sensor_watcher(sensor):
"""
Set up the rain sensor watcher
"""
# Connect to central hub
logger.debug('Connecting to central hub')
hub = Pyro4.Proxy('PYRONAME:central.hub')
logger.debug('Entering main loop')
while True:
# Inform the central hub that it's working
hub.report_in('rain_sensor')
# Upload rain info to the database
update_rain_info(sensor, time.time())
logger.debug('Sleeping for %s seconds', SLEEP_TIME)
time.sleep(SLEEP_TIME)
def argParse():
"""
Parse the command line arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true')
return parser.parse_args()
if __name__ == '__main__':
args = argParse()
if args.verbose:
logger.setLevel('DEBUG')
sensor = RainSensor()
rain_sensor_watcher(sensor)