-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbwxpi.plugin
293 lines (261 loc) · 9.29 KB
/
mbwxpi.plugin
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/awk -f
#
# mbwxpi.plugin Meteobridge plugin weatherstation to read wxnow.txt file
#
# Copyright 2021 by Fred T. Trimble (ftt@smtcpa.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Meteobridge user-defined plugin weatherstation to read, parse, convert
# and output weather data contained in a wxnow.txt file as a Meteobridge
# weatherstation. Operation of the plugin is customized by editing the
# accompanying UCI style config file.
#
# Written 25-Mar-2021 by Fred T. Trimble Ver 0.9.8d (ftt@smtcpa.com) and user AlaskaWX on Meteobridge forum
#
# wxnow.txt file format per https://cumuluswiki.org/a/Wxnow.txt
# Feb 01 2009 12:34
# 272/010g006t069r010p030P020h61b10150
# / g t r p P h b
# from http://104.56.43.104/wxnow.txt
# Mar 25 2021 16:16
# 325/004g005t068r000p000P074h085b09934
# plugin timestamp: https://forum.meteohub.de/viewtopic.php?p=10003#p10003
# awk lang reference:
# http://www.math.utah.edu/docs/info/gawk_toc.html
# https://www.gnu.org/software/gawk/manual/html_node/index.html#SEC_Contents
# write timestampped message to logfile ala mb style
function writelog(path, name, message)
{
system("echo '"name" ("strftime("%d.%m.%Y %T",systime())"): "message"' >> "path" 2>/dev/null")
}
# convert altimeter to station pressure
# amb: altermiter in millibars
# sam: station altitude in meters
# returns station pressure in millibars
function alttosta(amb, sam)
{
return (amb * 0.02953 * ((288.0 - 0.0065 * sam) / 288.0) ^ 5.2561) * 33.8639
}
# convert °F to °C
function ftoc (f)
{
return ((f - 32) * 5/9)
}
# main program
BEGIN {
myname = "mbwxpi"
ver = "0.9.8d" # version
exit_code = -1
last_time = 0.0
this_time = 0.0
rain_rate = 0.0
rain_day = 0.0
last_rain_day = 0.0
total_rain = 0.0
last_total_rain = 0.0
month["Jan"] = "01"
month["Feb"] = "02"
month["Mar"] = "03"
month["Apr"] = "04"
month["May"] = "05"
month["Jun"] = "06"
month["Jul"] = "07"
month["Aug"] = "08"
month["Sep"] = "09"
month["Oct"] = "10"
month["Nov"] = "11"
month["Dec"] = "12"
# default values to use if no config file
# the os command used to return the content of the wxnow.txt file
wxnow_cmd = "wget -q -O - 'http://localhost/public/wxnow.txt'"
station_altitude = 0.0
rain_reset_month = 1 # month to reset annual rain, 1 == Jan, 2 == Feb, etc.
logfile = "/tmp/log/meteobridge.log"
pollsleep = 60
writetolog = 0
# read plugin config using openwrt uci
conf = "/tmp/mnt/data/scripts" # location of uci config file
uci = "uci -c "conf" get "myname".@"myname"[0].wxnow_cmd 2>/dev/null"
item = ""
uci | getline item
close(uci)
if(length(item) > 0)
wxnow_cmd = item
uci = "uci -c "conf" get "myname".@"myname"[0].station_altitude 2>/dev/null"
item = ""
uci | getline item
close(uci)
if(length(item) > 0)
station_altitude = item + 0.0
uci = "uci -c "conf" get "myname".@"myname"[0].rain_reset_month 2>/dev/null"
item = ""
uci | getline item
close(uci)
if(length(item) > 0)
rain_reset_month = item + 0.0
uci = "uci -c "conf" get "myname".@"myname"[0].total_rain 2>/dev/null"
item = ""
uci | getline item
close(uci)
if(length(item) > 0)
{
total_rain = item + 0.0
last_total_rain = total_rain
}
uci = "uci -c "conf" get "myname".@"myname"[0].logfile 2>/dev/null"
item = ""
uci | getline item
close(uci)
if(length(item) > 0)
logfile = item
uci = "uci -c "conf" get "myname".@"myname"[0].pollsleep 2>/dev/null"
item = ""
uci | getline item
close(uci)
if(length(item) > 0)
pollsleep = item + 0.0
uci = "uci -c "conf" get "myname".@"myname"[0].writetolog 2>/dev/null"
item = ""
uci | getline item
close(uci)
if(length(item) > 0)
writetolog = item + 0.0
if(writetolog > 1)
{
writelog(logfile, myname, "wxnow.txt plugin weather station (version "ver") started.")
writelog(logfile, myname, "polling interval "pollsleep" seconds.")
writelog(logfile, myname, "read wxnow.txt command line: '"wxnow_cmd"'")
}
t = systime()
ssm = strftime("%S", t) + (strftime("%M", t) * 60) + (strftime("%H", t) * 3600) # seconds since midnight
if(pollsleep - ((ssm % pollsleep) > 0))
{
if(writetolog > 1)
writelog(logfile, myname, "inital sleep "pollsleep - (ssm % pollsleep)" seconds.")
exit_code = system("sleep "pollsleep - (ssm % pollsleep)" && exit 27") # initial sleep
}
if(exit_code != 0)
{
do # main plugin infinite loop
{
i = 1
do
{
rc = wxnow_cmd | getline wxnow_raw
if (rc > 0)
wxnow_line[i++] = wxnow_raw
} while (rc > 0)
rcc = close(wxnow_cmd)
if (rc <= 0) # getline error...
if(writetolog > 0)
writelog(logfile, myname, sprintf("error %d, executing os command %s", rcc, wxnow_cmd))
else
{
if(length(wxnow_line[1]) > 0)
{
if(split(wxnow_line[1], datea, /[ :]/) == 5) # regexp to parse wxnow.txt timestamp
this_time = mktime(datea[3]" "month[datea[1]]" "datea[2]" "datea[4]" "datea[5]" 00") # convert timestamp into unix epoch time
else
{
if(writetolog > 0)
writelog(logfile, myname, "error parsing wxnow.txt timestamp :" wxnow_line[1])
continue
}
}
if(this_time > last_time) # is data new?
{
#if it's new, parse...
if(length(wxnow_line[2]) > 0)
{
if(split(wxnow_line[2], val, /[\/,g,t,r,p,P,h,b]/) == 9) # regexp to parse wx data
{
if(writetolog > 2)
{
writelog(logfile, myname, sprintf("wxnow_line[2] == %s", wxnow_line[2]))
for(i in val)
writelog(logfile, myname, sprintf("val[%d] == %s", i, val[i]))
}
# convert data into fractional SI units as req by meteobridge
winddir = val[1] + 0.0 # wind dir, deg
windspd = (val[2] + 0.0) * 4.4704 # wind speed, mph, convert to 0.1 m/s
windgust = (val[3] + 0.0) * 4.4704 # wind gust, mph, convert to 0.1 m/s
temp = ftoc(val[4] + 0.0) * 10 # temp, °F convert to 0.1 °C
# rain_1hr = (val[5] + 0.0) * 2.54 # rain in last hr, 0.01 in, convert to 0.1 mm
# rain_24hr = (val[6] + 0.0) * 2.54 # rain in last 24 hrs, 0.01 in, convert to 0.1 mm
rain_day = (val[7] + 0.0) * 2.54 # rain since midnight, 0.01 in, convert to 0.1 mm
hum = (val[8] == 0? 100: val[8] + 0.0) # hum, % (00 == 100%)
baro = alttosta(val[9] / 10.0, station_altitude) * 10 # convert altimiter to station press, 0.1 millibar
if(rain_day >= last_rain_day)
total_rain += (rain_day - last_rain_day)
else
total_rain += rain_day
if(total_rain > last_total_rain)
rain_rate = ((total_rain - last_total_rain) / ((this_time - last_time) / 3600.0)) # increasing rain rate in 0.1 mm/hr
else
rain_rate = 0.0
# ...and output values to meteobridge via stdout as SI units using source data timestamp
printf(":%s th0 %1.0f %d\n", this_time, temp, hum) # temp 0.1 °C, % hum
printf(":%s thb0 %1.0f %d %1.0f\n", this_time, temp, hum, baro) # temp °C, % hum, baro 0.1 mb
printf(":%s wind0 %d %1.0f %1.0f\n", this_time, winddir, windgust, windspd) # gust, wind speed 0.1 m/s
printf(":%s rain0 %1.0f %1.0f\n", this_time, rain_rate, total_rain) # rain rate, total rain, 0.1 mm
if(writetolog > 2)
writelog(logfile, myname, "output sensor data to meteobridge with timestamp: "this_time)
if(total_rain > last_total_rain)
{
# save total rain to uci config file
uci = "uci -c "conf" set "myname".@"myname"[0].total_rain='"sprintf("%1.0f", total_rain)"' 2>/dev/null"
system(uci)
uci = "uci -c "conf" commit "myname" 2>/dev/null"
system(uci)
}
last_rain_day = rain_day
last_total_rain = total_rain
last_time = this_time
delete val
}
else
{
if(writetolog > 0)
writelog(logfile, myname, "error parsing wxnow.txt weather data: "wxnow_line[2])
continue
}
}
} # if
} # else
system("") # flush all i/o buffers
wxnow_raw = ""
delete wxnow_line
t = systime()
# reset total rain within 2x polling period of midnight
if(total_rain > 0 && strftime("%m", t) + 0.0 == rain_reset_month + 0.0 \
&& strftime("%d", t) + 0.0 == 1.0 \
&& strftime("%H", t) + 0.0 == 0.0 \
&& strftime("%M", t) * 60 + 0.0 <= pollsleep * 2 + 0.0)
{
total_rain = 0
last_total_rain = -1
uci = "uci -c "conf" set "myname".@"myname"[0].total_rain='"sprintf("%1.0f", total_rain)"' 2>/dev/null"
system(uci)
uci = "uci -c "conf" commit "myname" 2>/dev/null"
system(uci)
if(writetolog > 1)
writelog(logfile, myname, "reset annual rain.")
}
ssm = strftime("%S", t) + (strftime("%M", t) * 60) + (strftime("%H", t) * 3600) # seconds since midnight
exit_code = system("sleep "pollsleep - (ssm % pollsleep)" && exit 27") # polling sleep
} while(exit_code != 0)
}
}