Skip to content

Commit e040590

Browse files
authored
New Sensor Module: Flowsensor (#377)
* Add generic Flowsensor Module
1 parent 38ded49 commit e040590

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Hardware support is provided by specific GPIO, Sensor and Stream modules. It's e
4646
- PMS5003 particulate sensor (`pms5003`)
4747
- SHT40/SHT41/SHT45 temperature and humidity sensors (`sht4x`)
4848
- YF-S201 flow rate sensor (`yfs201`)
49+
- FLOWSENSOR generic flow rate sensor like YF-S201 or YF-DN50 (`flowsensor`)
4950

5051
### Streams
5152

mqtt_io/modules/sensor/flowsensor.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""
2+
3+
Flowsensor: Generic Flow Rate Sensor
4+
5+
Example configuration:
6+
7+
sensor_modules:
8+
- name: yfs201
9+
module: flowsensor
10+
11+
sensor_inputs:
12+
- name: flow_rate1
13+
module: flowsensor
14+
pin: 0
15+
digits: 0
16+
interval: 10
17+
factor: 0.2
18+
19+
Factor can be calculated from Pulse characteristcs (dicumentation):
20+
21+
From YF-S201 manual:
22+
Pulse Characteristic: F = 7 * Q (L/MIN).
23+
Pulse frequency (Hz) / 7.0 = flow rate in L/min
24+
==> Factor = 7.0
25+
26+
From YF-DN50 manual:
27+
Pulse Characteristic: F = 0.2 * Q (L/MIN).
28+
Pulse frequency (Hz) / 0.2 = flow rate in L/min
29+
==> Factor = 0.2
30+
31+
If you use "factor = 1", the sensor module returns the frequency in Hertz (Hz).
32+
33+
"""
34+
35+
from typing import Dict
36+
from ...types import CerberusSchemaType, ConfigType, SensorValueType
37+
from . import GenericSensor
38+
39+
REQUIREMENTS = ("gpiozero",)
40+
41+
42+
class FLOWSENSOR:
43+
"""
44+
Flowsensor Flow Rate Sensor class
45+
Multiple instances support multiple sensors on different pins
46+
"""
47+
48+
def __init__(self, gpiozero, name: str, pin: int) -> None: # type: ignore[no-untyped-def]
49+
self.name = name
50+
self.pin = gpiozero.DigitalInputDevice(pin)
51+
self.pin.when_activated = self.count_pulse
52+
self.count = 0
53+
54+
def count_pulse(self) -> None:
55+
"""Increment pulse count."""
56+
self.count += 1
57+
58+
def reset_count(self) -> None:
59+
"""Reset pulse count."""
60+
self.count = 0
61+
62+
def flow_rate(self, sample_window: int, factor: float) -> float:
63+
"""Return flow rate in liters per minute.
64+
65+
From YF-S201 manual:
66+
Pluse Characteristic:F=7Q(L/MIN).
67+
2L/MIN=16HZ 4L/MIN=32.5HZ 6L/MIN=49.3HZ 8L/MIN=65.5HZ 10L/MIN=82HZ
68+
69+
Pulse frequency (Hz) / 7.0 = flow rate in L/min
70+
71+
sample_window is in seconds, so hz is pulse_count / sample_window
72+
"""
73+
hertz = self.count / sample_window
74+
return hertz / factor
75+
76+
def get_value(self, interval: int, factor: float) -> float:
77+
"""Return flow rate in L/min over interval seconds and reset count."""
78+
flow_rate = self.flow_rate(interval,factor)
79+
self.reset_count()
80+
return flow_rate
81+
82+
83+
class Sensor(GenericSensor):
84+
"""
85+
Flowsensor: Flow Rate Sensor
86+
"""
87+
88+
SENSOR_SCHEMA: CerberusSchemaType = {
89+
"pin": {
90+
"type": 'integer',
91+
"required": True,
92+
"empty": False,
93+
},
94+
"interval": {
95+
"type": 'integer',
96+
"required": True,
97+
"empty": False,
98+
},
99+
"factor": {
100+
"type": 'float',
101+
"required": True,
102+
"empty": False,
103+
}
104+
}
105+
106+
def setup_module(self) -> None:
107+
# pylint: disable=import-outside-toplevel,import-error
108+
import gpiozero # type: ignore
109+
110+
self.gpiozero = gpiozero
111+
self.sensors: Dict[str, FLOWSENSOR] = {}
112+
113+
def setup_sensor(self, sens_conf: ConfigType) -> None:
114+
sensor = FLOWSENSOR(
115+
gpiozero=self.gpiozero, name=sens_conf["name"], pin=sens_conf["pin"]
116+
)
117+
self.sensors[sensor.name] = sensor
118+
119+
def get_value(self, sens_conf: ConfigType) -> SensorValueType:
120+
return self.sensors[sens_conf["name"]].get_value(sens_conf["interval"],sens_conf["factor"])

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy