#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#include <LiquidCrystal_I2C.h>
#include <MQ135.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
const int mq135_simulator = 32;
const int DHT_PIN = 12;
const int BUZ_PIN = 13;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber =2431632 ;
const char* myApiKey = "PS8Y0KR4R0FSN5K1";
const char* server = "api.thingspeak.com";
DHTesp dhtSensor;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
WiFiClient client;
float Read_ppm_data()
{
// Read the analog value from the potentiometer (10-bit range)
int value = analogRead(mq135_simulator);
// Map the 10-bit range to 0-1023
int to_1023_value = map(value, 0, 1023, 0, 1023);
// Adjust the formula based on the characteristics of your MQ135 sensor
// This is just a placeholder and may need to be modified
float to_ppm = to_1023_value * 1200.0 / 1023.0;
return to_ppm;
}
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(BUZ_PIN, OUTPUT);
pinMode(mq135_simulator, INPUT);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
lcd.init();
lcd.backlight();
}
void loop() {
// TempAndHumidity data = dhtSensor.getTempAndHumidity();
float ppm = Read_ppm_data();
TempAndHumidity data = dhtSensor.getTempAndHumidity();
ThingSpeak.setField(1,data.temperature);
ThingSpeak.setField(2,data.humidity);
ThingSpeak.setField(3,ppm);
if (data.temperature > 35 || data.temperature < 12 || data.humidity > 70 && data.humidity < 40) {
for (int i = 0; i < 6; i++) {
tone(BUZ_PIN, 440, 500); // A4
delay(400);
tone(BUZ_PIN, 523, 500); // C5
delay(400);
tone(BUZ_PIN, 587, 500); // D5
delay(400);
tone(BUZ_PIN, 659, 500); // E5
delay(400);
}
delay(500); // Pause between repetitions
}else{
noTone(BUZ_PIN);
}
int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("AQI: " + String(ppm, 3) + "ppm");
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
lcd.setCursor(0, 0);
lcd.print(" Temp: " + String(data.temperature, 2) + "\xDF"+"C ");
lcd.setCursor(0, 1);
lcd.print(" Humidity: " + String(data.humidity, 1) + "% ");
lcd.setCursor(0, 2);
lcd.print(" AQI: " + String(ppm, 3) + "ppm ");
lcd.print("Wokwi Online IoT");
delay(1000);
delay(10000);
}