DIY cooling system with ESP32

In this project, I created an Automated Cooling and Misting System for my propagation table using an ESP32 microcontroller, a temperature sensor, an RTC, a solar panel, and a 12V battery. This system mists the plants based on time and temperature: it activates between 5 AM and 8 PM, misting for 7 seconds every 5 minutes if the temperature is above 24°C, and for 8 seconds every 2 minutes if above 27°C. It’s fully automated, eco-friendly, and ensures your plants stay cool without any manual intervention. Check out my YouTube video to see it in action and learn how to build your own!

The whole project in one video

I tried to document as much as I could, I think I had enough to create a glimpse to each step that was put into this video.

For those who wish to follow

Components

  • Arduino IDE – download here 
  • ESP32 dev board
  • Breadboard
  • USB – mini communication wire
  • 4.7K ohm resistor
  • DS18B20 temperature sensor
  • Solar panel (with inverter)
  • 12v battery (I use 7amp/h but it can be less)
  • Step down to 5V with USB output
  • RTC (real time clock) DS3231
  • Water valve with solenoid

Wiring

Wiring information

Here is the code

Arduino
				#include "RTClib.h"
#include <WiFi.h>
#include <esp_sleep.h>
#include <OneWire.h>
#include <DallasTemperature.h>

RTC_DS3231 rtc;
const int relayPin = 23;
const int oneWireBus = 4; // Change to the GPIO pin you connected the DS18B20's data line to

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW); // Ensure relay is off

  sensors.begin(); // Start up the DS18B20 sensor

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC, proceeding without it...");
    return;
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  sensors.requestTemperatures(); // Send the command to get temperatures
  float temperature = sensors.getTempCByIndex(0); // Read temperature from the sensor in Celsius
  Serial.print("Temperature: ");
  Serial.println(temperature);
  
  DateTime now = rtc.now();
  Serial.println(String("DateTime::TIMESTAMP_FULL:\t") + now.timestamp(DateTime::TIMESTAMP_FULL));
  Serial.println(String("DateTime::TIMESTAMP_DATE:\t") + now.timestamp(DateTime::TIMESTAMP_DATE));
  Serial.println(String("DateTime::TIMESTAMP_TIME:\t") + now.timestamp(DateTime::TIMESTAMP_TIME));
  Serial.println(temperature);
  Serial.println("\n");

  bool isDayTime = now.hour() >= 5 && now.hour() < 20;
  bool isHotEnough = temperature > 24.0;
  bool isSoHot = temperature > 27.0;

  // Handle relay logic to avoid running twice in a row
  if (isDayTime && isSoHot) {
    digitalWrite(relayPin, HIGH); // Turn on relay
    delay(8000); // Keep relay on for 8 seconds
    digitalWrite(relayPin, LOW); // Turn off relay
    esp_sleep_enable_timer_wakeup(2 * 60 * 1000000ULL); // 2 minutes
  } else if (isDayTime && isHotEnough) {
    digitalWrite(relayPin, HIGH); // Turn on relay
    delay(7000); // Keep relay on for 7 seconds
    digitalWrite(relayPin, LOW); // Turn off relay
    esp_sleep_enable_timer_wakeup(5 * 60 * 1000000ULL); // 5 minutes
  } else {
    // Default deep sleep duration if no conditions are met
    esp_sleep_enable_timer_wakeup(10 * 60 * 1000000ULL); // 10 minutes
  }

  // Enter deep sleep mode
  esp_deep_sleep_start();
}

void loop() {
  // This is not used for this example.
}