2 # Simple sleep monitor (Harvie 2012)
4 # You probably have soundcard with one output and input
5 # Take PIR motion sensor from security system
6 # Use PIR to switch connection between left channels of output and input (tips of 3.5mm jacks)
7 # Connect grounds of these two together (maybe you will not need it - depending on your soundcard)
10 # Use alsamixer and some GUI recorder (like audacity) to tune volume to some usable level
11 # Place PIR facing to your bed and go sleep
15 out
=/tmp
/sleeplog-
"$(date +%F_%T)".txt
16 graphout
="${out%%.*}.png"
17 speaker-test
-t sine
&>/dev
/null
&
20 lastdate
="$(date +%s)"
24 historylen
='10 30 60 90 120'
28 while getopts "sg" OPT
; do
29 test "$OPT" == 's' && screen
=true
;
30 test "$OPT" == 'g' && graph
=true
;
33 echo "Writing log to: $out";
34 $graph && echo "Writing graph to: $graphout"
36 arecord | .
/goertzel
-n i
-q -l c
-t $tresh -d 4 |
while read line
; do
38 time="$(echo "$line" | cut -f 1)"
39 level
="$(echo "$line" | cut -f 2)"
40 test "$level" -gt "$tresh" && state
=false || state
=true
41 $state && statenum
=1 || statenum
=0;
42 $state && statename
='MOTION!' || statename
='Nothing';
44 echo -ne "$time\t$date $(date '+%F %T') $statenum"
47 after
=$
(( $date - $lastdate))
48 test $historymax -gt 0 && {
49 history=$
(echo -n "$(yes | tr '\ny' $laststate | head -c $after)$history" |
head -c $historymax)
50 for len
in $historylen; do
51 on
="$(echo -n ${history::$len} | tr -d 0 | wc -c)"
52 on
="$(echo "scale
=2; $on/$len" | bc)"
53 LC_ALL
=C
printf " %.2f" "$on"
58 echo -e "\t($statename $level After $after secs)";
62 $screen && xset dpms force off || true
;
64 $screen && xset dpms force on
;
66 $graph && .
/sleepplot.sh
"$out" "$graphout" &>/dev
/null
&
68 #Prepare invariants for next round
70 laststate
="$statenum";
72 kill $pid_test; sleep 0.2
76 .
/sleepplot.sh
"$out" "$graphout" &>/dev
/null
77 echo "Your graph: $graphout"