Aligned humandebug columns
[mirrors/Programs.git] / c / goertzel / sleepmon.sh
CommitLineData
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 15out=/tmp/sleeplog-"$(date +%F_%T)".txt
5769d767 16graphout="${out%%.*}.png"
86dfe893
H
17
18killall speaker-test &>/dev/null
890439ae 19speaker-test -t sine &>/dev/null &
991ef83f 20pid_test=$!
86dfe893 21
890439ae 22tresh=10
720d982e
TM
23lastdate="$(date +%s)"
24laststate=0
25history='';
26historymax=120;
6e6479ce 27historylen='10 30 60 90 120'
f51cf844 28screen=false
5769d767
TM
29graph=false
30
31while getopts "sg" OPT; do
f51cf844 32 test "$OPT" == 's' && screen=true;
5769d767 33 test "$OPT" == 'g' && graph=true;
f51cf844 34done
5769d767
TM
35
36echo "Writing log to: $out";
37$graph && echo "Writing graph to: $graphout"
38echo
19a509d1 39arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do
890439ae
TM
40 date="$(date +%s)"
41 time="$(echo "$line" | cut -f 1)"
42 level="$(echo "$line" | cut -f 2)"
720d982e
TM
43 test "$level" -gt "$tresh" && state=false || state=true
44 $state && statenum=1 || statenum=0;
45 $state && statename='MOTION!' || statename='Nothing';
46
47 echo -ne "$time\t$date $(date '+%F %T') $statenum"
48
49 #History
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"
57 done
58 }
59
60 #Debug
e4016aeb 61 printf "\t(%s %3d After %4d secs)\n" "$statename" "$level" "$after";
720d982e
TM
62
63 #Fun with values
64 $state && {
f1a48c15 65 $screen && xset dpms force off || true;
890439ae 66 } || {
f1a48c15 67 $screen && xset dpms force on;
890439ae 68 }
5769d767 69 $graph && ./sleepplot.sh "$out" "$graphout" &>/dev/null &
720d982e
TM
70
71 #Prepare invariants for next round
890439ae 72 lastdate="$date";
720d982e 73 laststate="$statenum";
890439ae 74done | tee "$out"
5769d767 75kill $pid_test; sleep 0.2
19a509d1 76echo
5769d767
TM
77echo "Your log: $out"
78$graph && {
79 ./sleepplot.sh "$out" "$graphout" &>/dev/null
80 echo "Your graph: $graphout"
81}
This page took 0.197369 seconds and 4 git commands to generate.