#include <WiFi.h>
#include <iostream>
#include <HTTPClient.h>
#include <WebServer.h>
#include <DHT.h>
#include <ThingSpeak.h>
#include <Arduino.h>
int D1,D2,D3,D0;
#define DHTPIN 23
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define LED_GREEN D1
#define LED_YELLOW D2
#define LED_RED D3
#define MQ135 A0
#define ssid "Wokwi-GUEST"
#define password ""
#define PPM_PIN 4
int ppm=0;
float m = -0.3376; //Slope
float b = 0.7165; //Y-Intercept
float R0 = 3.12; //Sensor Resistance in fresh air from previous code
WiFiClient client;
long myChannelNumber = 123456; // Channel id
const char myWriteAPIKey[] = "API_Key";
void setup()
{
Serial.begin(9600);
pinMode(LED_GREEN,OUTPUT);
pinMode(LED_YELLOW,OUTPUT);
pinMode(LED_RED,OUTPUT);
pinMode(MQ135, INPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); // WiFi.begin("Wokwi-GUEST", "");
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.println(WiFi.status());
Serial.print('.');
delay(1000);
}
Serial.println("Connected");
Serial.println(WiFi.status());
Serial.println(WiFi.localIP());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
dht.begin();
ThingSpeak.begin(client);
}
void loop() {
float sensor_volt; //Define variable for sensor voltage
float RS_gas; //Define variable for sensor resistance
float ratio; //Define variable for ratio
int sensorValue;//Variable to store the analog values from MQ-135
float h;
float t;
float ppm_log; //Get ppm value in linear scale according to the the ratio value
float ppm; //Convert ppm value to log scale
h = dht.readHumidity();
delay(4000);
t = dht.readTemperature();
delay(4000);
sensorValue = analogRead(PPM_PIN); //Read analog values of sensor
sensor_volt = sensorValue*(5.0/1023.0); //Convert analog values to voltage
RS_gas = ((5.0*1.0)/sensor_volt)-1.0; //Get value of RS in a gas
ratio = RS_gas/R0; // Get ratio RS_gas/RS_air
ppm_log = (log10(ratio)-b)/m; //Get ppm value in linear scale according to the ratio value
ppm = pow(10, ppm_log); //Convert ppm value to log scale
Serial.println("Temperature: ");
Serial.println(t);
Serial.println("Humidity: ");
Serial.println(h);
Serial.println("Our desired PPM = ");
Serial.println(ppm);
ThingSpeak.writeField(myChannelNumber, 1, t, myWriteAPIKey);
delay(20000);
ThingSpeak.writeField(myChannelNumber, 2, h, myWriteAPIKey);
delay(20000);
ThingSpeak.writeField(myChannelNumber, 3, ppm, myWriteAPIKey);
delay(20000);
if(ppm<=100)
{
digitalWrite(LED_GREEN,HIGH);
digitalWrite(LED_YELLOW,LOW);
digitalWrite(LED_RED,LOW);
}
else if(ppm<=200)
{
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_YELLOW,HIGH);
digitalWrite(LED_RED,LOW);
}
else
{
digitalWrite(LED_GREEN,LOW);
digitalWrite(LED_YELLOW,LOW);
digitalWrite(LED_RED,HIGH);
}
delay(2000);
}