#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#include <ESP32Servo.h>
// WiFi and ThingSpeak details
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2555788;
const char* myApiKey = "3UTG46FNKC4D69JS";
const char* server = "api.thingspeak.com";
// Pins
const int DHT_PIN = 15;
const int LED_PIN = 13;
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
const int SERVO_PIN_1 = 5; // Servo motor 1 pin
const int SERVO_PIN_2 = 16; // Servo motor 2 pin
// Ventilation rates (m2/h)
const float ventilationRates[] = {0.16, 0.42, 0.59, 0.83, 0.93, 1.18, 1.35, 1.52};
// Light durations (hours)
const int lightDurationsWeek1Day1to4 = 23;
const int lightDurationsWeek1Day5to7 = 20;
const int lightDurationsWeek2to8 = 23;
DHTesp dhtSensor;
WiFiClient client;
Servo servo1; // Create a Servo object for servo 1
Servo servo2; // Create a Servo object for servo 2
int currentWeek = 1; // Change this to simulate different weeks
int currentDay = 1; // Change this to simulate different days
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_PIN, OUTPUT);
pinMode(sensorPinO2, INPUT);
pinMode(sensorPinCO2, INPUT);
pinMode(sensorPinNH3, INPUT);
servo1.attach(SERVO_PIN_1); // Attach servo 1 to the pin
servo2.attach(SERVO_PIN_2); // Attach servo 2 to the pin
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);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Read sensor values
int O2Value = analogRead(sensorPinO2);
int CO2Value = analogRead(sensorPinCO2);
int NH3Value = analogRead(sensorPinNH3);
// Determine LED status based on conditions
int ledStatus = LOW;
// Ventilation and light duration based on current week and day
float ventilation = 0;
int lightDuration = 0;
// Conditions for different weeks and days
switch (currentWeek) {
case 1:
ventilation = ventilationRates[0];
if (currentDay <= 4) {
lightDuration = lightDurationsWeek1Day1to4;
} else {
lightDuration = lightDurationsWeek1Day5to7;
}
break;
case 2:
ventilation = ventilationRates[1];
lightDuration = lightDurationsWeek2to8;
break;
case 3:
ventilation = ventilationRates[2];
lightDuration = lightDurationsWeek2to8;
break;
case 4:
ventilation = ventilationRates[3];
lightDuration = lightDurationsWeek2to8;
break;
case 5:
ventilation = ventilationRates[4];
lightDuration = lightDurationsWeek2to8;
break;
case 6:
case 7:
case 8:
ventilation = ventilationRates[currentWeek - 1];
lightDuration = lightDurationsWeek2to8;
break;
}
// Control servos based on humidity and temperature
if (data.humidity < 60 || data.temperature < 35) {
servo1.write(90); // Move servo 1 to 90 degrees
servo2.write(90); // Move servo 2 to 90 degrees
} else {
servo1.write(0); // Move servo 1 to 0 degrees
servo2.write(0); // Move servo 2 to 0 degrees
}
// Check if the temperature is over 30 degrees Celsius
if (data.temperature > 30) {
ledStatus = HIGH;
digitalWrite(LED_PIN, HIGH); // Turn on the LED
} else {
digitalWrite(LED_PIN, LOW); // Turn off the LED
}
// Set ThingSpeak fields
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
ThingSpeak.setField(3, O2Value);
ThingSpeak.setField(4, CO2Value);
ThingSpeak.setField(5, NH3Value);
ThingSpeak.setField(6, ledStatus);
ThingSpeak.setField(7, ventilation);
ThingSpeak.setField(8, lightDuration);
// Push data to ThingSpeak
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// Print sensor values and LED status to Serial Monitor
Serial.println("Week: " + String(currentWeek));
Serial.println("Day: " + String(currentDay));
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 Status: " + String(ledStatus));
Serial.println("Ventilation: " + String(ventilation) + " m2/h");
Serial.println("Light Duration: " + String(lightDuration) + " hours");
// 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
}