import dht
from time import sleep
from machine import Pin
sensor = dht.DHT22(Pin(22))
logFile = open('log.txt', 'a')
while True:
try:
sleep(2)
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
# temp_f = temp * (9/5) + 32.0
print('Temperature: %3.1f C' %temp)
# print('Temperature: %3.1f F' %temp_f)
print('Humidity: %3.1f %%' %hum)
logFile.write('TEMP:' + str(temp) + ';HUMID:' + str(hum) + '\n')
except OSError as e:
print('Failed to read sensor.')