Difference between revisions of "Sump Pump Monitor"

From NAS-Central Buffalo - The Linkstation Wiki
Jump to: navigation, search
m
m
Line 28: Line 28:
 
* sox
 
* sox
 
* gnuplot
 
* gnuplot
 +
* mpack
  
  
Line 36: Line 37:
 
I just ran a network feed down to the "computer" and strapped the microphone near the check valve on the exit pipe.  Every time the pump goes off the noise of the check valve triggers an event
 
I just ran a network feed down to the "computer" and strapped the microphone near the check valve on the exit pipe.  Every time the pump goes off the noise of the check valve triggers an event
  
==Sound Detect==
+
==Scripts==
 +
===Sound Detect===
  
 
This script is essentially run as a Daemon
 
This script is essentially run as a Daemon
Line 124: Line 126:
 
</pre>
 
</pre>
  
==waterd-hourly==
+
===waterd-hourly===
  
 
This writes a log file out of the events in the last hour and deletes the even file
 
This writes a log file out of the events in the last hour and deletes the even file
Line 184: Line 186:
 
</pre>
 
</pre>
  
==waterd-minute==
+
===waterd-minute===
 +
 
 +
This just runs every minute to make sure the daemon is running
 +
 
 
<pre>
 
<pre>
 
#!/bin/bash
 
#!/bin/bash
 +
#
 +
# Copyright (C) 2014 Uday K. Paul
 +
#
 +
# Aug  28, 2014:
 +
#
 +
# This program is free software. You may distribute it under the terms of
 +
# the GNU General Public License as published by the Free Software
 +
# Foundation, version 2.
 +
#
 +
# This program is distributed in the hope that it will be useful, but
 +
# WITHOUT ANY WARRANTY; without even the implied warranty of
 +
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 +
# Public License for more details.
 +
#
 +
#
 +
 +
 
result=`ps aux | grep -i "sound-detect" | grep -v "grep" | wc -l`
 
result=`ps aux | grep -i "sound-detect" | grep -v "grep" | wc -l`
  
Line 198: Line 220:
 
</pre>
 
</pre>
  
==waterd-daily==
+
===waterd-daily===
<pre>#!/bin/bash
+
 
 +
 
 +
<pre>
 +
#!/bin/bash
 +
#!/bin/bash
 +
#
 +
# Copyright (C) 2014 Uday K. Paul
 +
#
 +
# Aug  28, 2014:
 +
#
 +
# Daily script to Sum up pump events over the day, tweet the total and send a graph of the last weeks events
 +
#
 +
# This program is free software. You may distribute it under the terms of
 +
# the GNU General Public License as published by the Free Software
 +
# Foundation, version 2.
 +
#
 +
# This program is distributed in the hope that it will be useful, but
 +
# WITHOUT ANY WARRANTY; without even the implied warranty of
 +
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 +
# Public License for more details.
 +
#
 +
#
  
  
Line 207: Line 250:
 
ttytter -status="Pumped $times times yesterday"
 
ttytter -status="Pumped $times times yesterday"
  
gnuplot /usr/local/bin/plotgraph && mpack -s Sumppump  out.pdf  EMAI@ADDRESS && rm out.pdf
+
gnuplot /usr/local/bin/plotgraph && mpack -s Sumppump  out.pdf  EMAIL@ADDRESS && rm out.pdf
 
+
  
 
</pre>
 
</pre>
  
 
==plotgraph==
 
==plotgraph==
 +
The gnuplot file that generates a pdf of the last weeks data from the hourly log file
 +
 
<pre>
 
<pre>
 
#!/usr/bin/gnuplot
 
#!/usr/bin/gnuplot
Line 244: Line 288:
  
 
==/etc/crontab==
 
==/etc/crontab==
 +
 +
The crontab that runs the above scripts on the day, hour and minute
 +
 
<pre>
 
<pre>
 
# /etc/crontab: system-wide crontab
 
# /etc/crontab: system-wide crontab

Revision as of 01:43, 13 September 2014


The sump pump setup

What you need

File:Sumpgraph.gif
Graph of weeks sump pump output
X Box Microphone

Any Debian based device will do. I happen to be using a dockstar here, instructions to debianize it are here: http://projects.doozan.com/debian/


You will Need:

  • Debian based device with Network access and USB port
  • USB Microphone

Need to install

  • Ruby
  • ttytter
  • alsa-utils
  • sox
  • gnuplot
  • mpack


Admittedly this is just a collection of shell scripts. I use a USB microphone from an XBOX that can be found used at almost any thrift store for a few dollars, or at GameStop for a bit more.

The meat of the scripting is done by an simple but excellent script I found by Thomer Gill written in ruby that I modified just a bit and his tutorial on sound detection using sox http://thomer.com/howtos/detect_sound.html

I just ran a network feed down to the "computer" and strapped the microphone near the check valve on the exit pipe. Every time the pump goes off the noise of the check valve triggers an event

Scripts

Sound Detect

This script is essentially run as a Daemon it samples 3 second sound clips, gets the maximum sound amplitude and writes a timestamp to a file, in this case

/root/myfile.out

(yes I know that's a real bad idea)

#!/usr/bin/ruby -w
#
# Copyright (C) 2014 Uday K. Paul
#
# Aug  28, 2014: Modified for use with sump pump
#
# Derived from original version Copyright (C) 2009 Thomer M. Gil [http://thomer.com/]
#
# Oct  22, 2009: Initial version
# 
# This program is free software. You may distribute it under the terms of
# the GNU General Public License as published by the Free Software
# Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# This program detects the presence of sound and appends a datestamp to a logfile
#

require 'getoptlong'

# You need to replace MICROPHONE with the name of your microphone, as reported
# by /proc/asound/cards
MICROPHONE = 'Logitech'
HWDEVICE = `cat /proc/asound/cards | grep '#{MICROPHONE}' | awk '{print $1}'`.to_i
SAMPLE_DURATION = 3 # seconds
FORMAT = 'S16_LE'   # this is the format that my USB microphone generates
THRESHOLD = 0.15

if !File.exists?('/usr/bin/arecord')
  warn "/usr/bin/arecord not found; install package alsa-utils"
  exit 1
end

if !File.exists?('/usr/bin/sox')
  warn "/usr/bin/sox not found; install package sox"
  exit 1
end

if !File.exists?('/proc/asound/cards')
  warn "/proc/asound/cards not found"
  exit 1
end

$options = {}
opts = GetoptLong.new(*[
  [ "--verbose", "-v", GetoptLong::NO_ARGUMENT ],
])
opts.each { |opt, arg| $options[opt] = arg }

time1=0

loop do
  out = `/usr/bin/arecord -D plughw:#{HWDEVICE},0 -d #{SAMPLE_DURATION} -f #{FORMAT} 2>/dev/null | /usr/bin/sox -t .wav - -n stat 2>&1`
  out.match(/Maximum amplitude:\s+(.*)/m)
  amplitude = $1.to_f
  puts amplitude if $options['--verbose']  
  if amplitude > THRESHOLD
    time2=time1
    time1 = Time.new
    diff = time1-time2
    if diff.to_f > 8.0
    open('/root/myfile.out', 'a') { |f|
    f.puts time1.inspect 
    
      } 
    puts diff 
    else
    end    
else
  end
end

waterd-hourly

This writes a log file out of the events in the last hour and deletes the even file


#!/bin/bash
#
# Copyright (C) 2014 Uday K. Paul
#
# Aug  28, 2014: 
#
# Hourly script to Sum up pump events over the last hour and write to an hourly log file that is kept
# with the pump event file being deleted
#
# This program is free software. You may distribute it under the terms of
# the GNU General Public License as published by the Free Software
# Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# This program detects the presence of sound and appends a datestamp to a logfile
#

highlimit=30

result=`ps aux | grep -i "sound-detect" | grep -v "grep" | wc -l`
read lines words chars filename <<< $(wc /root/myfile.out)

paste <(date +%D%t%k )  <(wc -l /root/myfile.out ) | awk '{ print $1" "$2" " $3}' >> /root/hourly.log

if [[ $lines -gt $highlimit ]]
 then
ttytter  -status="Pumped in last hour $lines"

elif [[ $lines -eq 0 ]]
 then
ttytter  -status="No Pumping in the last Hour"

else
echo "pump good"
echo $lines "pumped"
fi


 rm /root/myfile.out


if [ $result -ge 1 ]
   then
        echo "script is running"
   else
        /usr/local/bin/sound-detect &
fi

waterd-minute

This just runs every minute to make sure the daemon is running

#!/bin/bash
#
# Copyright (C) 2014 Uday K. Paul
#
# Aug  28, 2014: 
#
# This program is free software. You may distribute it under the terms of
# the GNU General Public License as published by the Free Software
# Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
#


result=`ps aux | grep -i "sound-detect" | grep -v "grep" | wc -l`


if [ $result -ge 1 ]
   then
        echo "script is running"
   else
        /usr/local/bin/sound-detect &
fi

waterd-daily

#!/bin/bash
#!/bin/bash
#
# Copyright (C) 2014 Uday K. Paul
#
# Aug  28, 2014: 
#
# Daily script to Sum up pump events over the day, tweet the total and send a graph of the last weeks events 
#
# This program is free software. You may distribute it under the terms of
# the GNU General Public License as published by the Free Software
# Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
#



times=`grep $(date --date="-1 day" +"%D%t") /root/hourly.log | awk '{total=total + $3}END{print total}'`

ttytter -status="Pumped $times times yesterday"

gnuplot /usr/local/bin/plotgraph && mpack -s Sumppump  out.pdf  EMAIL@ADDRESS && rm out.pdf

plotgraph

The gnuplot file that generates a pdf of the last weeks data from the hourly log file

#!/usr/bin/gnuplot
reset
# set terminal png size 2048,1536 linewidth 9  enhanced font ',30'
# set output "graph.png"

set terminal pdf font ',8'
set output 'out.pdf'

set xdata time
set timefmt "%m/%d/%Y %H"
set format x "%m/%d"
set xlabel "Date"

set ylabel "Cycles in Hour"

set title "Sump Pump" font ',20'

set key reverse Left outside

set style data lines 


 plot "< tail -168 /root/hourly.log" using 1:3 smooth unique title ""
#plot "/root/hourly.log" using 1:3 smooth unique title ""


/etc/crontab

The crontab that runs the above scripts on the day, hour and minute

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
00 *    * * *   root    /usr/local/bin/waterd-hourly >/dev/null 2>&1
*  *    * * *   root    /usr/local/bin/waterd-minute >/dev/null 2>&1
00 8    * * *   root    /usr/local/bin/waterd-daily >/dev/null 2>&1
#