In previous posts, I have detailed how I keep track of the status of my stand by generator. One of the main tasks of the stand by generator is to ensure that there is a steady supply of power for the sump pump. So, wouldn’t it be nice if we could monitor the level of the sump, preferably remotely, so we can take action if it is too high?
Raspberry Pi to the rescue. In the course of investigating distance sensors, I came across http://www.raspberrypi-spy.co.uk/2012/12/ultrasonic-distance-measurement-using-python-part-1/. It explains the use of the ultrasonic distance sensor HY-SRF05, which can read distances between 5cm and 2m. Perfect for my purpose. Besides, they only cost around $3.00 each!
I took the very simple voltage divider circuit mentioned on that webpage and created a little circuit board for it. Here’s it is, fresh out of the etcher, with the ink of hand drawn traces still covering the only copper left on the board. To the top you can see the 4 points of the board to which the HY-SRF05 will be attached by solder points.
Once I sanded the ink off, drilled the holes and mounted the resistors, connecting wires and sensor, I did a test. It worked! Woohoo! Next, accuracy testing was conducted, proving that most of the time the reading is within 5%. Good enough for my purpose.
To attach the sensor in a convenient spot, I used a hose clamp and short piece of wire strapping. Through experimenting I found out that in order to get a fairly accurate reading, you need to get as much clearance on the sides of the sensor as possible. The wire strapping allows for this, as it is bendable and strong enough to support the lightweight sensor. The sensor and circuit board are kept in place with a colourful binder clip, for easy removal in case the pump needs to be serviced.
The Raspberry Pi itself is mounted some distance away (+/- 50 cm) on the sump pump closet wall, using a case I obtained from Allied Electronics. A couple of screws inserted in the drywall keep the Pi high and dry.
So much for the hardware. Now, I should point out that this Pi is the second one I own, the first one being used to monitor generator temperature. The Pi uses a concept called ‘bit-banging’ to get the readings from the sensor. If the CPU is busy servicing a lot of processes, that could read to erroneous readings, since the CPU ‘time slices’ and is too busy to pay attention to our sensor when needed. So, for now, this is all the Pi does. I adapted the Python program from Raspberry Pi Spy to every five minutes take 10 readings and store these, along with the current time) in a simple text file. I also installed an FTP service on this Pi (‘vsfptd’) which will be used by my primary Pi to obtain the data as needed.
Here’s the amended program:
It does need to use supervisory permission to run, so once you switched to the directory where your Pi program is stored, you need to type in on the command line sudo python ultrasonic_1.py.
So much for the data generating Pi. Now we switch our attention to the data consuming Pi. It already runs as a webserver using bottle.py straight from within Python. All I did was add a function that generates an FTP call(using the Python library FTPlib), opens the file downloaded, reads the 9 values, adds them up and divides the total by nine to get the average value. It also retrieves the time when these values were recorded. Lastly, it generates a line of text along the lines of ‘Sump pump water level: 8.08 cm. Recorded at: 2013-03-12 19:18:47’, which can be plugged into the HTML template being used. Speaking of this template, in the appropriate place on the front page, I added a variable that bottle.py would populate at run time. That’s it!
Here’s the function I added into my bottle web server program
And here is the line it produces on the web page: