-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
39 lines (32 loc) · 1.11 KB
/
logger.py
File metadata and controls
39 lines (32 loc) · 1.11 KB
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
39
import time
import board
import adafruit_dht
import database
# Initialize the DHT device, with data pin connected to:
# GPIO 4 (active)
dhtDevice = adafruit_dht.DHT22(board.D4)
def main():
print("Starting Data Logger...")
database.init_db()
while True:
try:
# Print the values to the serial port
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity
if humidity is not None and temperature_c is not None:
print(f"Temp: {temperature_c:.1f} C Humidity: {humidity:.1f}%")
database.log_data(temperature_c, humidity)
else:
print("Failed to retrieve data from humidity sensor")
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error
# Wait 60 seconds before next reading
time.sleep(60.0)
if __name__ == "__main__":
main()