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