A Wireless Sump Pump Not Working Alarm Using MicroPython and NodeMCU
A Wireless Flood Alarm Using MicroPython and NodeMCU

Several times over the years our sump pump or float switch has failed and part of our basement started to flood. This is especially nasty, as the flood isn't just water. It's sewer water. I purchased a cheap electronic alarm in the past, but it wasn't really what we needed. I don't know if this will be just what we need either, but since I built this one, I can at least adapt it as I see faults. Basically the system consists of a NodeMCU wireless device using an ESP8266, (costing about $3.00 from AliExpress), with a float switch that when tripped loads a web page on my local web server, an Odroid C1, running headless Ubuntu, and when that php web page is loaded, an email is sent to me (and anyone else I choose) saying the flood alarm went off, etc. I think I can find a way to get an email to be forwarded to our cell phones too, but at this point, I haven't done that.

On the Odroid C1, I installed the mailutils package, as well as php and apache2. I don't have any domain, so I just left that blank. The email just says it came from www-data, when it arrives in my Inbox. I put a web_page.php in /var/www/html that contains this kind of thing:

This is where I can put some email commands for the rest of my family, and SMS texts, so nobody runs any more water downstairs until the problem is resolved. It also makes a terrible racket at about 250 Hz through a speaker driven by an H bridge so anyone in the vicinity of the alarm will hear it as well and in case the emails and texts don't work for some reason.

To get MicroPython on the NodeMCU, I followed the instructions at the MicroPython github repository. First the development tools built nicely on my Ubuntu 16.10 laptop, then in the esp8266 directory, MicroPython built. I erased the flash as suggested, and used make deploy to flash the NodeMCU with MicroPython 1.8.6. I'm total newbie at python, and found the mu editor very helpful to eliminate syntax errors. I couldn't get it to flash the files to the NodeMCU, and had to go in to mu/mu/logic.py to help it find my device at /dev/ttyUSB3 as I described on the forum. Hopefully these issues are fixed sometime. In the meantime I cloned the webrepl repository, and was able to get it connected to the NodeMCU through my home router. It has a way to download files to the NodeMCU. I used this to test things out after getting the syntax to pass mu's test.

The float switch was purchased from AliExpress for about a dollar. The kind I have is seen below:

float switch
I connected it between D1 and ground on the NodeMCU. D1 is GPIO5 and is just abbreviated as 5 in MicroPython. The H bridge and with speaker are hooked to GPIO12 and GPIO14 (for IN_A and IN_B), and 3.3V and GND. I'm still a bit puzzled why I can't use the Vin pin to supply the H bridge with 5 volts, but when I hook it up there the voltage falls to zero. The schematic of the NodeMCU seemed to indicate it would supply voltage there when powered over USB, but it doesn't. I need to look into this some more, because 5 volts should make the alarm even louder, which would be better.

I found that MicroPython is a work in progress. It might have been easier in Lua that comes with NodeMCU, but I wanted to try MicroPython out, and to get some experience in Python. For the ESP8266 used on the NodeMCU, the most complete documentation at this time is the pdf on version 1.8.6. I found that a number of the things, even there, are not yet functional on the ESP8266 at this time (December 2016), but by testing, I found what works, and enough does. I was initially using a simple pin interrupt for this project, but after testing I found the the jerk that happens when the sump pump turns on caused the float switch to momentarily lose connection and trip the interrupt. To remedy this I went to checking to see if the float switch was up for consecutive seconds. My code is shown below. I put it in main.py, which the NodeMCU executes after boot.py upon start-up.

The blue LED on the NodeMCU flashes toggles on and off every second when no alarm has been reported, but when an alarm has been reported, it is on solid, and the speaker is making an annoying racket.

The NodeMCU is powered by a USB power supply. The float switch is normally closed, and is mounted in the sump .

Hopefully this keeps us from another problem the next time the sump pump or its float switch fails.