#Lab32_i2C_LCD1602_Display_DHT.py
# Lib: https://peppe8o.com/download/micropython/LCD/lcd_api.py
# Lib: https://peppe8o.com/download/micropython/LCD/i2c_lcd.py
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import time
from dht import DHT22
sdaPIN= Pin(21) #for ESP32
sclPIN= Pin(22)
i2c= SoftI2C(sda=sdaPIN, scl=sclPIN, freq=100000)
I2C_ADDR = 0x27
print(I2C_ADDR)
# Size display
totalRows = 2
totalColumns = 16
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
dht22 = DHT22(Pin(19))
while True:
try:
time.sleep(0.5)
dht22.measure()
temperature_C = dht22.temperature()
temperature_C = round(temperature_C, 2)
temperature_F = temperature_C * (9/5) + 32.00
temperature_F = round(temperature_F, 2)
humidity = dht22.humidity()
humidity = round(humidity, 2)
print("Temperature: ", temperature_C, 'C', temperature_F, 'F')
print("Humidity: ", humidity, "%")
time.sleep(2)
lcd.putstr(" Weather Today ")
lcd.putstr("5/24/2023 15:25")
time.sleep(3)
lcd.clear()
lcd.putstr(" Temp: ")
lcd.putstr(str(temperature_C))
lcd.putstr(" C ")
lcd.putstr(" Humi: ")
lcd.putstr(str(humidity))
lcd.putstr(" % ")
time.sleep(2)
lcd.clear()
except OSError as e:
print('Failed to read sensor.')