Tech Gyan Set
Published © MIT

IoT-Based Smart Stray Animal Water Feeder System

An IoT-based smart water feeder that automatically refills and monitors water for stray animals. πŸΆπŸ’§

BeginnerFull instructions provided7 hours71
IoT-Based Smart Stray Animal Water Feeder System

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

🐢 Smart Stray Animal Water Feeder βœ… Complete ESP32 Code

C/C++
πŸ” How This Code Works

1️⃣ Ultrasonic sensor tank ka distance measure karta hai
2️⃣ Distance zyada = water level low
3️⃣ Distance kam = tank full
4️⃣ Low level detect hone par relay ON β†’ Pump start
5️⃣ Full level detect hone par relay OFF β†’ Pump stop
6️⃣ MQTT par water level + pump status publish hota hai
#include <WiFi.h>
#include <PubSubClient.h>

// ----------------------
// Pin Configuration
// ----------------------
#define TRIG_PIN 5
#define ECHO_PIN 18
#define RELAY_PIN 4

// ----------------------
// WiFi Credentials
// ----------------------
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

// ----------------------
// MQTT Configuration
// ----------------------
const char* mqtt_server = "broker.hivemq.com";
const char* topic_level = "animalfeeder/waterlevel";
const char* topic_status = "animalfeeder/pumpstatus";

WiFiClient espClient;
PubSubClient client(espClient);

// ----------------------
// Water Level Threshold (in cm)
// ----------------------
float tankHeight = 20.0;       // Total tank height
float lowLevelThreshold = 12.0; // When to start pump
float fullLevelThreshold = 5.0; // When to stop pump

// ----------------------
// Setup Function
// ----------------------
void setup() {
  Serial.begin(115200);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);

  digitalWrite(RELAY_PIN, HIGH);  // Relay OFF initially (active LOW)

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nWiFi Connected");

  client.setServer(mqtt_server, 1883);
}

// ----------------------
// MQTT Reconnect
// ----------------------
void reconnect() {
  while (!client.connected()) {
    client.connect("AnimalWaterFeeder");
  }
}

// ----------------------
// Ultrasonic Distance Read Function
// ----------------------
float getDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH);
  float distance = duration * 0.034 / 2;

  return distance;
}

// ----------------------
// Main Loop
// ----------------------
void loop() {

  if (!client.connected()) {
    reconnect();
  }

  client.loop();

  float distance = getDistance();

  Serial.print("Water Level Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  client.publish(topic_level, String(distance).c_str());

  // ----------------------
  // Pump Logic
  // ----------------------

  if (distance > lowLevelThreshold) {
    // Water Low β†’ Pump ON
    digitalWrite(RELAY_PIN, LOW);
    Serial.println("Pump ON");
    client.publish(topic_status, "Pump ON");
  }

  if (distance <= fullLevelThreshold) {
    // Tank Full β†’ Pump OFF
    digitalWrite(RELAY_PIN, HIGH);
    Serial.println("Pump OFF");
    client.publish(topic_status, "Pump OFF");
  }

  delay(3000);
}

Credits

Tech Gyan Set
32 projects β€’ 14 followers
Tech Gyan Set | IoT & Embedded Systems Creator | Arduino, ESP32, GSM & NodeMCU Projects | Smart Home & Real-Life Automation Tutorials
Thanks to Tech Gyan Set .

Comments