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
&
23 lastdate
="$(date +%s)"
27 historylen
='10 30 60 90 120'
31 while getopts "sg" OPT
; do
32 test "$OPT" == 's' && screen
=true
;
33 test "$OPT" == 'g' && graph
=true
;
36 echo "Writing log to: $out";
37 $graph && echo "Writing graph to: $graphout"
39 arecord | .
/goertzel
-n i
-q -l c
-t $tresh -d 4 |
while read line
; do
41 time="$(echo "$line" | cut -f 1)"
42 level
="$(echo "$line" | cut -f 2)"
43 test "$level" -gt "$tresh" && state
=false || state
=true
44 $state && statenum
=1 || statenum
=0;
45 $state && statename
='MOTION!' || statename
='Nothing';
47 echo -ne "$time\t$date $(date '+%F %T') $statenum"
50 after
=$
(( $date - $lastdate))
51 test $historymax -gt 0 && {
52 history=$
(echo -n "$(yes | tr '\ny' $laststate | head -c $after)$history" |
head -c $historymax)
53 for len
in $historylen; do
54 on
="$(echo -n ${history::$len} | tr -d 0 | wc -c)"
55 on
="$(echo "scale
=2; $on/$len" | bc)"
56 LC_ALL
=C
printf " %.2f" "$on"
61 printf "\t(%s %3d After %4d secs)\n" "$statename" "$level" "$after";
65 $screen && xset dpms force off || true
;
67 $screen && xset dpms force on
;
69 $graph && .
/sleepplot.sh
"$out" "$graphout" &>/dev
/null
&
71 #Prepare invariants for next round
73 laststate
="$statenum";
75 kill $pid_test; sleep 0.2
79 .
/sleepplot.sh
"$out" "$graphout" &>/dev
/null
80 echo "Your graph: $graphout"