##################################################
# Part 1: Display Temperature and Humidity
# Read Temperature and Humidity using DHT22 Sensor and display values on console and LCD Display
# Author: Dinesh Madumal (24007386)
# Version: 1.0
# Last Edit: 16/05/2023
# Import Libraries
from machine import I2C, Pin
import utime
from dht import DHT22
from pico_i2c_lcd import I2cLcd
def main():
# Creating a DHT object and assign GP0 pin
dht = DHT22(Pin(0))
# Creating a I2C object, specifying the data (SDA) and clock (SCL) pins
# SDA = GP16 and SCL = GP17
i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000)
# Getting I2C addres
I2C_ADDR = i2c.scan()[0]
# Creating a LCD object using the I2C address and specifying number of rows and columns in the LCD
# LCD number of rows = 4, number of columns = 20
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20)
# Continuously get DHT22 sensor readings while the board has power
while True:
# Getting temperature and humidity readings from DHT22 sensor
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
print("**************************************")
print(temp)
print(hum)
print("**************************************")
# Displaying values to the console
print("Temperature: {}°C Humidity: {:.0f}% ".format(temp, hum))
# Displaying values to the LCD display
lcd.putstr("Temperature: {}°C Humidity: {:.0f}% ".format(temp, hum))
# Wait for 2 seconds before taking the next measurement
utime.sleep(2)
# Clear the text in LCD display
lcd.clear()
if __name__ == "__main__":
main()