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 | 19 | tresh=10 |
720d982e TM |
20 | lastdate="$(date +%s)" |
21 | laststate=0 | |
22 | history=''; | |
23 | historymax=120; | |
24 | historylen='10 30 120' | |
f51cf844 TM |
25 | screen=false |
26 | while getopts "s" OPT; do | |
27 | test "$OPT" == 's' && screen=true; | |
28 | done | |
2fbfcde7 | 29 | echo "Writing to file: $out"; |
19a509d1 | 30 | arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do |
890439ae TM |
31 | date="$(date +%s)" |
32 | time="$(echo "$line" | cut -f 1)" | |
33 | level="$(echo "$line" | cut -f 2)" | |
720d982e TM |
34 | test "$level" -gt "$tresh" && state=false || state=true |
35 | $state && statenum=1 || statenum=0; | |
36 | $state && statename='MOTION!' || statename='Nothing'; | |
37 | ||
38 | echo -ne "$time\t$date $(date '+%F %T') $statenum" | |
39 | ||
40 | #History | |
41 | after=$(( $date - $lastdate)) | |
42 | test $historymax -gt 0 && { | |
43 | history=$(echo -n "$(yes | tr '\ny' $laststate | head -c $after)$history" | head -c $historymax) | |
44 | for len in $historylen; do | |
45 | on="$(echo -n ${history::$len} | tr -d 0 | wc -c)" | |
46 | on="$(echo "scale=2; $on/$len" | bc)" | |
47 | LC_ALL=C printf " %.2f" "$on" | |
48 | done | |
49 | } | |
50 | ||
51 | #Debug | |
52 | echo -e "\t($statename $level After $after secs)"; | |
53 | ||
54 | #Fun with values | |
55 | $state && { | |
f1a48c15 | 56 | $screen && xset dpms force off || true; |
890439ae | 57 | } || { |
f1a48c15 | 58 | $screen && xset dpms force on; |
890439ae | 59 | } |
720d982e TM |
60 | ./sleepplot.sh "$out" &>/dev/null & |
61 | ||
62 | #Prepare invariants for next round | |
890439ae | 63 | lastdate="$date"; |
720d982e | 64 | laststate="$statenum"; |
890439ae | 65 | done | tee "$out" |
991ef83f | 66 | kill $pid_test |
19a509d1 | 67 | echo |
890439ae | 68 | echo "Your file: $out" |