-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmd
executable file
·265 lines (224 loc) · 5.14 KB
/
gmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash
# Added to GIThub https://github.com/viper0131/gasmeter
# Our lamps:
# (These are wiringPi pin numbers)
green=7
# The input sensors
sensor=2
COUNTER=0
PREV_COUNTER=0
DATADIR="/var/lib/gasmeter"
COUNTERDATA="$DATADIR/counter"
DB_COUNTER="$DATADIR/gm-counter.rrd"
DB_COUNTER="$DATADIR/gm-counter-highres.rrd"
DB_FLOW="$DATADIR/gm-flow.rrd"
DB_FLOW_HIGHRES="$DATADIR/gm-flow-highres.rrd"
LOGFILE="/var/log/gasmeter.log"
TICKDATE=0
PREV_TICKDATE=0
TICK_SIZE=10 # each tick is 10L gas
FLOW=0
FLOW2=0
# setup:
# Program the GPIO correctly and initialise the lamps
#######################################################################
setup ()
{
#set -x
if [ ! -d "$DATADIR" ]; then
mkdir -p $DATADIR
fi
NOW=$(date +%F\ %T)
createRRD
echo "$NOW init: Starting..." >> $LOGFILE
for i in $green ; do gpio mode $i out ; done
for i in $green ; do gpio write $i 0 ; done
gpio mode $sensor in
if [ ! -f $COUNTERDATA ] ; then
COUNTER=0
else
COUNTER=( $(<$COUNTERDATA) )
fi
echo "$NOW init: Counter starts with $COUNTER" >> $LOGFILE
signalSetup
}
# waitSensor:
#######################################################################
waitSensor ()
{
TIMER=0
MAX_TIMER=2200
RESULT=0
# echo -n "Waiting for signal ... "
while [ $TIMER -le $MAX_TIMER ]; do
RESULT=`gpio read $sensor`
if [ $RESULT -eq 1 ]; then
echo "signal received..."
notifyGreen
break
fi
sleep 0.1
let TIMER=TIMER+1
done
return $RESULT
}
signalSetup()
{
gpio write $green 1
sleep 0.2
gpio write $green 0
sleep 0.1
gpio write $green 1
sleep 0.2
gpio write $green 0
sleep 0.1
gpio write $green 1
sleep 0.2
gpio write $green 0
}
blinkGreen()
{
gpio write $green 1
sleep 0.5
gpio write $green 0
}
notifyGreen()
{
while [ `gpio read $sensor` = 1 ]; do
gpio write $green 1
sleep 0.1
done
gpio write $green 0
}
calcFlow()
{
TICKDATE=$(date +%s)
if [ "$PREV_TICKDATE" -ne 0 ]; then
let DELTA=TICKDATE-PREV_TICKDATE
let DELTA_COUNT=COUNTER-PREV_COUNTER
#calc flow in liters/sec
e="$DELTA_COUNT * $TICK_SIZE / $DELTA"
FLOW=$(float_eval "$e")
#kuub / hour
e="$DELTA_COUNT * $TICK_SIZE / $DELTA * 3.6"
FLOW2=$(float_eval "$e")
#echo "flow (l/s): $FLOW"
#echo "flow (k/h): $FLOW2"
fi
PREV_TICKDATE=$TICKDATE
PREV_COUNTER=$COUNTER
}
doSomething()
{
NOW=$(date +%F\ %T)
FLOW2=0
if [ $1 -eq 1 ]; then
let COUNTER=COUNTER+1
calcFlow
fi
e="$COUNTER / 100"
value=$(float_eval "$e")
echo "$NOW, $value" >> $LOGFILE
echo "$NOW, $value"
echo $COUNTER > $COUNTERDATA
updateRRD $value $FLOW2
}
# RRD Tools
#####################################################################
createRRD ()
{
START=`date --date="\`date +%a\ %b\ %e\ 00:00\`" +%s`
if [ ! -f $DB_FLOW_HIGHRES ]; then
echo "Creating $DB_FLOW_HIGHRES..."
rrdtool create $DB_FLOW_HIGHRES \
--start $START \
--step 10 \
DS:flow:GAUGE:60:0:6 \
RRA:AVERAGE:0.75:1:535680 \
RRA:AVERAGE:0.75:12:535680 \
RRA:AVERAGE:0.75:288:535680 \
RRA:LAST:0.75:1:1488 \
RRA:MAX:0.75:12:1488
fi
if [ ! -f $DB_FLOW ]; then
echo "Creating $DB_FLOW..."
rrdtool create $DB_FLOW \
--start $START \
--step 300 \
DS:flow:GAUGE:600:0:6 \
RRA:AVERAGE:0.75:1:17856 \
RRA:AVERAGE:0.75:12:17856 \
RRA:AVERAGE:0.75:288:17856 \
RRA:MAX:0.75:12:1488
fi
if [ ! -f $DB_COUNTER_HIGHRES ]; then
echo "Creating $DB_COUNTER_HIGHRES..."
rrdtool create $DB_COUNTER_HIGHRES \
--start $START \
--step 10 \
DS:counter:GAUGE:60:0:99999999 \
RRA:LAST:0.75:1:535680 \
RRA:LAST:0.75:12:535680 \
RRA:LAST:0.75:288:535680
fi
if [ ! -f $DB_COUNTER ]; then
echo "Creating $DB_COUNTER..."
rrdtool create $DB_COUNTER \
--start $START \
--step 300 \
DS:counter:GAUGE:600:0:99999999 \
RRA:LAST:0.75:1:17856 \
RRA:LAST:0.75:12:17856 \
RRA:LAST:0.75:288:17856
fi
}
updateRRD()
{
echo "update RRD ($1, $2)"
rrdtool update $DB_COUNTER N:$1
rrdtool update $DB_COUNTER_HIGHRES N:$1
if [[ $2 > 0 ]]; then
rrdtool update $DB_FLOW N:$2
rrdtool update $DB_FLOW_HIGHRES N:$2
fi
}
# Floating point number functions.
#####################################################################
# Default scale used by float functions.
float_scale=2
function float_eval()
{
local stat=0
local result=0.0
if [[ $# -gt 0 ]]; then
result=$(echo "scale=$float_scale; $*" | bc -q 2>/dev/null)
stat=$?
if [[ $stat -eq 0 && -z "$result" ]]; then stat=1; fi
fi
echo $result
return $stat
}
#######################################################################
# The main program
# Call our setup routing once, then sit in a loop, waiting for
# the button to be pressed then executing the sequence.
#######################################################################
setup
#calcFlow
#sleep 1
#calcFlow
#sleep 3
#let COUNTER=COUNTER+100
#calcFlow
while true; do
waitSensor
if [ "$?" -eq 1 ]; then
#signal detected
doSomething 1
else
#no signal detected, timeout
echo "Timeout waiting for signal"
doSomething 0
fi
sleep 0.2
done