#include <LiquidCrystal_I2C.h>
#include <ESP8266WebServer.h>
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* serverAddress = "api.thingspeak.com";
const char* apiKey = "YourThingSpeakAPIKey";
const int updateInterval = 60000; // Update data every 1 minute
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address if necessary
const int fanPin = 3; // Electric Fan
const int dehumidifierPin = 4; // Dehumidifier
const int pumpPin = 5; // Water Pump
const int lightPin = 6; // Light Bulb
int tempThreshold = 25; // Temperature threshold in degrees Celsius
int humidityThreshold = 60; // Humidity threshold in percentage
int moistureThreshold = 500; // Soil moisture threshold
int lightThreshold = 500; // Light intensity threshold
// Initialize LCD Display
lcd.print("Initializing...");
// Initialize DHT11 Sensor
pinMode(dehumidifierPin, OUTPUT);
pinMode(pumpPin, OUTPUT);
pinMode(lightPin, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
lcd.print("Connecting...");
// Start the Wi-Fi server
// Display IP address on LCD
lcd.print(WiFi.localIP());
// Check for new client connections
WiFiClient client = server.available();
// Process the client request
processClientRequest(client);
temperature = dht.readTemperature();
humidity = dht.readHumidity();
moisture = analogRead(A0);
lightIntensity = analogRead(A1);
// Display readings on LCD
lcd.print("L.intenstity: ");
lcd.print(lightIntensity);
// Check and control parameters
if (temperature > tempThreshold) {
digitalWrite(fanPin, HIGH); // Turn on the fan
digitalWrite(fanPin, LOW); // Turn off the fan
if (humidity > humidityThreshold) {
digitalWrite(dehumidifierPin, HIGH); // Turn on the dehumidifier
digitalWrite(dehumidifierPin, LOW); // Turn off the dehumidifier
if (moisture < moistureThreshold) {
digitalWrite(pumpPin, HIGH); // Turn on the water pump
digitalWrite(pumpPin, LOW); // Turn off the water pump
if (lightIntensity < lightThreshold) {
digitalWrite(lightPin, HIGH); // Turn on the light bulb
digitalWrite(lightPin, LOW); // Turn off the light bulb
delay(2000); // Update readings every 2 seconds
void processClientRequest(WiFiClient& client) {
String request = client.readStringUntil('\r');
// Check if the request is to adjust thresholds
if (request.indexOf("/thresholds") != -1) {
// Extract the values from the request
int equalsIndex = request.indexOf('=');
int ampersandIndex = request.indexOf('&');
//int newTempThreshold = request.substring(equalsIndex + 1, apologize, but it seems that the code got cut off) //Here's the rest of the code:
int newTempThreshold = request.substring(equalsIndex + 1, ampersandIndex).toInt();
equalsIndex = request.indexOf('=', ampersandIndex);
ampersandIndex = request.indexOf('&', ampersandIndex + 1);
int newHumidityThreshold = request.substring(equalsIndex + 1, ampersandIndex).toInt();
equalsIndex = request.indexOf('=', ampersandIndex);
ampersandIndex = request.indexOf('&', ampersandIndex + 1);
int newMoistureThreshold = request.substring(equalsIndex + 1, ampersandIndex).toInt();
equalsIndex = request.indexOf('=', ampersandIndex);
int newLightThreshold = request.substring(equalsIndex + 1).toInt();
tempThreshold = newTempThreshold;
humidityThreshold = newHumidityThreshold;
moistureThreshold = newMoistureThreshold;
lightThreshold = newLightThreshold;
// Send a response to the client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("<html><body>");
client.println("<h1>Thresholds updated successfully</h1>");
client.println("</body></html>");
else if (request.indexOf("/trends") != -1) {
// Send the recorded trends to the client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("<html><body>");
client.println("<h1>Recorded Trends</h1>");
// Display the recorded trends
client.print("Temperature: ");
client.print(temperature);
client.print("Humidity: ");
client.print("Moisture: ");
client.print("Light Intensity: ");
client.print(lightIntensity);
client.println("</body></html>");
// Send a 404 Not Found response
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println("<html><body>");
client.println("<h1>404 Not Found</h1>");
client.println("</body></html>");