![]() |
photo by Kaufmann Mercantile |
It always happens that whenever I go for any vacation, my plants die. Because They don’t get proper care, sunlight, and water. So to avoid that I have created this DIY project which allows me to check my plant's moisture status, even when I am not at home, and also automate watering the plant.
In this project, I am using a moisture sensor that captures soil moisture after every 1 sec and sends it to ESP8266 nodeMCU. I am also using the real-time database of firebase to store this data and get the latest data to compare moisture value with a threshold value. If the moisture value is less than the threshold value, esp8266 node MCU starts the water pump for 5 sec and if the value is higher than the threshold value, it does nothing.
Implementing this DIY project, cost me 500-700 bucks. Below I have mentioned the components required for this project. All these components are available on amazon.
Required Component
![]() |
ESP8266 e-12 |
ESP8266 e-12 NodeMCu development board is very inexpensive system-on-chip (SOC). It has CPU, RAM, and Wifi modules. Pins information of ESP8266 nodeMCU are mentioned below:
Pin | Description |
---|---|
A0 | Analog |
RSV | Reserved |
SD0/SD1/SD2/SD3 | SDIO Data |
CMD | SDIO CMD |
CLK | Clock |
GND | Ground |
3V3 | 3.3V |
EN | Chip Enable |
RST | Reset |
VIN | 3.3V Regulator |
TXD | UART TXD |
RXD | UART RXD |
D0-D8 | GPIO pins |
Choose a water pump according to the distance between the water source and plants. I have used a 3-6V water pump because I have kept both water source and plant side by side. I am using a 9V battery, to power the water pump.
Wiring
Connect the -ve pin of the battery to the -ve pin of the water pump and connect the +ve pin of the battery to the middle pin of the relay and +ve connection of the water pump to the open pin of the relay.
5V single channel Relay |
Relay is an electro-mechanical component that acts as a switch. On the left-hand side, it has an output terminal, which is connected to AC/DC load. There are 3 pins in the o/p terminal
NC (normal close) - This pin is connected to create a close circuit and its connection break when the signal is sent from the relay signal pin.
COM(Common) pin - This is generally connected with AC/DC load.
NO(normal open) - this pin is generally open and it becomes close when the signal is sent from the relay signal pin
In i/p terminal, which is on the right-hand side has 3 pins - GND, Vcc, and digital pin. In this project, I have connected these pins with ESP8266 GND, 3V3, and D1 pins.
5V Relay | ESP8266 NodeMCU |
---|---|
GND | GND |
Vcc | 3V3 |
inpt 1 | D1 |
![]() |
3V moisture sensor |
Moisture Sensor | ESP8266 NodeMCU |
---|---|
GND | GND |
Vcc | 3V3 |
A0 | A0 |
D0 |
the following formula is used to calculate soil moisture in percentage:
moisture_value = (100- (analogRead(input_sensor)/1023.00)*100);
![]() |
male and female jumper wires |
You can also use wires instead of jumper wires. But with help of jumper wires, it is very easy to connect with all components.
Schematic Diagram & Connection
Schematic diagram of esp8266 connection with moisture sensor and water pump |
Firebase Real-time database
These are the following steps to set up a real-time database in firebase:
1. Sign in to firebase and click on go to the console.
4. Disable google analytics
7. Define security rules for your data. For this project, I have selected locked mode and defined read & write condition true
FIREBASE_AUTH key
To access the firebase real-time database in the project requires the FIREBASE_AUTH key. Following are steps to access it:1. Click on the setting icon on the left-hand side and select project-setting.
Setup Arduino-Firebase library in Arduino IDE
To make an API call for getting and sending data to the firebase real-time database from ESP8266 nodeMCU, I am using 3rd party library - FirebaseArduino. Download this git repo library as a zip file and add in Arduino IDE:
Coding
Copy the following code and execute this in Arduino IDE and upload it in esp8266 nodeMCU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <FirebaseArduino.h> //added Firebase Arduino to access realtime database | |
#include <ArduinoJson.h> //required for get and set api call | |
#define FIREBASE_HOST " db link" | |
#define FIREBASE_AUTH "secret key" | |
#define WIFI_SSID "wifi username" | |
#define WIFI_PASSWORD "password" | |
#define relay D1 | |
//const int relay = 1; | |
const int input_sensor = A0; //analog pin | |
void setup() { | |
Serial.begin(9600); //define baud rate for serial communication | |
delay(1000); | |
pinMode(relay,OUTPUT); | |
WiFi.begin(WIFI_SSID,WIFI_PASSWORD); | |
while(WiFi.status()!=WL_CONNECTED){ | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println("Connected"); | |
Serial.print("IP Address"); | |
Serial.println(WiFi.localIP()); | |
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH); | |
Firebase.set("/plant1/humidity", 0); | |
digitalWrite(relay,HIGH); | |
} | |
void loop() { | |
float moisture_value; | |
moisture_value = (100- (analogRead(input_sensor)/1023.00)*100); | |
Serial.println("Soil moisture is - "); | |
Serial.print(moisture_value); | |
Serial.println(" %"); | |
delay(1000); | |
String value = String(moisture_value); | |
Firebase.setFloat("/plant1/humidity",moisture_value); | |
if (Firebase.failed()) | |
{ | |
Serial.print("firebase2 failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(2000); | |
float plant1 = Firebase.getFloat("/plant1/humidity"); | |
Serial.print("plant1 humidit: "); | |
Serial.println(plant1); | |
if( WiFi.status()== WL_CONNECTED && plant1 >0 && plant1 < 35) | |
{ | |
digitalWrite(relay,LOW); | |
Firebase.setInt("/plant1/pump",1); | |
delay(1000); | |
Serial.print("pump is high "); | |
digitalWrite(relay,HIGH); | |
}else | |
{ | |
digitalWrite(relay,HIGH); | |
Firebase.setInt("/plant1/pump",0); | |
Serial.print("pump is low "); | |
} | |
if (Firebase.failed()) | |
{ | |
Serial.print("firebase3 failed:"); | |
Serial.println(Firebase.error()); | |
digitalWrite(relay,HIGH); | |
return; | |
} | |
} |