Intro: Water Meter + Arduino = Aquameterduino !
After a bad water damage (our cellar basically filled up whilst we were away for several days), I decided to monitor our water usage for anomalies. Enters Arduino...
The idea of this project is to use an arduino to count every drop of water that goes through the water company meter. Said arduino needs to be connected to my LAN so that I can query it on a regular basis, it also needs to be aware of (real) time so it can timestamp the water consumption.
For this project you will need:
- an arduino
- an ethernet shield + sd card (based on the W5100 chip)
- a proto shield
- a realtime clock (http://www.ebay.com/sch/i.html?_nkw=DS1307+Based+Real+Time+Clock)
- a shmitt trigger (e.g. 74C14N)
- a capacity (1 microF)
- some resitors (20kohm and about 100Kohm)
- a reed switch
- a reed switch-enabled water meter (Gioanola, about 75 euros http://www.gioanola.it/download/downen/08_eloutput_EN_HI.pdf)
- a set of passive POE cables (http://www.ebay.com/sch/i.html?_nkw=Passive+Power+over+Ethernet)
I asked a professional to mount a basic water meter right behind the Water Company one (which by law, I cannot touch). Said basic meter has a little magnet on one of its internal wheels and is shipped with a reed switch. So, in practice, everytime a liter goes through it, the switch is closed momentarily. Of course this switch needs to be de-bounced. This could be done by sotfware but we'll come back to that...
The arduino will serve basic http requests so that it can be queried over the LAN and will also log its activity onto a SD card. So this arduino will be a very busy one... at times. Hence the necessity for the reed switch signals to be conveyed through interrupts so that we never miss a signal. This basically rules out debouncing the switch through software as interrupts and timer do not cohabitate nicely.
Hardware debouncer it is, then !
Step 1: RC Debouncer
So, the reed switch bounces... We are going to transform the spikes produced by said switch into a nice square wave.
Extensive documentation can be found on the net on this topic (and I found this one particularly useful: http://www.ganssle.com/debouncing-pt2.htm).
In a nutshell, R x C = the time during which you want to smooth out your signal. In my case, I chose 20ms which translates into 1 microF for the capacity and 20kohm for the R2 (well all I had available was 2x 10k), R1 needs to be 'bigger' than R2, 100k is good (33k+68k in my case).
A Schmitt trigger later and I have a clean square signal ready to trigger an arduino interrupt.
I tested the setup on a bread board (as you should !) before commiting it to a proper shield.
Step 2: Ethernet and SD Card
The ethernet connection is used to answer a handful of basic http requests. A log of all water meter activity is saved on the SD card.
Using an SD card and an ethernet connection together has proven challenging (both use an SPI connection) until I used the W5100 chip based ethernet shield with an integrated SD card.
Great tutorial there: http://www.ladyada.net/learn/arduino/ethfiles.html
So that's out of the way.
Step 3: Real Time Clock, Interrupts.
My real time clocks is based on the DS1307 chip, extremely common. Add a coin cell and a (magic frequency) quartz. I liked the form factor of this RTC as it can be plugged directly onto the arduino pins, like a shield.
And interrupts just, well.., interrupt your program so that you can do something based on that event. Try to be quick (like increment a counter and return) so that if another interrupt shows up, you don't miss it.
Associate a callback method and a pin in your code. Every time a (raising) signals shows up on said pin, you get a callback...
Step 4: Putting It All Together
A little lego for grown ups... I stacked up my arduino, the ethernet shield, my custom shield and the realtime clock.
I added a couple of LEDs to my project and a (nice?) box. Given the location of my water meter, POE cables proved handy.
Step 5: Scripting / Code
I started from the ladyada code (ethernet+sd card), added an interrupt. The ip address of the shield is the only thing to be configured and you can do it via a config file on the SD card. So this script can be installed as-is, no line needs modification.
RTC setup : I set the time of my RTC using the d1307_setTime script. Good enough for my purpose. Do it once and for all, that's the whole point of the RTC module and its coin battery.
Ip address: if config.txt exists on the SD card, its content will override the ip address.
I wrote a script which runs on a linux box and which crontab fires a few times a day. Pretty brute force as it downloads the whole content of the SD card and then 'explores' it. It sends me an email with a reassuring 'everything is OK' or an alarming 'too much water consumed today' !
> crontab -l
00 8,16,23 * * * /bin/bash $HOME/bin/watermeter.sh > $HOME/watermeter.log 2>&1
Step 6: Internet of Things
I though I would experiment the Internet of Things trend and upload my data to a server. I did so (to Pachube / Cosm) but did not find it added much to this project. I left the functionality in the script as it doesn't hurt and might come in handy someday...
In theory you can connect your arduino directly to it, simplifying the project somewhat and you can install triggers/alerts.
In practice the service is in its beta version and needs to become a lot more friendly. At this point it should be viewed as a repository only, which is only a good thing if the service is reliable over time... we'll see. For now, I decided to keep control over my data logging + processing and my alerts on this side of the cloud...