Mechatronics LAB
Published © GPL3+

ESP32-based Smart Greenhouse Control with DHT Sensor (ThingS

ESP32-based Smart Greenhouse Control with DHT Sensor (ThingSpeak)

BeginnerProtip1 hour205
ESP32-based Smart Greenhouse Control with DHT Sensor (ThingS

Things used in this project

Hardware components

esp
×1
dht22
×1

Story

Read more

Schematics

ESP32-based Smart Greenhouse Control with DHT Sensor (ThingSpeak)

Code

ESP32-based Smart Greenhouse Control with DHT Sensor (ThingSpeak)

Arduino
ESP32-based Smart Greenhouse Control with DHT Sensor (ThingSpeak)
#include <WiFi.h>
#include <ThingSpeak.h>
#include <DHT.h>

#define DHTPIN 4     // DHT sensor connected to Pin D4
#define DHTTYPE DHT22 // or DHT11

const char* ssid = "your_SSID";     // Wi-Fi network name
const char* password = "your_PASSWORD"; // Wi-Fi password

unsigned long myChannelNumber = 123456;  // Your ThingSpeak Channel Number
const char * myWriteAPIKey = "your_WRITE_API_KEY"; // Your ThingSpeak Write API Key

WiFiClient client;
DHT dht(DHTPIN, DHTTYPE);

int fanPin = 5;   // Relay pin for fan
int pumpPin = 18; // Relay pin for water pump

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  // Wait for the WiFi connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  ThingSpeak.begin(client);
  
  dht.begin();
  
  // Set pin modes
  pinMode(fanPin, OUTPUT);
  pinMode(pumpPin, OUTPUT);
}

void loop() {
  float temperature = dht.readTemperature(); // Temperature in Celsius
  float humidity = dht.readHumidity();       // Humidity in percentage

  // Ensure data is valid
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Display readings in the serial monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C ");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  // Control the fan and water pump based on temperature and humidity
  if (temperature > 30) {
    digitalWrite(fanPin, HIGH);  // Turn on fan if temperature is above 30°C
  } else {
    digitalWrite(fanPin, LOW);   // Turn off fan
  }

  if (humidity < 50) {
    digitalWrite(pumpPin, HIGH); // Turn on water pump if humidity is below 50%
  } else {
    digitalWrite(pumpPin, LOW);  // Turn off water pump
  }

  // Upload data to ThingSpeak
  ThingSpeak.setField(1, temperature); // Temperature data
  ThingSpeak.setField(2, humidity);    // Humidity data
  ThingSpeak.setField(3, (digitalRead(fanPin) == HIGH) ? "ON" : "OFF"); // Fan status
  ThingSpeak.setField(4, (digitalRead(pumpPin) == HIGH) ? "ON" : "OFF"); // Water pump status
  
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); // Write data to ThingSpeak

  // Wait 30 seconds before sending the next update
  delay(30000); 
}

Credits

Mechatronics LAB
75 projects • 47 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .
Contact

Comments

Please log in or sign up to comment.