# MUHAMMAD IKMAL AFIFI BIN ISMAIL (CB230183)
from machine import Pin, I2C, ADC
from time import sleep
from dht import DHT22
from lcd1602 import LCD
# Constants
LED_PIN = 2
DHT_PIN = 20
POT_PIN = 28
I2C_FREQ = 100000
# Initialize hardware
led_pin = Pin(LED_PIN, Pin.OUT)
dht_pin = Pin(DHT_PIN)
pot_pin = Pin(POT_PIN)
dht = DHT22(dht_pin)
adc = ADC(pot_pin)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=I2C_FREQ)
lcd = LCD()
def set_led(state: bool) -> None:
"""Set the LED state"""
led_pin.value(state)
def read_pot_value() -> int:
"""Read potentiometer value and map it to 0-100 range"""
return int((adc.read_u16() / 65535) * 100)
def display_data(temp: float, hum: float, threshold_temp: int) -> None:
"""Display data on LCD"""
lcd.clear()
lcd.write(0, 0, f'T: {temp:.1f}C')
lcd.write(9, 0, f'H: {hum:.1f}%')
lcd.write(0, 1, f'Total Temp: {threshold_temp:.1f}C')
def main() -> None:
while True:
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# Read potentiometer value for temperature threshold
threshold_temp = read_pot_value()
print(f"Temperature: {temp:.1f}C")
print(f"Humidity: {hum:.1f}%")
print(f"Threshold Temp: {threshold_temp:.1f}C")
display_data(temp, hum, threshold_temp)
sleep(1)
if temp > 40 and threshold_temp > 40:
set_led(True) # Turn on LED
else:
set_led(False) # Turn off LED
if __name__ == '__main__':
main()