Introduction: ESP8266 Water Leak Detector MQTT Homie

In this tutorial I will show how to build ESP8266 WiFi water leak sensor with ESP8266, Arduino and OpenHab server. Typical application for this sensor would be putting underneath the kitchen sink or near the water heater to detect potential water leak. This is a rather simple setup to get everything accomplished over MQTT thanks to the power of Homie Framework with only a few lines of code.

Step 1: Step 1: Materials

List of materials is fairly simple. This also takes into account that you have OpenHAB 2.0 running along with Mosquitto Server and you have an account setup on OpenhabCloud for the notifications.

Step 2: Step 2: Wiring

The wiring schematic is fairly simple.

  • White Wire to 3.3v on Wemos D1 mini
  • Red Wire to D1 on Wemos D1 mini
  • 3.3k resistor from D1 to Ground

Step 3: Step 3: Programming

The only dependency you will have is Homie 2.0 Library loaded on your Arduino.

#include <Homie.h>

int inPin = D1;         // the number of the input pin
int debounce_count = 100; // make sure we have 100 straight readings before reporting a leak
int counter = 0; 
int reading;          
int current_state = LOW;    

int no_leak_counter = 0;

long timekeep = 0;         
HomieNode waterLeakNode("water-leak", "water-leak");

void loopHandler() {
  if (millis() != timekeep) {
    reading = digitalRead(inPin);
    if (reading == current_state && counter > 0) {
    if (reading != current_state) {
    if(counter >= debounce_count) {
      counter = 0;
      no_leak_counter = 0;
      current_state = reading;
      Serial.println("WATER LEAK");
      Homie.setNodeProperty(waterLeakNode, "status", String("true"), true);
    } else {
    if (no_leak_counter > 100000) {
      no_leak_counter = 0;
      Serial.println("NO LEAK");
      Homie.setNodeProperty(waterLeakNode, "status", String("false"), true);
    timekeep = millis();

void setupHandler() {
  pinMode(inPin, INPUT);
  Homie.setNodeProperty(waterLeakNode, "status", String("false"), true);

void setup() {
  Homie.setFirmware("water-leak-monitor", "1.0.0");

void loop() {



String kitchen_leak "Kitchen Leak Status [MAP(]" (gF_Kitchen) {mqtt="<[OpenHab:devices/kitchen-water-leak-sensor/water-leak/status:state:default]"}


false=OK -=Unable to Detect


rule "notify KitchenLeak" // send notification of node state change
when Item kitchen_leak changed then if(kitchen_leak.state == "true") { sendNotification("YOUR OPENHABCLOUD EMAIL", "Kitchen Leak Detected") } end

Homie Configuration File

  "name": "Kitchen Leak Sensor",
  "device_id": "kitchen-water-leak-sensor",
  "wifi": {
    "ssid": "MYSSDID",
    "password": "MYWIFIPASSWORD"
  "mqtt": {
    "host": "MY MQTT SERVER",
    "port": 1883,
    "base_topic": "devices/",
    "auth": true,
    "username": "sensor",
    "password": "sensor1"
  "ota": {
    "enabled": true

Please read the documentation on setting your homie device at homie documentation and how to load the configuration file.

Step 4: Final

That should be working nicely, give it a try and let me know what you think of everything. If you did everything correctly, you will receive a notification on your mobile device whenever it detects a leak.

Thanks for reading :)