from machine import Pin, I2C, ADC
import network
import time
import dht
import ujson
from umqtt.simple import MQTTClient
from i2c_lcd import I2cLcd # Use the i2c_lcd module
# I2C Setup for LCD1602
I2C_ADDR = 0x27
I2C_ROWS = 2
I2C_COLS = 16
i2c = I2C(1, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_ROWS, I2C_COLS)
# MQTT Server Parameters
MQTT_CLIENT_ID = "micropython-weather-demo"
MQTT_BROKER = "broker.hivemq.com"
MQTT_USER = "project722"
MQTT_PASSWORD = "9PJ6am#ZyKs36j6"
MQTT_TOPIC = "sensor/distance"
# Initialize DHT22 sensor
sensor = dht.DHT22(Pin(15))
# Initialize LDR sensor
ldr = ADC(Pin(34)) # GPIO 34 for ESP32
ldr.atten(ADC.ATTN_11DB) # Configure ADC to read higher voltage ranges (up to 3.3V)
# Stepper motor setup
pinEnabled = Pin(4, Pin.OUT, value=0)
pinStep = Pin(5, Pin.OUT)
pinDirection = Pin(18, Pin.OUT)
stepsPerRevolution = 200
# Function to display messages on LCD
def lcd_message(line1, line2=""):
lcd.clear()
lcd.putstr(line1)
if line2:
lcd.move_to(0, 1)
lcd.putstr(line2)
time.sleep(2)
# Function to rotate stepper motor
def rotate_stepper():
# Rotate clockwise
pinDirection.on()
for _ in range(stepsPerRevolution):
pinStep.on()
time.sleep_ms(10)
pinStep.off()
time.sleep_ms(10)
# Rotate counter-clockwise
pinDirection.off()
for _ in range(stepsPerRevolution):
pinStep.on()
time.sleep_ms(10)
pinStep.off()
time.sleep_ms(10)
# Display initialization message
lcd_message("Initializing...")
# Connect to WiFi
print("Connecting to WiFi", end="")
lcd_message("Connecting to", "WiFi")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
lcd_message("Connected to", "WiFi!")
# Connect to MQTT server
print("Connecting to MQTT server... ", end="")
lcd_message("Connecting to", "MQTT")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Connected!")
lcd_message("MQTT", "Connected")
prev_weather = ""
prev_ldr_value = -1 # To store the previous LDR value
while True:
# Read DHT22 sensor values
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
# Read LDR sensor value
ldr_value = ldr.read()
ldr_voltage = ldr_value * (3.3 / 4095) # Convert ADC value to voltage
# Display temperature and humidity on LCD
lcd_message(f"Temp: {temperature:.1f}C", f"Humidity: {humidity:.1f}%")
# Display LDR voltage on LCD
lcd_message(f"LDR Voltage:", f"{ldr_voltage:.2f} V")
# Rotate stepper motor if temperature exceeds 25°C
if temperature > 25:
rotate_stepper()
# Prepare MQTT messages
weather_message = ujson.dumps({
"temp": temperature,
"humidity": humidity,
})
ldr_message = ujson.dumps({
"ldr": ldr_voltage
})
# Publish weather data if it has changed
if weather_message != prev_weather:
print("Weather Updated!")
print(f"Reporting to MQTT topic {MQTT_TOPIC}: {weather_message}")
try:
client.publish(MQTT_TOPIC, weather_message)
except OSError:
pass # Silently ignore the error
prev_weather = weather_message
# Publish LDR data if it has changed
if ldr_value != prev_ldr_value:
print("LDR value updated!")
print(f"Reporting LDR value to MQTT topic {MQTT_TOPIC}: {ldr_message}")
try:
client.publish(MQTT_TOPIC, ldr_message)
except OSError:
pass # Silently ignore the error
prev_ldr_value = ldr_value
time.sleep(1)