Sunday, March 16, 2014

RasPi Softener Salt Level Monitor

If you can't tell from the fact that this is my first post in 2014 and it's March already that I am not one for routine tasks, then I'll tell you.  I'm not one for routine tasks.  More often then not, I get distracted or forget.  So when it comes to my water softener, I usually discover it's low after the black plastic spatulas all turn white.  So it's about time I did something about it.
I was inspire by the cool (and cheap) HC-SR04 distance sensors. They look like cool little robot heads and can tell you how far away something is.  I HAD to use one for something.
Johnny Five
The idea was simple.  Monitor the distance from the lid of the softener do the bottom and alert when the distance to too far.


So I build up a list of requirements:

  1. Wireless, because there's no network jack near my softener
  2. Simple, because I don't want Frankenstein sitting on top of my softener getting bored. 
  3. Sends an alert, because if I'm going to forget to walk downstairs and check the softener tank, I'm certain I'll forget to check some webpage until the spatulas are all white.

The design came together naturally:

  1. Raspberry Pi with a wifi dongle (still cheaper than any Arduino + wifi solution)
  2. HC-SR04 distance sensor, cause this was the original requirement
  3. Xively Internet of Things data service.  This provides a simple way to push lots of simple data from simple devices to the cloud
  4. Zapier rule engine site.  This is a fantastic service that is like "If this then that" but with useful data sources rather than just trendy social media things.

The build was pretty simple:


The Sensor has four pins. The VCC and Ground pins are really simple.  I  hooked one up the the 5v out on my Pi and one the ground using standard female to female jumpers.

The next pin of interest is the Trig pin.  This pin triggers the pulse of sound from the sensor.  It can be triggered with a simple 3.3v pulse from a unmodified Pi pin.  so far so good.  Three pins, three jumpers.

But, oh no! 5v vs 3.3v strikes again!  The Echo pin outputs 5v! and that is Bad® for Pi pins.  What to do?  When lets look at how the sensor works.
  1. Sensor gets a 10us pulse on the Trig pin
  2. Sensor sends an audio pulse out
  3. Sensor listens intently for a reply while drumming its fingers
  4. When the sensor finally hears a response it raises its Echo pin voltage for as long as it took for the echo to return.
  5. Sensor continues drumming its fingers until the Trig pin goes high again.
Pretty cool, and you can't hear the sensor, even when it's drumming its fingers.

So this means the Echo pin is not communicating with serial, spi, or i2c protocol.  So we can use a simple resistor to drop the return voltage to a tolerable level. A simple 1000 Ohm Resistor (Brown, Black, Red) will do the trick.  I split one of my jumper wires and soldered in a resistor.
Shrink tubing or electricians tape will button this up nicely

Ok, so this last wire just goes from the Echo pin to a pin on the Pi.


Next it's all about coding


We need to read the sensor and send the data to the cloud.  To do this I borrowed some great python code from Adam Lappin on his Byte Creation blog.  He creates a reading function that triggers the sensor, times how long the Echo pin is high and then using an average speed of sound calculates the distance the sound traveled.

Yay!  A distance.  Now to send it to the cloud.  I borrowed some more code because it's already written, so why not, from the Xively Raspberry Pi Sample code and combined them.  The result is hosted on github for all to use and abuse.

Before using the code make sure to setup an account with Xively and to setup a device.  The API key and Feed ID variables will need to be set from your account.  It wont work without them.

Cool all done right?  Nope.  Because even though the script pushes data to the cloud, it won't tell me when the distance exceeds a couple feet.  That's what Zapier is for.

Setup Alerting is the next step


Zapier is a cool web service rule engine that allows you to create rules that have a trigger and an action.  Zapier has a trigger called a WebHook.  Which is essentially a URL that when it receives structured data, triggers a "Zap" or rule on Zapier.  So I setup an account and followed the simple tutorial from Xively on how to create and connect a Xively alert to a Zapier WebHook.

I setup an SMS message to go to my phone when the distance is reported greater than two feet.  This gives me plenty of time to fetch more salt before the spatulas turn white.  I also added a trigger if the feed "freezes" to send an email to me so I know something happened to my Pi or script.

Finally I mounted the hardware


My softener has a removable lid, so I used some double sided foam tape to attach the sensor to the bottom of the lid, not too close to the edge as to avoid false echos.
Don't put me in the tank!! Noooooo
Then I wrapped the cables around the lid and attached the Pi in it's case to the top of the lid.
All closed up and running
One last bit.  I set the script to run when the Pi booted with the following line in the /etc/rc.local

sudo /home/pi/scripts/sonar.py &

Finally my water softener can talk to me, every home owner's dream.
Feed Me!!!

6 comments:

  1. I attempted a similar thing, except instead of rangefinding salt levels I was trying to measure the depth of the sump pump well. Worked out fine until the Maxbotix LV-EZ4 fell into the well and got nice & saturated over the course of a day. It was beyond drying out.

    ReplyDelete
    Replies
    1. I ended up using a float switch (http://www.homesecuritystore.com/p-843-sb2-sump-bobber-water-level-sensor.aspx) for monitoring my sump. I have a bayweb thermostat (which I love) and it has a dry contact port for alerting. I set it to SMS me if the float switch closes. I suppose there's no reason that switch can't be wired up to a Pi just as easily.

      Delete
  2. Great thing I come to know from this post. I really didn't have such idea before. I'll definitely try this by myself and link this post to my articles. Thanks Ben ... for sharing such new idea (at least for me ;)

    Alex | Water Softener Critic

    ReplyDelete
  3. Thank you for sharing this information with the world. I can appreciate how much research you did on this topic and you really came through with valid points of interest..
    Keep on it..
    Peter

    ReplyDelete
  4. Hi. I am in the process of doing the same project, however some of the links above are no longer valid.
    Is anybody able to help with the missing info please?
    thank you
    Richard

    ReplyDelete
  5. Your article has piqued a lot of positive interest. I can see why since you have done such a good job of making it interesting softener salt

    ReplyDelete