import syslog
import weewx
from weewx.wxengine import StdService
from gpiozero import Button
import datetime
import time
DRIVER_NAME = "RW111GPIOTest"
DRIVER_VERSION = "0.1"
class Rw111GpioService(StdService):
def __init__(self, engine, config_dict):
super(Rw111GpioService, self).__init__(engine, config_dict)
d = config_dict.get('Rw111GpioService', {})
# Read from config which pin to use on the RPI GPIO
# Defaults to 6
# Use the loop packet event as that allows data to still get into the WeeWX database
# as well as supporting a OLED module on the RPI
self.rw111 = Rw111(**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, "rw111gpio: cannot read value: %s" % e)
# Get Rainfall data
def get_rain(self, event):
rainfall = self.rw111.get_rainfall()
syslog.syslog(syslog.LOG_DEBUG, "rw111gpio: found rain value of %s cm" % rainfall)
event.packet['rain'] = float(rainfall)
class Rw111(object):
""" Object that represents a RainWise Wired Rain Gauge. """
def __init__(self, **d):
""" Initialize Object. """
# Read from config the bucket size
self.bucket_size = d.get("bucket_size", 0.1) # in mm
self.rain_count = 0
# Read from config which pin to use on the RPI GPIO
self.rain_sensor = Button(d.get("rw111_pin", 6))
self.rain_sensor.when_pressed = self.bucket_tipped()
syslog.syslog(syslog.LOG_DEBUG, "rw111gpio: Button Initialized")
def bucket_tipped(self):
self.rain_count = self.rain_count + 1
syslog.syslog(syslog.LOG_DEBUG, "rw111gpio: Button Pressed, count %s" % self.rain_count)
def get_rainfall(self):
""" Returns rainfall in cm. """
rainfall = (self.rain_count * self.bucket_size) / 10.0
self.reset_rainfall()
syslog.syslog(syslog.LOG_DEBUG, "rw111gpio: Rainfall got, count: %s" % self.rain_count)
return rainfall
def reset_rainfall(self):
self.rain_count = 0
**************************
It's running, but always returning 0.0.
I've confirmed the button works and is wired correctly. But within this service, it doesn't seem to be registering.