#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
// WiFi and ThingSpeak details
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2560763; // Your channel number
const char* myApiKey = "V8X0R3AHWA28OV2V"; // Your API key
const char* server = "api.thingspeak.com";
// Pins
const int DHT_PIN = 15;
const int LED_PIN_1 = 13;
const int LED_PIN_2 = 23;
const int LED_PIN_CONDITIONS = 2; // Pin for LED based on O2, CO2, NH3 conditions
const int sensorPinO2 = 34; // Analog pin for O2 sensor
const int sensorPinCO2 = 35; // Analog pin for CO2 sensor
const int sensorPinNH3 = 32; // Analog pin for NH3 sensor
// Thresholds
const int O2Threshold = 750; // O2 threshold in cm
const int CO2Threshold = 400; // CO2 threshold in ppm
const int NH3Threshold = 20; // NH3 threshold in ppm
DHTesp dhtSensor;
WiFiClient client;
unsigned long previousMillis_1 = 0; // Variable to store the last time LED_1 was updated
unsigned long previousMillis_2 = 0; // Variable to store the last time LED_2 was updated
const long intervalOn_2 = 23000; // Interval to turn on the LED_2 (23 seconds)
const long intervalOff_2 = 1000; // Interval to turn off the LED_2 (1 second)
int ledStatus_1 = LOW; // Initial LED_1 status
int ledStatus_2 = LOW; // Initial LED_2 status
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
pinMode(LED_PIN_CONDITIONS, OUTPUT);
pinMode(sensorPinO2, INPUT);
pinMode(sensorPinCO2, INPUT);
pinMode(sensorPinNH3, INPUT);
connectToWiFi();
ThingSpeak.begin(client);
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print("Attempt ");
Serial.print(attempts);
Serial.println(": WiFi not connected");
attempts++;
if (attempts > 20) { // Retry 20 times before resetting
Serial.println("Failed to connect to WiFi. Resetting...");
ESP.restart(); // Reset the ESP32 if unable to connect
}
}
Serial.println("WiFi connected!");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connection lost. Reconnecting...");
connectToWiFi();
}
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to update the LED_2 status
if (currentMillis - previousMillis_2 >= (ledStatus_2 == HIGH ? intervalOn_2 : intervalOff_2)) {
// Save the last time LED_2 was updated
previousMillis_2 = currentMillis;
// Toggle LED_2 status
ledStatus_2 = (ledStatus_2 == HIGH) ? LOW : HIGH;
// Update LED_2
digitalWrite(LED_PIN_2, ledStatus_2);
}
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Display week based on temperature and humidity conditions
if (data.temperature <= 35 && data.temperature >= 32 && data.humidity >= 55 && data.humidity <= 60) {
Serial.println("Week 1");
} else if (data.temperature == 30 && data.humidity >= 55 && data.humidity <= 60) {
Serial.println("Week 2");
} else if (data.temperature == 28 && data.humidity >= 55 && data.humidity <= 60) {
Serial.println("Week 3");
} else if (data.temperature == 26 && data.humidity >= 55 && data.humidity <= 65) {
Serial.println("Week 4");
} else if (data.temperature == 21 && data.humidity >= 60 && data.humidity <= 70) {
Serial.println("Week 5");
} else if (data.temperature < 21 && data.temperature > 18 && data.humidity >= 60 && data.humidity <= 70) {
Serial.println("Week 6");
digitalWrite(LED_PIN_1, HIGH); // Turn on LED_1 for Week 6 condition
} else if (data.temperature < 21 && data.temperature > 18 && data.humidity >= 60 && data.humidity <= 70) {
Serial.println("Week 7");
} else if (data.temperature < 21 && data.temperature > 18 && data.humidity >= 60 && data.humidity <= 70) {
Serial.println("Week 8");
}
// Check temperature and humidity for LED_1
if ((data.temperature > 21 || data.temperature < 18) && data.humidity >= 60 && data.humidity <= 70) {
digitalWrite(LED_PIN_1, HIGH); // Turn on LED_1
Serial.println("LED_PIN_1 ON: Temperature is out of range");
} else {
digitalWrite(LED_PIN_1, LOW); // Turn off LED_1
Serial.println("LED_PIN_1 OFF: Temperature is within range");
}
// Read sensor values
int O2Value = analogRead(sensorPinO2);
int CO2Value = analogRead(sensorPinCO2);
int NH3Value = analogRead(sensorPinNH3);
// Check O2, CO2, and NH3 conditions for LED
if (O2Value < O2Threshold || CO2Value > CO2Threshold || NH3Value > NH3Threshold) {
digitalWrite(LED_PIN_CONDITIONS, HIGH); // Turn on LED
Serial.println("LED_PIN_CONDITIONS ON: One or more conditions are out of range");
} else {
digitalWrite(LED_PIN_CONDITIONS, LOW); // Turn off LED
Serial.println("LED_PIN_CONDITIONS OFF: All conditions are within range");
}
// Set ThingSpeak fields
ThingSpeak.setField(1, data.humidity);
ThingSpeak.setField(2, data.temperature);
ThingSpeak.setField(3, O2Value);
ThingSpeak.setField(4, CO2Value);
ThingSpeak.setField(5, NH3Value);
ThingSpeak.setField(6, ledStatus_1);
ThingSpeak.setField(7, ledStatus_2);
// Push data to ThingSpeak
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// Print sensor values and LED status to Serial Monitor
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("O2: " + String(O2Value) + " cm");
Serial.println("CO2: " + String(CO2Value) + " ppm");
Serial.println("NH3: " + String(NH3Value) + " ppm");
Serial.println("LED_1 Status: " + String(ledStatus_1));
Serial.println("LED_2 Status: " + String(ledStatus_2));
Serial.println("LED_CONDITIONS Status: " + String(digitalRead(LED_PIN_CONDITIONS)));
// Check if data was successfully pushed to ThingSpeak
if (x == 200) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error: " + String(x));
}
Serial.println("---");
delay(10000); // Wait for 10 seconds before the next loop
}