rename files as sensors location
[vaillant-thermostat] / temp2.sh
1 #!/bin/sh
2
3 # sudo apt-get install jq
4 # ln -s `pwd`/temp2.sh /var/www/html/temp2.sh 
5 # ln -s /dev/shm/temp2.png /var/www/html/temp2.png 
6 # ln -s /dev/shm/temp2-gray.png /var/www/html/temp2-gray.png 
7 #
8 # install as cgi-script and invoke to get png use http://localhost/temp2.sh?gray for kindle grayscale output
9
10 # use as http://localhost/temp2.sh?t=6h to show last 6h instead of default 48h
11
12 INTERVAL=`echo $QUERY_STRING | grep -i 't=[0-9]*h' | sed 's/.*t=\([0-9]*h\).*/\1/'`
13 test -z "$INTERVAL" && INTERVAL=48h
14
15 LINES=0
16 echo $QUERY_STRING | grep -i 'lines=' >/dev/null && LINES=1
17
18
19 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"value\" FROM \"mqtt_consumer\" WHERE time > now() - $INTERVAL AND \"topic\"='stat/boiler/DS18B20/temperature'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/DS18B20-temperature
20 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"value\" FROM \"mqtt_consumer\" WHERE time > now() - $INTERVAL AND \"topic\"='stat/boiler/DHT22/temperature'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/DHT22-temperature
21 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"value\" FROM \"mqtt_consumer\" WHERE time > now() - $INTERVAL AND \"topic\"='stat/boiler/DHT22/humidity'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/DHT22-humidity
22
23 #curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"temperature_C\" FROM \"rtl_433\" WHERE time > now() - $INTERVAL AND \"topic\"='rpi2/rtl_433/influx' AND \"id\" = '5'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/rtl_433-temperature
24 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"temperature_C\" FROM \"rtl_433\" WHERE time > now() - $INTERVAL AND \"topic\"='rpi2/rtl_433/influx' AND \"rid\" = '172'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' | tr -d '\\"' > /dev/shm/rtl_433-vani-temperature
25 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"temperature_C\" FROM \"rtl_433\" WHERE time > now() - $INTERVAL AND \"topic\"='rpi2/rtl_433/influx' AND \"rid\" = '75'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' | tr -d '\\"' > /dev/shm/rtl_433-kupaona-temperature
26 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"temperature_C\" FROM \"rtl_433\" WHERE time > now() - $INTERVAL AND \"topic\"='rpi2/rtl_433/influx' AND \"rid\" = '143'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' | tr -d '\\"' > /dev/shm/rtl_433-kuhinja-temperature
27 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"temperature_C\" FROM \"rtl_433\" WHERE time > now() - $INTERVAL AND \"topic\"='rpi2/rtl_433/influx' AND \"rid\" = '183'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' | tr -d '\\"' > /dev/shm/rtl_433-soba-temperature
28
29
30 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT mean(\"value\"),spread(\"value\") FROM \"mqtt_consumer\" WHERE time > now() - $INTERVAL AND \"topic\"='stat/boiler/DS18B20/temperature'"  | jq '.results[0].series[0].values[0][1,2]' > /dev/shm/temperature.mean.spread
31 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT mean(\"value\"),spread(\"value\") FROM \"mqtt_consumer\" WHERE time > now() - $INTERVAL AND \"topic\"='stat/boiler/DHT22/humidity'"  | jq '.results[0].series[0].values[0][1,2]' > /dev/shm/humidity.mean.spread
32
33 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"humidity\" FROM \"rtl_433\" WHERE time > now() - $INTERVAL AND \"topic\"='rpi2/rtl_433/influx' AND \"id\" = '5'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/rtl_433-humidity
34 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"humidity\",\"channel\" FROM \"rtl_433\" WHERE time > now() - $INTERVAL AND \"topic\"='rpi2/rtl_433/influx' AND \"rid\" = '172'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' | tr -d '\\"' > /dev/shm/rtl_433-vani-humidity
35 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"humidity\",\"channel\" FROM \"rtl_433\" WHERE time > now() - $INTERVAL AND \"topic\"='rpi2/rtl_433/influx' AND \"rid\" = '75'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' | tr -d '\\"' > /dev/shm/rtl_433-kupaona-humidity
36 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"humidity\",\"channel\" FROM \"rtl_433\" WHERE time > now() - $INTERVAL AND \"topic\"='rpi2/rtl_433/influx' AND \"rid\" = '143'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' | tr -d '\\"' > /dev/shm/rtl_433-kuhinja-humidity
37
38 GRAY=`echo $QUERY_STRING | grep -i gray | wc -l`
39
40 tz_secs=`date +%:z | cut -d: -f1 | sed -e 's/^+//' -e 's/$/ * 3600/' | bc`
41
42 cd /home/pi/vaillant-thermostat/
43 gnuplot -e "t_mean=`head -1 /dev/shm/temperature.mean.spread`;t_spread=`tail -1 /dev/shm/temperature.mean.spread`;h_mean=`head -1 /dev/shm/humidity.mean.spread` ; h_spread=`tail -1 /dev/shm/humidity.mean.spread` ; gray=$GRAY ; lines=$LINES ; interval=\"$INTERVAL\" ; tz_secs=$tz_secs" temp2.gnuplot
44
45 if [ "$GRAY" = 1 ] ; then
46         convert /dev/shm/temp2.png -gravity center -extent 600x800 -colorspace Gray /dev/shm/temp2-gray.png
47         GRAY="-gray"
48 else
49         GRAY=""
50 fi
51
52 set > /dev/shm/temp2.cgi.vars
53
54 lines_checked=""
55 test "$LINES" = 1 && lines_checked="checked"
56
57 if echo $HTTP_USER_AGENT | grep Mozilla > /dev/null ; then
58         echo "Content-type: text/html\r\n\r\n"
59         cat << __HTML__
60 <body>
61 <form method="get" action="/temp2.sh" style="position: fixed; top: 0; left: 0">
62 <input type=submit name="t" value="48h">
63 <input type=submit name="t" value="24h">
64 <input type=submit name="t" value="12h">
65 <input type=submit name="t" value="6h">
66 <input type=submit name="t" value="1h">
67 <input type=checkbox name="lines" $lines_checked> <!-- $LINES -->
68 </form>
69 <img src="/temp2.png">
70 </body>
71 __HTML__
72         exit 0
73 fi
74
75 echo "Location: $REQUEST_SCHEME://$SERVER_NAME:$SERVER_PORT/temp2$GRAY.png"
76 echo
77
78 exit 0
79
80 montage /dev/shm/[1-3].png -tile 1x3 -geometry +0+0 /tmp/m1.png
81 convert /tmp/m1.png -gravity center -extent 600x800 /tmp/m1.pgm
82 convert /tmp/m1.png -gravity center -extent 600x800 -colorspace Gray /tmp/m1.gray.png
83 display /tmp/m1.pgm
84
85 echo Location: http://192.168.3.2/m1.gray.png
86 echo
87