import dht
import time
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from time import sleep
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000) #initializing the I2C method for ESP8266
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
# Define PID parameters
Kp = 1.0
Ki = 0.1
Kd = 0.01
# Setpoint values
setpoint_temp = 37.6 # Adjust as needed
setpoint_humidity = 55.0 # Adjust as needed
# Initialize PID variables
integral_temp = 0.0
integral_humidity = 0.0
last_error_temp = 0.0
last_error_humidity = 0.0
# Initialize DHT22 sensor
sensor_pin = 14 # Example pin, adjust as needed
dht_sensor = dht.DHT22(Pin(sensor_pin))
# Simulated heater and humidifier pins
heater_pin = 15 # Example pin, adjust as needed
humidifier_pin = 16 # Example pin, adjust as needed
# Initialize heater and humidifier control variables
heater_state = False
humidifier_state = False
# Define hysteresis parameters
hysteresis_temp = 0.5 # Adjust as needed
hysteresis_humidity = 5.0 # Adjust as needed
def read_sensor():
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
return temperature, humidity
def pid_control(actual, setpoint, integral, last_error, Kp, Ki, Kd):
error = setpoint - actual
integral = integral + error
derivative = error - last_error
output = Kp * error + Ki * integral + Kd * derivative
return output, integral
def control_heater(heater_state):
# Simulated function to control the heater
# You would replace this with your actual heater control logic
return not heater_state # Toggle the heater state for simulation
def control_humidifier(humidifier_state):
# Simulated function to control the humidifier
# You would replace this with your actual humidifier control logic
return not humidifier_state # Toggle the humidifier state for simulation
def apply_control(output, pin_state, hysteresis):
# Simulated function to apply the control output to the pin state with hysteresis
# You would replace this with your actual control mechanism
if output > hysteresis:
return control_heater(pin_state)
elif output < -hysteresis:
return control_humidifier(pin_state)
else:
return pin_state
# Main PID control loop
while True:
temperature, humidity = read_sensor()
# Temperature control
temp_output, integral_temp = pid_control(temperature, setpoint_temp, integral_temp, last_error_temp, Kp, Ki, Kd)
# Apply temp_output to control temperature
heater_state = apply_control(temp_output, heater_state, hysteresis_temp)
# Humidity control
humidity_output, integral_humidity = pid_control(humidity, setpoint_humidity, integral_humidity, last_error_humidity, Kp, Ki, Kd)
# Apply humidity_output to control humidity
humidifier_state = apply_control(humidity_output, humidifier_state, hysteresis_humidity)
# Update last error values
last_error_temp = temperature - setpoint_temp
last_error_humidity = humidity - setpoint_humidity
# Print sensor readings
print('Temperature: %3.1f C' % temperature)
print('Humidity: %3.1f %%' % humidity)
lcd.putstr("PID temp & Humid")
time.sleep(1)
lcd.clear()
lcd.putstr('Temp: %3.1f C' % temperature)
lcd.putstr(' Humid: %3.1f %%' % humidity)
time.sleep(5)
# Print control outputs (for simulation purposes)
print('Heater State:', 'ON' if heater_state else 'OFF')
print('Humidifier State:', 'ON' if humidifier_state else 'OFF')
lcd.clear()
lcd.putstr('HeaterS: %3.1f' % heater_state)
lcd.putstr(' HumidS: %3.1f ' % humidifier_state)
time.sleep(5)
#lcd.putstr('Humidifier State:', 'ON' if humidifier_state else 'OFF')
lcd.clear()
# Delay for a short period
time.sleep(5)