Commit | Line | Data |
---|---|---|
890439ae TM |
1 | #!/bin/sh |
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 | ||
f51cf844 | 15 | |
5e47bf73 | 16 | out=/tmp/sleeplog-"$(date +%F_%T)".txt |
890439ae | 17 | speaker-test -t sine &>/dev/null & |
991ef83f | 18 | pid_test=$! |
890439ae TM |
19 | tresh=10 |
20 | lastdate=0 | |
f51cf844 TM |
21 | screen=false |
22 | while getopts "s" OPT; do | |
23 | test "$OPT" == 's' && screen=true; | |
24 | done | |
2fbfcde7 | 25 | echo "Writing to file: $out"; |
19a509d1 | 26 | arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do |
890439ae TM |
27 | date="$(date +%s)" |
28 | time="$(echo "$line" | cut -f 1)" | |
29 | level="$(echo "$line" | cut -f 2)" | |
5e47bf73 | 30 | echo -ne "$time\t$date\t$(date '+%F%t%T')\t" |
890439ae | 31 | test "$level" -gt "$tresh" && { |
7cd5c7da | 32 | echo -n "0 Nothing detected..."; |
f1a48c15 | 33 | $screen && xset dpms force off || true; |
890439ae | 34 | } || { |
7cd5c7da | 35 | echo -n "1 Motion detected!!!!"; |
f1a48c15 | 36 | $screen && xset dpms force on; |
890439ae TM |
37 | } |
38 | test "$lastdate" != 0 && { | |
39 | after=$(( $date - $lastdate)) | |
40 | echo -ne "\t$level After $after secs"; | |
41 | } | |
42 | echo; | |
827dfbda | 43 | ./sleepplot.sh "$out" &>/dev/null |
890439ae TM |
44 | lastdate="$date"; |
45 | done | tee "$out" | |
991ef83f | 46 | kill $pid_test |
19a509d1 | 47 | echo |
890439ae | 48 | echo "Your file: $out" |