In the age of the Internet of Things (IoT), even our potted plants can benefit from smart technology. The Smart Plant Pot with ESP32 is an ingenious solution that helps both novice and experienced gardeners cultivate healthier and more vibrant plants. In this article, we’ll delve into the concept and working principles that are essential to create a Smart Plant Pot using an ESP32 microcontroller and reading the enviroment variables like temperature and humidity around it and displaying the data using a local server created by Raspberry Pi.
Introduction
With the increasing popularity of urban living and limited gardening space, smart plant pots have become a go-to solution for plant enthusiasts. These devices offer real-time plant health monitoring, automated watering, and the convenience of remote management. By combining the power of the ESP32 microcontroller and IoT connectivity, you can create a Smart Plant Pot that ensures your leafy friends thrive in any environment.
Working Principle
The Smart Plant Pot operates by combining sensors and microcontrollers with a connection to the internet. Key components include moisture sensors, temperature sensors, and the ESP32 microcontroller. These sensors provide real-time data on soil moisture levels, ambient temperature, and other environmental conditions. The ESP32 processes this data and communicates with a cloud platform or a dedicated mobile app (In our case we will be communicating with a local server for now). Users can remotely monitor and control their plant’s well-being and receive alerts when intervention is needed. For remote data access click here and understand the usage of Firebase for realtime data.
Things We Need
To build a Smart Plant Pot with an ESP32, you will need the following components:
- ESP32 Development Board: The central microcontroller for data processing and internet connectivity.
- Moisture Sensor: Measures soil moisture levels.
- Temperature and Humidity Sensor (e.g., DHT11 or DHT22): Monitors the ambient conditions around the plant.
- Water Pump or Solenoid Valve: For automated watering.
- Relay Module: Controls the water pump or solenoid valve.
- Wi-Fi Router: For Internet / Local Network connectivity. (Internet is not needed for this project to function. We will need internet only during the installation of Flask on the RPi)
- Power Supply: Provides power to the system. (240v AC depending on your water pump)
- Circuit Connections: Wires, breadboard, and other accessories for assembling the circuit.
- Raspberry Pi, Windows Computer : (if you want to read the data via a server)
Circuit Diagram
The circuit diagram showcases the connections between the ESP32, sensors, water pump (or solenoid valve), and other components. It’s important to ensure proper wiring and connections to guarantee the efficient operation of your Smart Plant Pot.
The project will still work without a server. But that beats the main purpose of an IoT Project, hence please create a RPi Server using Flask or if you do not have a RPi : then please create and Flask server on Windows/Mac.
Suggested Reading
- How to Create a Flask Server with Python using a RPi.
- How to Create a Flask Server using Python using Windows
Working of the System with Arduino
The Smart Plant Pot operates as follows:
- Sensors Data Collection: The moisture sensor and temperature/humidity sensor continually collect data from the soil and ambient environment.
- Data Processing: The ESP32 microcontroller processes this data, checking if the plant requires watering or if the environmental conditions are within optimal ranges.
- Automated Watering: If the soil moisture falls below the desired level, the microcontroller activates the water pump (or solenoid valve) to irrigate the plant.
- Data Transmission: The ESP32 sends the collected data, including moisture levels and temperature, to a Server or Realtime Database.
- Remote Monitoring: Users can access this data remotely through a smartphone app or a web-based interface. They receive alerts when conditions fall outside predefined parameters. (If you want to create this full application please contact me via mail)
- User Intervention: Based on the real-time data and alerts, users can take actions such as adjusting watering schedules or monitoring plant health more closely. (If you want to create this full application please contact me via mail)
Code
While the code for a Smart Plant Pot with ESP32 can be extensive and highly customized, here’s a simplified example of code for monitoring soil moisture and transmitting data to a Local Server. Keep in mind that you’ll need to adapt and expand upon this code to integrate additional sensors and functionality:
The Code for the ESP32 should be uploaded on the ESP32
The main.py and the index.html file should be kept in one directory as shown in the video. The video represents only the file structure. If you are new to the server environment, please read the suggested reading from the here.
After the files are stored in the given structure. Start the server by running the main.py file.
#include <Arduino.h>
#include <WiFi.h>
#include <DHT.h>
const char* ssid = "ES"; // Replace with your WiFi SSID
const char* password = "12345678"; // Replace with your WiFi Password
const char* host = "192.168.100.8"; // Replace with your Servers IP address
#define DHT_SENSOR_TYPE DHT11 // Choose DHT11 or DHT22 based on what you are using
const int moisturePin = 34; // Analog pin for soil moisture sensor
const int DHTPin = 4; // GPIO4 for DHT22 or DBT11 sensor
const int pumpPin = 35; // GPIO2 for controlling the pump
const int pumpThreshold = 500; // Adjust this threshold based on your soil moisture sensor
const int wateringDuration = 5000; // Adjust watering duration as needed (in milliseconds)
DHT dht_sensor(DHTPin, DHT_SENSOR_TYPE);
void setup() {
dht_sensor.begin();
Serial.begin(115200);
delay(10);
pinMode(pumpPin, OUTPUT);
dht_sensor.begin();
connectToWiFi();
}
void loop() {
float humidity = dht_sensor.readHumidity();
Serial.print("Humidity : ");
Serial.println(humidity);
float temperature = dht_sensor.readTemperature();
Serial.print("Temperature : ");
Serial.println(temperature);
int moistureValue = analogRead(moisturePin);
Serial.print("Moisture : ");
Serial.println(moistureValue);
sendDataToServer(humidity,temperature);
// Control the pump based on moisture level
if (moistureValue < pumpThreshold) {
turnOnPump();
delay(wateringDuration);
turnOffPump();
Serial.println("Pump turned ON for watering");
} else {
Serial.println("Soil moisture level is sufficient, no watering needed");
}
// Send data to the cloud platform
//sendToCloudPlatform(humidity, temperature, moistureValue);
delay(1000); // Send data every 10 minutes
}
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
}
/*
void sendToCloudPlatform(float humidity, float temperature, int moistureValue) {
// HTTP POST request to send data to the cloud platform
}
*/
void turnOnPump() {
digitalWrite(pumpPin, HIGH); // Turn the pump ON
}
void turnOffPump() {
digitalWrite(pumpPin, LOW); // Turn the pump OFF
}
void sendDataToServer(float humidity, float temperature) {
WiFiClient client;
if (!client.connect(host, 3234)) {
Serial.println("Connection to server failed");
return;
}
String url = "/update?humidity=" + String(humidity) + "&temperature="+ String(temperature);
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
Serial.println("Request sent to server");
}
Conclusion
A Smart Plant Pot with ESP32 is a remarkable example of how technology can enhance our daily lives and promote sustainable, healthy living. With this system, you can ensure your plants receive the right amount of water and are grown in optimal environmental conditions. By customizing and expanding upon this project, you can create a highly sophisticated and efficient solution for nurturing your indoor or outdoor garden. Whether you’re a seasoned gardener or just starting your green journey, a Smart Plant Pot with ESP32 is an invaluable tool in your gardening arsenal.