from machine import Pin
from gpio_lcd import GpioLcd
import utime
from time import sleep
import dht
sensor = dht.DHT22(Pin(22))
conversion_factor = 3.3 / 65535
lcd = GpioLcd(rs_pin = Pin(0),
enable_pin = Pin(1),
d4_pin = Pin(2),
d5_pin = Pin(3),
d6_pin = Pin(4),
d7_pin = Pin(5),
num_lines = 2, num_columns = 16)
degree = bytearray([0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00])
def sensorTH():
try:
sleep(2)
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
return temp, hum
except OSError as e:
print('Failed to read sensor.')
while True:
temperature, humidity = sensorTH()
temp_f = temperature * (9/5) + 32.0
lcd.move_to(0, 0)
lcd.putstr("Temp")
lcd.move_to(4, 0)
lcd.custom_char(0, degree)
lcd.putchar(chr(0))
lcd.move_to(5, 0)
lcd.putstr("C / H.Rel %")
lcd.move_to(1, 1)
temp_fmt = "{0:.2f}".format(temperature)
lcd.putstr(temp_fmt)
lcd.move_to(10, 1)
hum_fmt = "{0:.2f}".format(humidity)
lcd.putstr(hum_fmt)
print('Temperature: ', temp_fmt, "C")
print('Temperature: %3.2f F' %temp_f)
print('Humidity: ', hum_fmt, "%")
utime.sleep(0.5)