Introduction: Ultra-low Power Basement Flooding Alarm With ESP8266

About: Hobbyist

Hello, welcome to my first instructable.

The basement of my house gets flooded in every few years for various reasons like heavy summer thunderstorms, high groundwater or even a pipe bursting. Although it is not a nice place, but my central-heating boiler is located down there and water can harm its electronic parts, so I need to pump out the water as soon as possible. It is difficult and uncomfortable to check the situation after a heavy summer thunderstorm, so I decided to make an ESP8266 based alarm which sends me an e-mail in case of a flooding. (When the flooding is caused by high groundwater the water level is usually less than 10 centimetres which is not harmful to the heater and it is not recommended to pump out because it will come back anyway and the more you pump, the more groundwater will come next time. But it is good to know about the situation.)

In this application the device might be in "sleep" for years, and if everything works as planned, only operates for a few seconds. Using deep sleep is not practical as it draws too much current if we want to sleep for very long periods and the ESP8266 can only sleep for about 71 minutes maximum.

I decided to use a float switch to switch on the power of the ESP. With this solution the ESP is not powered when the switch is open, so the power consumption is only the self-discharge of the batteries, which keeps the system ready to alarm for years.

When the water level reaches the float switch, the ESP starts up normally, connects to my WiFi network, sends me an e-mail and goes to sleep forever with ESP.Deepsleep(0) till the power is turned off and on again. If it can't connect to WiFi or can't send the e-mail, it goes to sleep for 20 minutes, and tries again until success.

This idea is similar to the solution described by Andreas Spiess in this video. But because of the nature of the flooding and the float switch, we don't need to add a MOSFET to keep the ESP powered on until it finishes its task, because the float switch will be closed if the water level is over the trigger level.

Step 1: The Schematic:


  • D1: BAT46 Schottky-diode for deep-sleep wakeup. I have better experiences with Schottky diodes than resistors between D0 and RST.
  • Float Switch: Simple $1.2 reed tube and magnet based float switch from eBay. The ring with the magnet can be reversed to change between high and low fluid level switching. eBay Link
  • Battery holder: for 2x AAA 1.5V batteries
  • P1: 2x 2P 5.08mm (200mil) screw terminals for connecting the wires from the battery and the float switch.
  • C1: 1000uF 10V capacitor to increase the stability of the ESP while the radio is on. Please note, if the ESP is in deep sleep, the energy stored in the capacitor is enough to power it for 3-4 minutes. In that period, the operation of the float switch can't restart the ESP because the capacitor keeps it powered on while in deep sleep. This is only interesting during testing.
  • U1: LOLIN / Wemos D1 Mini Pro ESP8266 microcontroller. This is the pro version with external antenna connector, which might be useful when it is placed in the basement. Please note, you should re-solder the 0 ohm SMD "resistor" to select the external antenna instead of the default built-in ceramic antenna. I recommend to purchase LOLIN microcontrollers from the official LOLIN AliExpress store because there are a lot of fake or old version Wemos / LOLIN boards out there.
  • Perfboard: A 50mm*50mm proto board will be enough to fit all the parts. The circuit is too simple to make a PCB. :)

Please note, the battery is connected to the 3.3V input. Although the D1 Mini has a built in LDO for USB / LiPo operation, we don't need that when it is powered from the 3V of 2xAAA alkaline batteries. With this connection my D1 Mini was able to complete it's task with only 1.8V supply voltage as well.

Step 2: The Code

The program could be nicer or simpler, but it's parts are well-proven in my other projects.

The sketch uses the following libraries:

ESP8266WiFi.h: Default for ESP8266 boards.

Gsender.h: Gmail sender library from Borya, can be downloaded from here.

The program flow is quite simple.

  • ESP starts.
  • Reads RTC memory to check is it a first start or not
  • Connects to WiFi using the cleverwifi() function. This connects to WiFi using the router MAC address (BSSID) and channel number for quicker connection, retries without those after 100 unsuccessful attempt and goes to sleep after 600 attempt. This function was derived from OppoverBakke's WiFi power consumption saver sketch, but without the saving the connection data to the RTC part in this application.
  • Checks the battery voltage with the ESP built in ADC_MODE(ADC_VCC) / ESP.getVcc() features. This doesn't require external voltage divider or any wiring to A0. Perfect for voltages under 3.3V, which is our case.
  • Sends an alrt e-mail with Gsender.h. I added variables and custom text to the subject and message strings to report battery voltage, time elapsed since first detection and advice regarding battery replacement. Please don't forget to change the recipient e-mail address.
    • Sleeps
      • If successful, it sleeps "forever" with ESP.deepSleep(0); Physically it will be in sleep mode until the water level is high. This is technically a few hours or maximum a few days, which won't drain the battery with the few uA sleep current. When the water is gone the float switch will open and the ESP will be powered off completely, and the current consumption will be 0.
      • If unsuccessful, it goes to sleep for 20 minutes, then tries again. It is possible to have an AC power outage in case of a summer thunderstorm. It counts the restarts and stores it in the RTC memory. This information is used to report the time elapsed since the first alarm attempt. (Please note, when testing it with USB power and serial monitor, the RTC may keep the cycle count value between downloads as well.)

Step 3: Assembly and Installation

After testing the code on a breadboard, I soldered it to a small piece of perfboard.

I used 2 pieces of 5.08mm pitch 2 pole screw terminals stitched together, a female header for the ESP, a capacitor and a few jumpers.

Please note, the SMD resistor with the "0" number next to the ceramic antenna should be re-soldered to the empty pads next to it to select the external antenna.

Then I put the whole thing into a small IP55 electrical junction box. The wires from the float switch are connected trough a cable gland.

The box is placed into a safe height, where the water can (hopefully) never reach it, so I used a pair of a relatively thick, 1mm^2 (17AWG) copper wire to connect the float switch. With this setup, the ESP could start and send the message even with 1.8V input voltage.

After installation, this silent sentinel is on guard, but I hope it doesn't have to send an alarm soon...