-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht.py
38 lines (30 loc) · 959 Bytes
/
dht.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
import adafruit_dht
import os
from os.path import join, dirname
from dotenv import load_dotenv
# Create .env file path.
dotenv_path = join(dirname(__file__), '.env')
# Load file from the path.
load_dotenv(dotenv_path)
DHT11_PIN = os.getenv('DHT11_PIN')
# Initialize the dht device, with data pin connected to:
# Use a DHT11 sensor
dhtDevice = adafruit_dht.DHT11(int(DHT11_PIN))
# Use a DHT22 sensor
# dhtDevice = adafruit_dht.DHT22(int(DHT11_PIN))
def get_measurements():
try:
temperature = dhtDevice.temperature
return {
'humidity': dhtDevice.humidity,
'temperature_c': temperature,
'temperature_f': temperature * (9 / 5) + 32
}
except RuntimeError as error:
# Errors happen fairly often, DHTs are hard to read, just keep going
print(error.args[0])
return None
except Exception as error:
dhtDevice.exit()
print(error)
return None