docs
[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 &
43f096b6
TM
20
21touch "$out"
22bash ./sleepstats.sh "$out" &>/dev/null &
86dfe893 23
890439ae 24tresh=10
720d982e 25lastdate="$(date +%s)"
f51cf844 26screen=false
5769d767
TM
27graph=false
28
3afb9fca
H
29export LC_ALL=C
30
43f096b6
TM
31#trap 'kill -9 $(jobs -p);' SIGINT
32
5769d767 33while getopts "sg" OPT; do
f51cf844 34 test "$OPT" == 's' && screen=true;
5769d767 35 test "$OPT" == 'g' && graph=true;
f51cf844 36done
5769d767
TM
37
38echo "Writing log to: $out";
39$graph && echo "Writing graph to: $graphout"
40echo
19a509d1 41arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do
890439ae
TM
42 date="$(date +%s)"
43 time="$(echo "$line" | cut -f 1)"
44 level="$(echo "$line" | cut -f 2)"
720d982e
TM
45 test "$level" -gt "$tresh" && state=false || state=true
46 $state && statenum=1 || statenum=0;
47 $state && statename='MOTION!' || statename='Nothing';
48
3afb9fca 49 printf "%.2f\t %s %s %d" "$time" "$date" "$(date '+%F %T')" "$statenum"
720d982e 50
720d982e 51 #Debug
f321d355 52 after=$(( $date - $lastdate))
3afb9fca 53 printf " (%s %3d After %4d secs)\n" "$statename" "$level" "$after";
720d982e
TM
54
55 #Fun with values
56 $state && {
f1a48c15 57 $screen && xset dpms force off || true;
890439ae 58 } || {
f1a48c15 59 $screen && xset dpms force on;
890439ae 60 }
5769d767 61 $graph && ./sleepplot.sh "$out" "$graphout" &>/dev/null &
720d982e
TM
62
63 #Prepare invariants for next round
890439ae
TM
64 lastdate="$date";
65done | tee "$out"
43f096b6 66kill $(jobs -p); sleep 0.2
19a509d1 67echo
5769d767
TM
68echo "Your log: $out"
69$graph && {
70 ./sleepplot.sh "$out" "$graphout" &>/dev/null
71 echo "Your graph: $graphout"
72}
This page took 0.253166 seconds and 4 git commands to generate.