#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
int ledPin1 = 5; // LED 1 connected to digital pin 5
int ledPin2 = 18; // LED 2 connected to digital pin 18
int ledPin3 = 19; // LED 3 connected to digital pin 19
int potensioPin1 = 4; // Potentiometer 1 connected to analog pin 4
int potensioPin2 = 2; // Potentiometer 2 connected to analog pin 2
int potensioPin3 = 15; // Potentiometer 3 connected to analog pin 15
int val1 = 0; // Variable to store the potentiometer 1 value
int val2 = 0; // Variable to store the potentiometer 2 value
int val3 = 0; // Variable to store the potentiometer 3 value
const int DHT_PIN = 15;
const int LED_PIN = 13;
const int LDR_PIN = 32;
#define LIGHT_SENSOR_PIN 35
#define GAS_PIN 34 // Analog pin for gas sensor
// Define threshold values
const float THRESHOLD_VALUE = 300.0; // Example threshold for gas sensor; adjust as needed
float gasValue = 0;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2555314;
const char* myApiKey = "D2KZTE61AXPF6CJL";
const char* server = "api.thingspeak.com";
DHTesp dhtSensor;
WiFiClient client;
// Define the number of weeks for the forecast
const int NUM_WEEKS = 8;
int currentWeek = 5; // Week 6
int currentDay = 0;
// Ventilation rate for week 6 in m²/h
const float ventilationRate = 1.18;
// Duration of LED operation in seconds
const int LED_DURATION = 23 * 3600; // 23 hours in seconds
// Variables to track LED operation
unsigned long previousMillis = 0;
bool ledOn = false;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_PIN, OUTPUT);
pinMode(LIGHT_SENSOR_PIN, INPUT);
pinMode(ledPin1, OUTPUT); // Set the ledPin1 as an OUTPUT
pinMode(ledPin2, OUTPUT); // Set the ledPin2 as an OUTPUT
pinMode(ledPin3, OUTPUT); // Set the ledPin3 as an OUTPUT
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() {
unsigned long currentMillis = millis();
if (currentWeek == 5) { // Only for week 6
// Define parameters for week 6
float minTemperature = 18;
float maxTemperature = 21;
float minHumidity = 60;
float maxHumidity = 70;
int lightDuration = 23 * 3600; // 23 hours in seconds
// Simulate temperature and humidity readings within the specified range
float temperature = random(minTemperature * 10, (maxTemperature + 1) * 10) / 10.0;
float humidity = random(minHumidity * 10, (maxHumidity + 1) * 10) / 10.0;
TempAndHumidity data;
data.temperature = temperature;
data.humidity = humidity;
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
// Read analog values from the gas sensor
gasValue = analogRead(GAS_PIN);
ThingSpeak.setField(3, gasValue);
// Read analog value from the light sensor
int lightValue = analogRead(LIGHT_SENSOR_PIN);
ThingSpeak.setField(4, lightValue);
// Set the ventilation rate for week 6
ThingSpeak.setField(5, ventilationRate);
// Control the LED based on the conditions
if (data.temperature > 35 || data.temperature < 12 || data.humidity > 70 || data.humidity < 40 || gasValue > THRESHOLD_VALUE) {
digitalWrite(LED_PIN, HIGH);
ledOn = true;
previousMillis = currentMillis;
} else {
// Turn off LED after 23 hours
if (ledOn && currentMillis - previousMillis >= LED_DURATION) {
digitalWrite(LED_PIN, LOW);
ledOn = false;
}
}
// Control the LED based on potentiometer values
val1 = analogRead(potensioPin1);
val2 = analogRead(potensioPin2);
val3 = analogRead(potensioPin3);
// Control LED 1
if (val1 > 0 && val1 < 750) {
analogWrite(ledPin1, 255); // Turn LED 1 fully on
} else {
analogWrite(ledPin1, 0); // Turn LED 1 off
}
// Control LED 2
if (val2 > 0 && val2 < 400) {
analogWrite(ledPin2, 255); // Turn LED 2 fully on
} else {
analogWrite(ledPin2, 0); // Turn LED 2 off
}
// Control LED 3
if (val3 > 0 && val3 < 20) {
analogWrite(ledPin3, 255); // Turn LED 3 fully on
} else {
analogWrite(ledPin3, 0); // Turn LED 3 off
}
// Write fields to ThingSpeak
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// Print sensor values to the Serial Monitor
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("Gas Value: " + String(gasValue));
Serial.println("Light Value: " + String(lightValue));
Serial.println("Ventilation Rate: " + String(ventilationRate) + " m²/h");
if (x == 200) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error: " + String(x));
}
Serial.println("---");
}
delay(10000); // Delay for 10 seconds between each data transmission
}