#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
// WiFi credentials
const char* SSID = "Wokwi-GUEST";
const char* Pass = "";
// ThingSpeak credentials
const int myChannelNumber = 2546316;
const char* myApiKey = "62SEI236MWC7UKXF";
const char* server = "api.thingspeak.com";
// DHT22 sensor pin
const int DHT_PIN = 13;
// Light, ventilation, and trap pins
const int LIGHT_PIN = 14; // Change according to your wiring
const int VENT1_PIN = 15; // Ventilation 1 pin
const int VENT2_PIN = 16; // Ventilation 2 pin
const int TRAP1_PIN = 17; // Trap 1 pin
const int TRAP2_PIN = 18; // Trap 2 pin
// Gas sensor pins (analog)
const int OXYGEN_PIN = 32; // Example pin for oxygen sensor
const int CO2_PIN = 33; // Example pin for CO2 sensor (MQ-135)
const int NH3_PIN = 34; // Example pin for NH3 sensor (MQ-135)
// NTP Client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 0, 60000); // Update time every 60 seconds
DHTesp dhtSensor;
WiFiClient client;
// Function to convert analog reading to oxygen concentration (ppm)
float convertToOxygenConcentration(int analogValue) {
// Example conversion logic
return analogValue * 0.01; // Dummy conversion
}
// Function to convert analog reading to CO2 concentration (ppm)
float convertToCO2Concentration(int analogValue) {
// Example conversion logic
return analogValue * 0.02; // Dummy conversion
}
// Function to convert analog reading to NH3 concentration (ppm)
float convertToNH3Concentration(int analogValue) {
// Example conversion logic
return analogValue * 0.03; // Dummy conversion
}
void setup() {
Serial.begin(115200);
// Initialize DHT sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// Initialize light pin
pinMode(LIGHT_PIN, OUTPUT);
// Initialize ventilation pins
pinMode(VENT1_PIN, OUTPUT);
pinMode(VENT2_PIN, OUTPUT);
// Initialize trap pins
pinMode(TRAP1_PIN, OUTPUT);
pinMode(TRAP2_PIN, OUTPUT);
// Connect to WiFi
WiFi.begin(SSID, Pass);
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);
// Initialize NTP Client
timeClient.begin();
}
void loop() {
// Update time
timeClient.update();
// Get current hour
int currentHour = timeClient.getHours();
// Control light based on time
if (currentHour >= 1 && currentHour < 24) { // Light ON from 1 AM to 24 PM (23 hours)
digitalWrite(LIGHT_PIN, HIGH); // Turn on light
} else {
digitalWrite(LIGHT_PIN, LOW); // Turn off light
}
// Read temperature
float temperature = dhtSensor.getTemperature();
// Read gas sensor values
int oxygenValue = analogRead(OXYGEN_PIN);
int co2Value = analogRead(CO2_PIN);
int nh3Value = analogRead(NH3_PIN);
// Convert analog readings to gas concentrations (requires calibration)
float oxygenConcentration = convertToOxygenConcentration(oxygenValue);
float co2Concentration = convertToCO2Concentration(co2Value);
float nh3Concentration = convertToNH3Concentration(nh3Value);
// Add gas sensor values to ThingSpeak fields
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, oxygenConcentration);
ThingSpeak.setField(3, co2Concentration);
ThingSpeak.setField(4, nh3Concentration);
// Check conditions for ventilation and traps
if (oxygenConcentration < 750 && co2Concentration > 400 && nh3Concentration >= 20) {
// Open both ventilators and traps
digitalWrite(VENT1_PIN, HIGH);
digitalWrite(VENT2_PIN, HIGH);
digitalWrite(TRAP1_PIN, HIGH);
digitalWrite(TRAP2_PIN, HIGH);
} else {
// Close both ventilators and traps
digitalWrite(VENT1_PIN, LOW);
digitalWrite(VENT2_PIN, LOW);
digitalWrite(TRAP1_PIN, LOW);
digitalWrite(TRAP2_PIN, LOW);
}
// Write data to ThingSpeak
int status = ThingSpeak.writeFields(myChannelNumber, myApiKey);
Serial.println("Temperature: " + String(temperature, 2) + "°C");
Serial.println("Oxygen Concentration: " + String(oxygenConcentration) + "ppm");
Serial.println("CO2 Concentration: " + String(co2Concentration) + "ppm");
Serial.println("NH3 Concentration: " + String(nh3Concentration) + "ppm");
if (status == 200) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error " + String(status));
}
Serial.println("---");
delay(1000); // Delay for 1 second
}