-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio_rain_service.py
64 lines (52 loc) · 2.29 KB
/
gpio_rain_service.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
# You may need to go into the driver for the rest of your hardware and comment out the 'Rain =' line.
# If you are using the GW1000 driver and there is no rain sensor communicating with the GW1000, no change is needed.
import syslog
import weewx
from weewx.engine import StdService
from gpiozero import Button
import datetime
import time
DRIVER_NAME = "GPIORainGauge"
DRIVER_VERSION = "1.0"
class GpioRainGauge(StdService):
def __init__(self, engine, config_dict):
super(GpioRainGauge, self).__init__(engine, config_dict)
d = config_dict.get('GPIORainGauge', {})
self.gauge = RainGauge(**d)
self.bind(weewx.NEW_LOOP_PACKET, self.load_data)
def load_data(self, event):
try:
self.get_rain(event)
except Exception as e:
syslog.syslog(syslog.LOG_ERR, "GPIORainGauge: cannot read value: %s" % e)
# Get Rainfall data
def get_rain(self, event):
rainfall = round(self.gauge.get_rainfall(),3)
if rainfall != 0.000:
syslog.syslog(syslog.LOG_DEBUG, "GPIORainGauge: found rain value of %s mm" % rainfall)
event.packet['rain'] = float(rainfall)
# Cleanup the gpio pin if the Engine is shut down, or GPIOZero will throw error GPIOPinInUse when engine restarts
def ShutDown(self):
self.gauge.release_pin()
syslog.syslog(syslog.LOG_DEBUG, "GPIORainGauge: ShutDown() called")
class RainGauge(object):
""" Object that represents a Wired Rain Gauge. """
def __init__(self, **d):
""" Initialize Object. """
# Read from config the bucket size
self.bucket_size = float(d.get('bucket_size'))
self.rain_count = 0
# Read from config which pin to use on the RPI GPIO
self.rain_sensor = Button(d.get('pin_number'))
self.rain_sensor.when_pressed = self.bucket_tipped
def bucket_tipped(self):
self.rain_count = self.rain_count + 1
def get_rainfall(self):
rainfall = (self.rain_count * self.bucket_size)
self.reset_rainfall()
return rainfall
def reset_rainfall(self):
self.rain_count = 0
def release_pin(self):
self.rain_sensor.close()
syslog.syslog(syslog.LOG_DEBUG, "Releasing pin in gpiozero")