Commit | Line | Data |
---|---|---|
32f185d8 | 1 | #!/bin/bash |
890439ae TM |
2 | # Simple sleep monitor (Harvie 2012) |
3 | # | |
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) | |
8 | # Power up PIR | |
9 | # Launch this script | |
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 | |
12 | # Wake up | |
13 | # Enjoy your data | |
14 | ||
5e47bf73 | 15 | out=/tmp/sleeplog-"$(date +%F_%T)".txt |
5769d767 | 16 | graphout="${out%%.*}.png" |
86dfe893 H |
17 | |
18 | killall speaker-test &>/dev/null | |
890439ae | 19 | speaker-test -t sine &>/dev/null & |
43f096b6 TM |
20 | |
21 | touch "$out" | |
22 | bash ./sleepstats.sh "$out" &>/dev/null & | |
86dfe893 | 23 | |
890439ae | 24 | tresh=10 |
720d982e TM |
25 | lastdate="$(date +%s)" |
26 | laststate=0 | |
27 | history=''; | |
28 | historymax=120; | |
6e6479ce | 29 | historylen='10 30 60 90 120' |
f51cf844 | 30 | screen=false |
5769d767 TM |
31 | graph=false |
32 | ||
3afb9fca H |
33 | export LC_ALL=C |
34 | ||
43f096b6 TM |
35 | #trap 'kill -9 $(jobs -p);' SIGINT |
36 | ||
5769d767 | 37 | while getopts "sg" OPT; do |
f51cf844 | 38 | test "$OPT" == 's' && screen=true; |
5769d767 | 39 | test "$OPT" == 'g' && graph=true; |
f51cf844 | 40 | done |
5769d767 TM |
41 | |
42 | echo "Writing log to: $out"; | |
43 | $graph && echo "Writing graph to: $graphout" | |
44 | echo | |
19a509d1 | 45 | arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do |
890439ae TM |
46 | date="$(date +%s)" |
47 | time="$(echo "$line" | cut -f 1)" | |
48 | level="$(echo "$line" | cut -f 2)" | |
720d982e TM |
49 | test "$level" -gt "$tresh" && state=false || state=true |
50 | $state && statenum=1 || statenum=0; | |
51 | $state && statename='MOTION!' || statename='Nothing'; | |
52 | ||
3afb9fca | 53 | printf "%.2f\t %s %s %d" "$time" "$date" "$(date '+%F %T')" "$statenum" |
720d982e TM |
54 | |
55 | #History | |
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)" | |
3afb9fca | 62 | printf " %.2f" "$on" |
720d982e TM |
63 | done |
64 | } | |
65 | ||
66 | #Debug | |
3afb9fca | 67 | printf " (%s %3d After %4d secs)\n" "$statename" "$level" "$after"; |
720d982e TM |
68 | |
69 | #Fun with values | |
70 | $state && { | |
f1a48c15 | 71 | $screen && xset dpms force off || true; |
890439ae | 72 | } || { |
f1a48c15 | 73 | $screen && xset dpms force on; |
890439ae | 74 | } |
5769d767 | 75 | $graph && ./sleepplot.sh "$out" "$graphout" &>/dev/null & |
720d982e TM |
76 | |
77 | #Prepare invariants for next round | |
890439ae | 78 | lastdate="$date"; |
720d982e | 79 | laststate="$statenum"; |
890439ae | 80 | done | tee "$out" |
43f096b6 | 81 | kill $(jobs -p); sleep 0.2 |
19a509d1 | 82 | echo |
5769d767 TM |
83 | echo "Your log: $out" |
84 | $graph && { | |
85 | ./sleepplot.sh "$out" "$graphout" &>/dev/null | |
86 | echo "Your graph: $graphout" | |
87 | } |