Article
Build a Smart Water Leak Detector with ESP8266
💧
A water leak under a sink, behind a washing machine, or near a water heater usually goes unnoticed until someone finds a wet floor or a stained ceiling downstairs — by then the damage is already significant. A cheap water leak sensor, paired with an ESP8266, can catch the very first drop and send an instant alert to your phone, even when nobody’s home.
How it works
- The water leak sensor is a set of two parallel exposed traces (similar to a soil moisture sensor, but laid flat on the floor) — when dry, the two traces are fully insulated from each other; once water bridges them, it conducts weakly, closing the circuit and sharply dropping the measured resistance.
- The ESP8266 continuously reads a digital or analog signal from the sensor; once the value crosses a detection threshold, it treats that as a leak.
- To avoid false alarms from momentary noise, the system requires the wet signal to stay consistent across several consecutive readings before confirming a real alert.
- Once a leak is confirmed, the ESP8266 sends an urgent message via the Telegram Bot API including the installation location (e.g. “Water leak: under the kitchen sink”), and can also trigger a local buzzer alarm so people in the house hear it immediately.
Parts list
- An ESP8266 (NodeMCU or Wemos D1 Mini, with built-in WiFi)
- A water leak sensor module (a cheap, easy-to-find open-trace pair sensor)
- A small buzzer (optional) for local alarming
- A 5V power supply (a small USB adapter near an outlet at the install site)
- A cable long enough to place the sensor at the lowest point (floor level, under a pipe) while keeping the board mounted higher, away from moisture
Sample code
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
const char* WIFI_SSID = "your-wifi";
const char* WIFI_PASS = "your-wifi-password";
const char* BOT_TOKEN = "TELEGRAM_BOT_TOKEN";
const char* CHAT_ID = "TELEGRAM_CHAT_ID";
const char* LOCATION_NAME = "Under the kitchen sink";
const int SENSOR_PIN = A0;
const int BUZZER_PIN = D5;
const int WATER_THRESHOLD = 500; // tune to your actual sensor, lower value means wetter
WiFiClientSecure secureClient;
UniversalTelegramBot bot(BOT_TOKEN, secureClient);
bool leakActive = false;
int consecutiveWetReads = 0;
const int CONFIRM_READS = 3; // requires 3 consecutive readings to confirm, avoids false alarms
void setup() {
Serial.begin(115200);
pinMode(BUZZER_PIN, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(500);
secureClient.setInsecure();
bot.sendMessage(CHAT_ID, "Water leak sensor at " + String(LOCATION_NAME) + " is ready.", "");
}
void loop() {
int reading = analogRead(SENSOR_PIN);
bool wetNow = reading < WATER_THRESHOLD;
if (wetNow) {
consecutiveWetReads++;
} else {
consecutiveWetReads = 0;
}
if (consecutiveWetReads >= CONFIRM_READS && !leakActive) {
leakActive = true;
digitalWrite(BUZZER_PIN, HIGH);
bot.sendMessage(CHAT_ID, "WATER LEAK ALERT: " + String(LOCATION_NAME), "");
Serial.println("Leak confirmed, alert sent!");
} else if (!wetNow && leakActive) {
leakActive = false;
digitalWrite(BUZZER_PIN, LOW);
bot.sendMessage(CHAT_ID, "Leak cleared (or dried up): " + String(LOCATION_NAME), "");
}
delay(2000); // check every 2 seconds, fast enough for early detection
}
Common pitfalls
- Mounting the ESP8266 board too low, right near the floor risks the board itself getting damaged once water spreads — only the sensor should sit at floor level; mount the main board higher, away from where water could reach.
- No confirmation across multiple readings (
CONFIRM_READS) causes false alarms from momentary humidity or dust on the sensor — always require the signal to stay consistent across several consecutive readings. - Bare metal sensor traces oxidize over time, gradually reducing sensitivity — choose a sensor with an anti-oxidation coating, or check it periodically, especially in consistently damp spots.
- A single sensor covering the whole house misses leaks elsewhere — for homes with multiple risk spots (kitchen, bathroom, washing machine, water heater), install several independent units, each labeled with its own location in the alert message.
Extensions
Integrate with an electronic solenoid valve to automatically shut off the main water supply the moment a leak is detected, check out the smart mailbox notifier with ESP8266 project for another take on sending Telegram alerts from home sensors, or publish state over MQTT to show it on a shared Home Assistant dashboard.
Tags:#esp8266#sensors#safety
Comments