Control Your Water Pump Remotely Using an Android App with Soil Moisture Sensor & Firebase Integration

ESP32 Firebase

Welcome to Yarana IoT Guru! πŸŒ±πŸš€

Are you looking for an efficient way to automate your irrigation system and remotely control your water pump? With IoT-based smart irrigation, you can:
βœ… Monitor soil moisture in real-time
βœ… Control your water pump remotely using an Android app
βœ… Store and retrieve live data using Firebase

This project is ideal for smart farming, home gardens, and IoT enthusiasts who want to save water while keeping their plants healthy. 🌾🌻

πŸ‘‰ πŸ“₯ Download Complete Source Code:
πŸ”— GitHub Repository


🎯 What You Will Learn in This Project

βœ”οΈ Connecting a soil moisture sensor to your Android app
βœ”οΈ Using Firebase for real-time data storage and retrieval
βœ”οΈ Controlling a water pump using a relay module
βœ”οΈ Building an Android app interface for easy control & monitoring


πŸ›  Components & Technologies Used

βœ… Soil Moisture Sensor – Measures soil moisture levels
βœ… Relay Module – Controls the water pump
βœ… ESP32 / ESP8266 – Connects the sensor to Firebase
βœ… Firebase Realtime Database – Stores sensor readings
βœ… Android App (Kotlin/Java) – Displays real-time data & controls the pump
βœ… WiFi Network – Required for remote access


πŸ“Œ Why Build a Smart Irrigation System?

Traditional irrigation methods waste water and require manual effort. An automated smart irrigation system ensures:
πŸ”Ή Optimal water usage – No over-watering or under-watering
πŸ”Ή Remote control – Turn the pump on/off from anywhere
πŸ”Ή Data-driven decisions – Adjust watering based on real-time soil moisture levels
πŸ”Ή Increased crop yield – Smart irrigation leads to better plant health


πŸš€ Step-by-Step Guide to Building a Smart Irrigation System

Step 1: Setting Up the Soil Moisture Sensor

  1. Connect the soil moisture sensor to the ESP32 or ESP8266.
  2. Use analog input pins to read the sensor values.
  3. The sensor provides a voltage output depending on the soil moisture level:
    • Dry soil β†’ High resistance β†’ Low voltage output
    • Wet soil β†’ Low resistance β†’ High voltage output

πŸ“₯ Example Code to Read Moisture Sensor Data

cppCopyEdit#define SENSOR_PIN A0  // Connect the sensor to the Analog pin

void setup() {
    Serial.begin(115200);
    pinMode(SENSOR_PIN, INPUT);
}

void loop() {
    int moistureValue = analogRead(SENSOR_PIN);
    Serial.print("Soil Moisture Level: ");
    Serial.println(moistureValue);
    delay(1000);  // Read every second
}

Step 2: Sending Data to Firebase

Now, we send the soil moisture data to Firebase so that the Android app can retrieve it.

πŸ“₯ ESP32 Code to Send Data to Firebase

cppCopyEdit#include <WiFi.h>
#include <FirebaseESP32.h>

#define FIREBASE_HOST "your-firebase-url.firebaseio.com"
#define FIREBASE_AUTH "your-firebase-secret"
#define SENSOR_PIN A0  

FirebaseData firebaseData;
WiFiClient client;

void setup() {
    Serial.begin(115200);
    WiFi.begin("your-SSID", "your-PASSWORD");
    
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
    }
    
    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

void loop() {
    int moistureValue = analogRead(SENSOR_PIN);
    
    Firebase.setInt(firebaseData, "/soilMoisture", moistureValue);
    Serial.println("Soil Moisture Data Sent to Firebase: " + String(moistureValue));

    delay(5000); // Send data every 5 seconds
}

Step 3: Controlling the Water Pump Using a Relay

Once we receive moisture data on Firebase, we can use it to turn the water pump ON/OFF automatically.

πŸ“₯ Code to Control the Relay

cppCopyEdit#define RELAY_PIN 5  

void setup() {
    pinMode(RELAY_PIN, OUTPUT);
}

void loop() {
    int moistureValue = analogRead(SENSOR_PIN);
    
    if (moistureValue < 300) {  // If soil is dry
        digitalWrite(RELAY_PIN, HIGH); // Turn ON pump
    } else {
        digitalWrite(RELAY_PIN, LOW);  // Turn OFF pump
    }
}

Step 4: Building an Android App to Display Data & Control Pump

Now, we need an Android app to:
βœ… Display real-time moisture levels
βœ… Turn the pump ON/OFF manually

πŸ“₯ Steps to Build the Android App

1️⃣ Use Firebase Realtime Database to read sensor data.
2️⃣ Create a user-friendly UI using Kotlin or Java.
3️⃣ Add a manual ON/OFF button to control the pump remotely.

πŸ“₯ Android Code to Read Firebase Data

javaCopyEditDatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference("soilMoisture");

databaseRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int moistureLevel = dataSnapshot.getValue(Integer.class);
        moistureTextView.setText("Moisture: " + moistureLevel);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w("Firebase", "Failed to read value.", databaseError.toException());
    }
});

Step 5: Testing & Demonstration

1️⃣ Upload the ESP32 code and connect it to Firebase.
2️⃣ Run the Android app and check if soil moisture data is displayed.
3️⃣ Test the water pump by making the soil dry/wet.
4️⃣ Verify remote control functionality from the app.

βœ… Congratulations! You have successfully built a smart irrigation system that saves water and automates plant watering. πŸš€


πŸ“Ί Watch the Full Video Tutorial

πŸ”΄ Smart Irrigation System Video on Yarana IoT Guru

πŸ“’ Check out our GitHub Repo for the full source code!
πŸ‘‰ Download Code


πŸ“Œ Frequently Asked Questions (FAQs)

❓ Can I use this system for large farms?
βœ… Yes! You can scale it by adding more sensors and pumps.

❓ What if my WiFi goes down?
βœ… You can set up offline mode where the ESP32 runs without Firebase.

❓ Can I modify the app?
βœ… Yes! You can customize the UI and add features like notifications.


πŸš€ Final Thoughts

This IoT-based smart irrigation system is a game-changer for farming and gardening. It helps you:
βœ… Save water
βœ… Improve plant health
βœ… Control irrigation remotely

Try this project today and take your farming automation to the next level! πŸš€

Leave a Reply

Your email address will not be published. Required fields are marked *