#include "DHT.h"
#include <WiFi.h>
#include <HTTPClient.h>
#define DHTPIN 4 // DHT sensor connected to GPIO 4
#define DHTTYPE DHT22 // DHT22 sensor type
#define RELAY_PIN 19 // Relay module pin connected to the fan
#define POT_PIN 35 // Potentiometer pin to set threshold
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak API
const char* server = "http://api.thingspeak.com/update";
const char* apiKey = "YD85BTR7JJQGHH2X";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(RELAY_PIN, OUTPUT); // Relay pin setup
pinMode(POT_PIN, INPUT); // Potentiometer setup
// Start Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWi-Fi Connected!");
}
void loop() {
// Read sensor data
float temperature = readTemperature();
float humidity = readHumidity();
// Calculate heat index
float heatIndex = computeHeatIndex(temperature, humidity);
// Calculate threshold from potentiometer
float threshold = calculateThreshold();
// Control fan based on heat index and threshold
controlFan(heatIndex, threshold);
// Send data to ThingSpeak
sendDataToThingSpeak(temperature, humidity, heatIndex, threshold);
// Wait 1 minute before next measurement
delay(6000);
}
// Function to read temperature
float readTemperature() {
float temp = dht.readTemperature();
if (isnan(temp)) {
Serial.println("Failed to read temperature!");
return -1; // Return an invalid value
}
return temp;
}
// Function to read humidity
float readHumidity() {
float hum = dht.readHumidity();
if (isnan(hum)) {
Serial.println("Failed to read humidity!");
return -1; // Return an invalid value
}
return hum;
}
// Simple computeHeatIndex function
float computeHeatIndex(float temp, float humidity) {
// Approximation formula for heat index
return temp + (0.55 * (0.55 - humidity / 100) * (temp - 14.5));
}
// Function to calculate threshold using rule of three
float calculateThreshold() {
int potValue = analogRead(POT_PIN);
return (potValue * 20.0 / 4095.0) + 20.0; // Map potentiometer (0-4095) to range (20-40)
}
// Function to control the fan
void controlFan(float heatIndex, float threshold) {
if (heatIndex > threshold) {
digitalWrite(RELAY_PIN, HIGH); // Turn fan ON
Serial.println("Fan ON");
} else {
digitalWrite(RELAY_PIN, LOW); // Turn fan OFF
Serial.println("Fan OFF");
}
}
// Function to send data to ThingSpeak
void sendDataToThingSpeak(float temperature, float humidity, float heatIndex, float threshold) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey +
"&field1=" + String(temperature) +
"&field2=" + String(humidity) +
"&field3=" + String(heatIndex) +
"&field4=" + String(threshold);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak successfully.");
} else {
Serial.println("Failed to send data to ThingSpeak.");
}
http.end();
} else {
Serial.println("Wi-Fi not connected.");
}
}