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

Commit

Permalink
Update library to adafruit-circuitpython-dht
Browse files Browse the repository at this point in the history
  • Loading branch information
nanorobocop committed Feb 11, 2022
1 parent 0be653a commit a5e06d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
25 changes: 17 additions & 8 deletions dht22_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

from prometheus_client import Gauge, start_http_server

import Adafruit_DHT
import board
import adafruit_dht

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

SENSOR = Adafruit_DHT.DHT22


def read_sensor(pin):
humidity, temperature = Adafruit_DHT.read_retry(SENSOR, pin)
def read_sensor(sensor):
print(sensor)
try:
temperature = sensor.temperature
humidity = sensor.humidity
except Exception as error:
print(f"Exception: {error.args[0]}")
time.sleep(2.0)
return

if humidity is None or temperature is None:
return
Expand Down Expand Up @@ -51,14 +56,18 @@ def main():

args = parser.parse_args()

pin = getattr(board, "D" + str(args.gpio))
sensor = adafruit_dht.DHT22(pin, use_pulseio=False)

if args.addr is not None:
start_http_server(args.port, args.addr)
else:
start_http_server(args.port)

while True:
read_sensor(pin=args.gpio)
read_sensor(sensor)
time.sleep(args.interval)


main()
if __name__ == "__main__":
main()
12 changes: 9 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
prometheus_client
Adafruit_DHT
ConfigArgParse
Adafruit-Blinka==6.20.1
adafruit-circuitpython-dht==3.7.0
Adafruit-PlatformDetect==3.19.4
Adafruit-PureIO==1.1.9
prometheus-client==0.13.1
pyftdi==0.53.3
pyserial==3.5
pyusb==1.2.1
RPi.GPIO==0.7.0

0 comments on commit a5e06d1

Please sign in to comment.