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"
18 killall speaker-test
&>/dev
/null
19 speaker-test
-t sine
&>/dev
/null
&
22 bash .
/sleepstats.sh
"$out" &>/dev
/null
&
25 lastdate
="$(date +%s)"
29 historylen
='10 30 60 90 120'
35 #trap 'kill -9 $(jobs -p);' SIGINT
37 while getopts "sg" OPT
; do
38 test "$OPT" == 's' && screen
=true
;
39 test "$OPT" == 'g' && graph
=true
;
42 echo "Writing log to: $out";
43 $graph && echo "Writing graph to: $graphout"
45 arecord | .
/goertzel
-n i
-q -l c
-t $tresh -d 4 |
while read line
; do
47 time="$(echo "$line" | cut -f 1)"
48 level
="$(echo "$line" | cut -f 2)"
49 test "$level" -gt "$tresh" && state
=false || state
=true
50 $state && statenum
=1 || statenum
=0;
51 $state && statename
='MOTION!' || statename
='Nothing';
53 printf "%.2f\t %s %s %d" "$time" "$date" "$(date '+%F %T')" "$statenum"
56 after
=$
(( $date - $lastdate))
57 test $historymax -gt 0 && {
58 history=$
(echo -n "$(yes | tr '\ny' $laststate | head -c $after)$history" |
head -c $historymax)
59 for len
in $historylen; do
60 on
="$(echo -n ${history::$len} | tr -d 0 | wc -c)"
61 on
="$(echo "scale
=2; $on/$len" | bc)"
67 printf " (%s %3d After %4d secs)\n" "$statename" "$level" "$after";
71 $screen && xset dpms force off || true
;
73 $screen && xset dpms force on
;
75 $graph && .
/sleepplot.sh
"$out" "$graphout" &>/dev
/null
&
77 #Prepare invariants for next round
79 laststate
="$statenum";
81 kill $
(jobs -p); sleep 0.2
85 .
/sleepplot.sh
"$out" "$graphout" &>/dev
/null
86 echo "Your graph: $graphout"