Initial commit. Includes DHT22 and PushButton.
This commit is contained in:
40
dht22/print_DHT22.py
Executable file
40
dht22/print_DHT22.py
Executable file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
import adafruit_dht
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def get_sensor_data(dht22):
|
||||
try:
|
||||
temperature = 9./5. * dht22.temperature + 32.
|
||||
humidity = dht22.humidity
|
||||
return temperature, humidity
|
||||
except RuntimeError as e:
|
||||
print("Reading from DHT failure: ", e.args)
|
||||
return None
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# Setup for Hardware
|
||||
DHT22_PIN = 4
|
||||
dht_device = adafruit_dht.DHT22(4)
|
||||
file = '/home/pi/code/dht22/log.csv'
|
||||
|
||||
with open(file,'a') as f:
|
||||
|
||||
try:
|
||||
while True:
|
||||
data = get_sensor_data(dht22=dht_device)
|
||||
if data:
|
||||
temperature, humidity = get_sensor_data(dht22=dht_device)
|
||||
current_time = datetime.now().strftime("%H:%M:%S")
|
||||
print(f'{humidity:.2f}% {temperature:.2f}degF')
|
||||
f.write(f'{current_time},{humidity:.2f},{temperature:.2f}\n')
|
||||
time.sleep(60)
|
||||
else:
|
||||
time.sleep(0.1)
|
||||
|
||||
finally:
|
||||
GPIO.cleanup()
|
||||
Reference in New Issue
Block a user