DEV Community

Himanshu Dada
Himanshu Dada

Posted on

Building a Smart Water Tank Monitoring System with ESP32, MQTT, and Real-Time Alerts

 Water tanks are everywhere — on rooftops, in basements, at industrial sites. And yet, most of them are still dumb. Nobody knows the water level until it’s too late. Either the pump runs dry, or the tank overflows and floods the area.

We’ve built dozens of monitoring systems for clients across India, the US, and the UK. The pattern is always the same: someone needs to know the water level remotely, get alerts when something goes wrong, and ideally control the pump without walking up three flights of stairs.

Here’s how we design a smart water tank monitoring system using an ESP32, ultrasonic sensing, MQTT, and real-time alerts. No fluff, just the engineering.

The Hardware Stack

Microcontroller: ESP32-WROOM-32. Dual-core, built-in WiFi and Bluetooth, plenty of GPIOs. We’ve used this chip in over 40 projects. It’s our go-to for IoT applications that need reliable wireless connectivity.

Sensor: JSN-SR04T waterproof ultrasonic sensor. Unlike the cheap HC-SR04 modules, this one has an IP67-rated transducer. It sits inside the tank, pointing downward, and measures the distance to the water surface. Accuracy is ±2mm at ranges up to 4.5 meters. Good enough for most residential and light industrial tanks.

Power: We use a 3.7V 18650 lithium cell with a TP4056 charging module. The ESP32 in deep sleep draws about 10µA. With a 3000mAh cell, the system runs for months between charges. For always-on installations, we add a Hi-Link HLK-PM01 AC-DC converter.

PCB: Custom 2-layer board designed in KiCad. We keep it simple — no unnecessary components. The board measures 50x35mm, fits inside a standard junction box.

Firmware Architecture

Our firmware runs on ESP-IDF v5.1. No Arduino core for production systems — we need deterministic timing and lower power consumption.

The state machine has three modes:

1. Deep Sleep (default): ESP32 sleeps for 30 minutes. RTC memory holds the last reading and alert state.
2. Measurement: Wakes up, powers the sensor via GPIO, takes 5 readings, discards outliers, averages the rest.
3. Transmit: Connects to WiFi, publishes data via MQTT, checks for any pending alerts, then goes back to sleep.

Total active time: about 8 seconds. Power consumption during active mode is 80mA. Over a day, the average draw is under 0.5mA.

MQTT Communication

We use MQTT because it’s lightweight, reliable, and works over unreliable networks. The broker runs on a Raspberry Pi 4 at the client site, or we use AWS IoT Core for cloud-connected deployments.

Topics we use:

  1. tank/1/level — publishes water level as percentage (0-100)
  2. tank/1/distance — raw distance in centimeters
  3. tank/1/status — sends "normal", "low", "critical", "overflow"
  4. tank/1/pump — subscribe to control pump (0 = off, 1 = on)
  5. tank/1/alert — publishes alert messages

QoS: We use QoS 1 for level data. For alerts, QoS 2 ensures delivery even if the network drops temporarily.

Real-Time Alerts

This is where the system earns its keep. We set three thresholds:

1. Low (25%): Sends a push notification via Telegram bot. “Tank 1 at 24%. Refill within 6 hours at current usage.”
2. Critical (10%): Telegram + email via SMTP. If a pump controller is connected, we auto-shutoff to prevent dry running.
3. Overflow (95%): Immediate alert. If the pump is on, we turn it off.

We use Telegram because it’s free, simple, and works everywhere. The ESP32 posts directly to https://api.telegram.org/bot<TOKEN>/sendMessage. No need for a separate server.

For clients who want SMS, we integrate Twilio. Costs about $0.0079 per message. Worth it for critical alerts.

Example: Customizable Vending Machines

Here’s a real use case. One of our clients runs a chain of customizable vending machines across Bangalore. Each machine has a small water tank for the beverage dispensing system. Before our system, a technician drove to each location every two days to check water levels. That’s 12 locations, 24 hours of driving per week.

We installed our ESP32 monitoring system in each machine. The dashboard shows real-time water levels for all 12 machines on a single screen. Alerts go to the operations team’s Telegram group. Now they only visit when a tank is below 30%. Driving time dropped to 4 hours per week. The client saved ₹48,000 per month in labor alone.

The same hardware works for personalized vending machines that mix custom drink recipes. Each machine has multiple ingredient tanks. Our system monitors all of them independently. When a syrup tank runs low, the machine automatically switches to a backup and alerts the restocking team. No downtime, no missed sales.

PCB Design Considerations

We learned a few things the hard way:

1. Sensor cable length: The JSN-SR04T works reliably up to 20 meters of cable. Beyond that, signal integrity degrades. We add a 100nF capacitor at the sensor end for longer runs.
2. Ground loops: In metal tanks, the sensor ground can create a ground loop through the tank wall. We isolate the sensor with a 10kΩ resistor on the trigger pin.
3. Antenna placement: The ESP32’s PCB antenna is sensitive. We mount the board so the antenna faces away from the metal tank. If that’s not possible, we use an external antenna with an IPEX connector.

Deployment and Shipping

We ship fully tested prototypes to clients within 10 working days. The package includes:

  1. Assembled and programmed ESP32 board
  2. Waterproof ultrasonic sensor with 5m cable
  3. Power supply (battery or AC-DC)
  4. Junction box with cable glands
  5. QR code linking to the setup guide

For production runs, we work with a PCB assembly partner in Shenzhen. Lead time is 3 weeks for 1000 units. We handle firmware updates over-the-air using our custom OTA server.

Final Thoughts

Building a smart water tank monitor isn’t complicated. The ESP32, an ultrasonic sensor, and MQTT give you everything you need. The hard part is making it reliable in the real world — handling WiFi disconnections, sensor drift, power failures. We’ve been doing this for 8 years. We know the edge cases.

If you’re building something similar, or you want a system tailored to your specific tank setup, reach out. We design, prototype, and ship. No hand-holding required.

Top comments (0)