from machine import Pin, ADC, I2C
from time import sleep
import dht
from umqtt.simple import MQTTClient
from lcd_api import LcdApi
from pcf8574_lcd import PCF8574Lcd
BENZENE_THRESHOLD = 5.0
AMMONIA_THRESHOLD = 25.0
CO2_THRESHOLD = 1000.0
SMOKING_THRESHOLD = 50.0
ALCOHOL_THRESHOLD = 100.0
ssid = "Wokwi-GUEST"
password = ""
mqtt_server = "test.mosquitto.org"
mqtt_port = 1883
DHT_PIN = 18
BUZZER_PIN = 19
MQ135_PIN = 32
# Initialize DHT sensor
dht_sensor = dht.DHT22(Pin(DHT_PIN))
# Initialize ADC for MQ135 sensor
mq135_adc = ADC(Pin(MQ135_PIN))
# Initialize I2C for LCD
i2c = I2C(scl=Pin(22), sda=Pin(21))
lcd = PCF8574Lcd(i2c, 0x27)
# Initialize MQTT client
client = MQTTClient("esp32", mqtt_server, mqtt_port)
def wifi_connect():
import network
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while not station.isconnected():
sleep(0.5)
print(".", end="")
print("Connected to WiFi")
def lcd_handle(temperature, humidity, mq135_value):
lcd.clear()
lcd.putstr("Current data:\n")
lcd.putstr("Temperature: {}oC\n".format(temperature))
lcd.putstr("Humidity: {}%\n".format(humidity))
lcd.putstr("MQ135: {}PPM".format(mq135_value))
def read_ppm_data():
return mq135_adc.read()
# Implement functions to read other sensor data (benzene, ammonia, CO2, smoking, alcohol)
def warning(temp, humidity, ppm, benzene, ammonia, co2, smoking, alcohol):
gas_warning = False
poor_air_quality = False
if benzene > BENZENE_THRESHOLD or ammonia > AMMONIA_THRESHOLD or co2 > CO2_THRESHOLD or smoking > SMOKING_THRESHOLD or alcohol > ALCOHOL_THRESHOLD:
gas_warning = True
if 40 < temp <= 70 or 800 < ppm <= 1000:
poor_air_quality = True
if gas_warning or poor_air_quality:
pass # Activate buzzer
else:
pass # Deactivate buzzer
print("Temp: {:.2f}°C".format(temp))
print("Humidity: {:.1f}%".format(humidity))
print("PPM In Air: {:.2f}ppm".format(ppm))
print("Benzene: {:.2f} ppm".format(benzene))
print("Ammonia: {:.2f} ppm".format(ammonia))
print("CO2: {:.2f} ppm".format(co2))
print("Smoking: {:.2f} ppm".format(smoking))
print("Alcohol: {:.2f} ppm".format(alcohol))
print("--------")
def mqtt_reconnect():
while not client.isconnected():
print("Attempting MQTT reconnection...")
try:
client.connect()
print("Connected to your device!")
except:
print("Try again in 5 seconds...")
sleep(5)
def main():
wifi_connect()
lcd.putstr("Connected to WiFi")
sleep(1)
lcd.clear()
while True:
mqtt_reconnect()
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
ppm = read_ppm_data()
lcd_handle(temperature, humidity, ppm)
# Read values of benzene, ammonia, CO2, smoking, and alcohol sensors
benzene_value = 0.0 # Implement function to read benzene sensor
ammonia_value = 0.0 # Implement function to read ammonia sensor
co2_value = 0.0 # Implement function to read CO2 sensor
smoking_value = 0.0 # Implement function to read smoking sensor
alcohol_value = 0.0 # Implement function to read alcohol sensor
# Call warning function with all sensor values
warning(temperature, humidity, ppm, benzene_value, ammonia_value, co2_value, smoking_value, alcohol_value)
# Publish data to MQTT broker
client.publish("21127147_21127365_21127644/Data", '{{"temperature": {}, "humidity": {}, "ppm": {}}}'.format(temperature, humidity, ppm))
sleep(5)
if __name__ == "__main__":
main()