Tuesday, March 12, 2013

Raspberry Pi and monitoring sump pump water level

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:


   1: #!/usr/bin/python
   2: #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   3: #|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
   4: #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   5: #
   6: # ultrasonic_1.py
   7: # Measure distance using an ultrasonic module
   8: #
   9: # Author : Matt Hawkins
  10: # Date   : 09/01/2013
  11: # Modified: Keith Hekker
  12: # Date    : 2013/03/11
  13: # Import required Python libraries
  14: import time
  15: import RPi.GPIO as GPIO
  16: import os
  17: import datetime
  20: # Use BCM GPIO references
  21: # instead of physical pin numbers
  22: GPIO.setmode(GPIO.BCM)
  24: # Define GPIO to use on Pi
  25: GPIO_TRIGGER = 23
  26: GPIO_ECHO    = 24
  28: print "Ultrasonic Measurement"
  30: # Set pins as output and input
  31: GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
  32: GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo
  34: # Set trigger to False (Low)
  35: GPIO.output(GPIO_TRIGGER, False)
  37: while 1:
  38:     # Allow module to settle
  39:     dTimeRecorded = datetime.datetime.now()
  40:     print dTimeRecorded
  42:     tdistance = 0
  43:     #Create a list to store distances
  44:     lDistances = []
  45:     for x in range(0,10):
  46:         time.sleep(1.0)
  47:         # Send 10us pulse to trigger
  48:         GPIO.output(GPIO_TRIGGER, True)
  49:         time.sleep(0.00001)
  50:         GPIO.output(GPIO_TRIGGER, False)
  51:         start = time.time()
  53:         while GPIO.input(GPIO_ECHO)==0:
  54:             start = time.time()
  56:         while GPIO.input(GPIO_ECHO)==1:
  57:             stop = time.time()
  59:         # Calculate pulse length
  60:         elapsed = stop-start
  62:         # Distance pulse travelled in that time is time
  63:         # multiplied by the speed of sound (cm/s)
  64:         distance = elapsed * 34300
  66:         # That was the distance there and back so halve the value
  67:         distance = distance / 2
  68:         print "Distance : %.1f" % distance
  69:         if x > 0 and distance > 0:
  70:             tdistance = tdistance + distance
  71:             lDistances.append(distance)
  73:     fdistance = tdistance/9
  74:     print "Final Distance : %.1f" % fdistance
  76:     lDistances.append(dTimeRecorded)
  77:     with open('distances','w') as file:
  78:         for item in lDistances:
  79:             file.write("{}\n".format(item))
  81:     time.sleep(300)
  83: # Reset GPIO settings
  84: GPIO.cleanup()

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

   1: def getSumpPumpDepth():
   2:     sftp = ftplib.FTP('','fred','fredspassword')
   3:     sftp.cwd("pythonprogs")
   4:     gFile = open("distancemeasurement","wb")
   5:     sftp.retrbinary("RETR distances",gFile.write)
   6:     sftp.quit()
   7:     gFile.close()
   8:     listdata = [line.strip() for line in open("distancemeasurement","r")]
   9:     nTotalDistance = 0
  10:     nListLength = len(listdata)
  11:     for x in range(0,nListLength):
  12:         if x < nListLength -1:
  13:             nTotalDistance = nTotalDistance + float(listdata[x])
  14:         else:
  15:             cTimeRecorded = listdata[x]
  17:     #print nTotalDistance
  18:     nAvgDistance = nTotalDistance/(nListLength -1)
  19:     #print nAvgDistance
  20:     #print cTimeRecorded
  21:     return "Sump pump water level: " + str(nAvgDistance)[:4] + " cm. Recorded at: " + cTimeRecorded[:19]

And here is the line it produces on the web page:


Monday, March 04, 2013

Raspberry Pi–Using FTP between 2 Pi’s programmatically

I have a need to transfer sensor data periodically from on Pi to another. For no good reason, I decided to use FTP for this. Actually, as it turned out, it was a good decision, since the process works well and the overhead involved, i.e. installation and programming as well as CPU time, is minimal.

Both Pi’s are running Raspbian Wheezy, latest version.

This is what I did:

On the Pi that has the data that I want, I installed vsftp thusly:

sudo apt-get install vsftpd

Once the installation finished I adjusted the configuring parameters:

sudo nano /etc/vsftpd.conf

Once this file opened up, I removed the # tags in front of local_enable=YES and write_enable=YES, changed anymous_enable to NO, then closed and saved the file when I was prompted.

Next, I restarted the service like this:

sudo service vsftpd restart

On the Pi that needs the data I do the retrieving via FTP in a python program using a standard Python library, surprising called ftplib.

Here’s the program:

   1: import ftplib
   2: sftp = ftplib.FTP('','pi','mypassword')
   3: sftp.cwd("pythonprogs")
   4: gFile = open("distancemeasurement","wb")
   5: sftp.retrbinary("RETR distancemeasurement",gFile.write)
   6: sftp.quit()
Line 3 (cwd) is Change Working Directory)

That’s it! Pretty straightforward, I think.