# reference https://wokwi.com/projects/399897718244007937
from machine import Pin, SoftI2C
import dht
import time
import machine
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
dSensor = dht.DHT22(Pin(2)) // use GPIO2
def readDHT():
try:
dSensor.measure()
temp = dSensor.temperature()
temp_f = (temp * (9/5)) + 32.0
hum = dSensor.humidity()
print('Temperature= {} C, {} F'.format(temp, temp_f))
print('Humidity= {} '.format(hum))
return temp, hum
except OSError as e:
print('Failed to read data from DHT sensor')
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(1), sda=Pin(0), freq=10000) // I2C use GPIO 0 and 1
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
degree = bytearray([0x1c,0x14,0x1c,0x00,0x00,0x00,0x00,0x00]) #define a customized LCD icon for º (degree sign)
lcd.custom_char(0, degree) #degree sign 'º' will be used as chr(0) in program
while True:
print(readDHT())
t, h = readDHT()
lcd.putstr("Temp: "+str(t)+ chr(0)+"C") #display temperature to LCD
lcd.move_to(0,1)
lcd.putstr('Hum: ' + str(h) + "%")
time.sleep(4)
lcd.clear()