Skip to content
This repository was archived by the owner on Apr 15, 2022. It is now read-only.

Commit a5e06d1

Browse files
committed
Update library to adafruit-circuitpython-dht
1 parent 0be653a commit a5e06d1

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

Diff for: dht22_exporter.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
from prometheus_client import Gauge, start_http_server
1111

12-
import Adafruit_DHT
12+
import board
13+
import adafruit_dht
1314

1415
# Create a metric to track time spent and requests made.
1516
dht22_temperature_celsius = Gauge(
@@ -19,11 +20,15 @@
1920
dht22_humidity = Gauge(
2021
'dht22_humidity', 'Humidity in percents provided by dht sensor')
2122

22-
SENSOR = Adafruit_DHT.DHT22
23-
24-
25-
def read_sensor(pin):
26-
humidity, temperature = Adafruit_DHT.read_retry(SENSOR, pin)
23+
def read_sensor(sensor):
24+
print(sensor)
25+
try:
26+
temperature = sensor.temperature
27+
humidity = sensor.humidity
28+
except Exception as error:
29+
print(f"Exception: {error.args[0]}")
30+
time.sleep(2.0)
31+
return
2732

2833
if humidity is None or temperature is None:
2934
return
@@ -51,14 +56,18 @@ def main():
5156

5257
args = parser.parse_args()
5358

59+
pin = getattr(board, "D" + str(args.gpio))
60+
sensor = adafruit_dht.DHT22(pin, use_pulseio=False)
61+
5462
if args.addr is not None:
5563
start_http_server(args.port, args.addr)
5664
else:
5765
start_http_server(args.port)
5866

5967
while True:
60-
read_sensor(pin=args.gpio)
68+
read_sensor(sensor)
6169
time.sleep(args.interval)
6270

6371

64-
main()
72+
if __name__ == "__main__":
73+
main()

Diff for: requirements.txt

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
prometheus_client
2-
Adafruit_DHT
3-
ConfigArgParse
1+
Adafruit-Blinka==6.20.1
2+
adafruit-circuitpython-dht==3.7.0
3+
Adafruit-PlatformDetect==3.19.4
4+
Adafruit-PureIO==1.1.9
5+
prometheus-client==0.13.1
6+
pyftdi==0.53.3
7+
pyserial==3.5
8+
pyusb==1.2.1
9+
RPi.GPIO==0.7.0

0 commit comments

Comments
 (0)