from machine import I2C, Pin, ADC
from time import sleep
from dht import DHT22
from pico_i2c_lcd import I2cLcd
from picozero import Servo # Importing Servo class from picozero
# Initialize DHT22 for temperature and humidity measurement
dht = DHT22(Pin(15))
# Initialize I2C for the LCD display (using GPIO 20 for SDA and GPIO 21 for SCL)
i2c = I2C(0, scl=Pin(21), sda=Pin(20), freq=100000) # Updated to use pins on the right
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20) # 20x4 I2C LCD initialization
# Initialize ADC for the LDR (connected to ADC pin)
ldr = ADC(Pin(26)) # LDR connected to GPIO 26 (ADC pin)
# Initialize Servo for window control using picozero (GPIO 14)
servo = Servo(14)
# Initialize relay for HVAC control (GPIO 27)
relay = Pin(27, Pin.OUT)
# Adjust the range to control the servo for window mechanism
def control_window(open_window):
if open_window:
# Open window (move servo to 180 degrees)
servo.value = 1 # Full open position
print("Window opened.")
else:
# Close window (move servo to 0 degrees)
servo.value = 0 # Full closed position (adjust this if necessary)
print("Window closed.")
# Helper function to control the HVAC system (via relay)
def control_hvac(turn_on):
if turn_on:
relay.value(1) # Turn on HVAC (activate relay)
print("HVAC turned on.")
else:
relay.value(0) # Turn off HVAC (deactivate relay)
print("HVAC turned off.")
# Function to classify weather as good or bad based on conditions
def classify_weather(temp, humidity, light):
if temp > 30 or humidity > 75 or light < 1000:
return 0 # Bad weather
else:
return 1 # Good weather
# Use the adc_to_lux calibration function
def adc_to_lux(adc_value):
"""Convert ADC value to approximate lux value using a linear mapping."""
# Observed ADC values in Wokwi:
# ADC 512 -> 100,000 lux (bright light)
# ADC 65535 -> 0.1 lux (dim light)
lux = 100000 - (adc_value - 512) * (100000 - 0.1) / (65535 - 512)
return max(lux, 0.1) # Ensure lux value doesn't go below 0.1
while True:
# Measure temperature and humidity from DHT22 sensor
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# Measure light intensity from LDR (0-65535 for 16-bit resolution)
light_adc = ldr.read_u16()
# Convert ADC value to lux
light_lux = adc_to_lux(light_adc)
# Classify weather (1 = Good, 0 = Bad)
weather_label = classify_weather(temp, hum, light_lux)
# Prepare weather status text
weather_status = "Good" if weather_label == 1 else "Bad"
# Show data on 20x4 LCD
lcd.clear()
# First line: Temperature and Humidity
lcd.putstr(f"Temp:{temp:.1f}C Hum:{hum:.0f}%")
# Second line: Light Intensity in Lux
lcd.move_to(0, 1)
lcd.putstr(f"Light: {light_lux:.1f} lux")
# Third line: Weather Status
lcd.move_to(0, 2)
lcd.putstr(f"Weather: {weather_status}")
# Fourth line: Optional - Any extra info can go here
lcd.move_to(0, 3)
lcd.putstr("System Operational")
# Control mechanisms based on weather classification
if weather_label == 1: # Good weather
control_window(True) # Open window
control_hvac(False) # Turn off HVAC
print("Good weather.")
else: # Bad weather
control_window(False) # Close window
control_hvac(True) # Turn on HVAC
print("Bad weather.")
sleep(2) # Delay before the next measurement