4 socket = require("socket")
6 function startswith(String,Start)
8 return string.sub(String,1,string.len(Start))==Start
14 function url_encode(str)
16 str = string.gsub (str, "\n", "\r\n")
17 str = string.gsub (str, "([^%w %-%_%.%~])",
18 function (c) return string.format ("%%%02X", string.byte(c)) end)
19 str = string.gsub (str, " ", "+")
24 function capture(cmd, raw)
25 local f = assert(io.popen(cmd, 'r'))
26 local s = assert(f:read('*a'))
28 if raw then return s end
29 s = string.gsub(s, '^%s+', '')
30 s = string.gsub(s, '%s+$', '')
31 s = string.gsub(s, '[\n\r]+', ' ')
35 function mqtt_encode(str)
37 str = string.gsub (str, "\n", "")
38 str = string.gsub (str, "/", "-")
43 function getConfig(configname)
45 local uci=require("uci")
46 local cur=uci.cursor()
54 web_url = cur.get(config,"web","url")
55 web_user = cur.get(config,"web","user")
56 web_pass = cur.get(config,"web","password")
57 web_devid = cur.get(config,"web","devid")
59 web_iface = cur.get(config,"web","iface")
63 command = '/sbin/ifconfig '..web_iface..' | grep \'\\<inet\\>\' | sed -n \'1p\' | tr -s \' \' | cut -d \' \' -f3 | cut -d \':\' -f2'
64 f=io.popen(command,'r')
72 io.input("/sys/class/net/"..web_iface.."/address")
74 io.input("/sys/class/net/eth0/address")
77 mac = io.read("*line")
78 mac = mac:gsub(":","")
85 logging = cur.get(config,"logging","enabled")
86 touch_file = cur.get(config,"logging","touch_file")
88 serial_port = cur.get(config,"serial","port")
89 serial_baud = cur.get(config,"serial","baud")
91 input_file = cur.get(config,"input","file")
92 input_exec = cur.get(config,"input","exec")
93 alarm_exec = cur.get(config,"alarm","exec")
97 command = "stty -F "..serial_port.." "..serial_baud
102 mqtt_host = cur.get(config,"mqtt","host")
103 mqtt_port = cur.get(config,"mqtt","port")
104 mqtt_id = cur.get(config,"mqtt","id")
105 mqtt_topic = cur.get(config,"mqtt","topic")
106 mqtt_alarm_topic = cur.get(config,"mqtt","alarm_topic")
108 mqtt_user = cur.get(config,"mqtt","user")
109 mqtt_passwd = cur.get(config,"mqtt","password")
111 if mqtt_host and not mqtt_id then
112 mqtt_id="weather-"..web_devid
115 if mqtt_host and not mqtt_port then
119 if mqtt_host and not mqtt_topic then
120 mqtt_topic = 'weathermon/{dev}/{type}/{id}/{param}'
123 if mqtt_host and not mqtt_alarm_topic then
124 mqtt_alarm_topic = 'alarm/{dev}/{type}/{id}'
131 local file = io.open(touch_file, 'w')
137 socket.select(nil, nil, sec)
140 function splitStr(str,char)
146 pos = str:find(char);
151 res[idx]=str:sub(1,pos-1)
161 function printLog(str)
162 if logging=="on" then
163 capture("logger -t weathermon "..str)
165 elseif logging=="syslog" then
166 capture("logger -t weathermon "..str)
167 elseif logging=="stdout" then
172 function submitValue(type,id,param,val)
174 url = web_url.."?stype="..url_encode(type).."&sid="..url_encode(id).."¶m="..url_encode(param).."&value="..url_encode(val)
179 command = command.." --interface "..ip_addr
183 command = command.." -u "..web_user..":"..web_pass
186 command = command.." \""..url.."\" 2>&1"
188 result = capture(command)
194 function processJson(str)
200 for key,value in pairs(msg) do
202 if key=="model" or key=="device" then
204 elseif key=="id" then
206 elseif key=='time' then
214 if not sensor_id then
215 sensor_id = web_devid
218 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
219 if next(sensor)==nil then
220 sensor["command"]="alarm"
222 for k,v in pairs(sensor) do
223 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = \""..v.."\"")
224 submitValue(sensor_type,sensor_id,k,v)
226 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
229 return mqtt_encode(web_devid)
230 elseif name=="type" then
231 return mqtt_encode(sensor_type)
232 elseif name=="id" then
233 return mqtt_encode(sensor_id)
234 elseif name=="param" then
237 return '{'..name..'}'
240 mqtt_client:publish(mqtt_path,v)
244 printLog("Cannot parse sensor input: "..str)
249 function processLine(str)
251 msg=splitStr(line,':')
252 msg_type=msg[1] or ''
253 msg_body=msg[2] or ''
254 if msg_type=="STATUS" then
255 printLog("Status: "..msg_body)
256 elseif msg_type=="ERROR" then
257 printLog("Error: "..msg_body)
258 elseif msg_type=="SENSOR" then
259 printLog("SENSOR: "..msg_body)
260 sens = splitStr(msg_body,",")
264 sensor_id = web_devid
265 for i,rec in ipairs(sens) do
266 recrd=splitStr(rec,'=')
272 elseif key=="ID" then
279 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
280 for k,v in pairs(sensor) do
281 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
282 submitValue(sensor_type,sensor_id,k,v)
284 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
288 elseif name=="type" then
290 elseif name=="id" then
292 elseif name=="param" then
295 return '{'..name..'}'
298 mqtt_client:publish(mqtt_path,v)
302 printLog("Cannot parse sensor input: "..msg_body)
304 elseif msg_type=="ALARM" then
305 printLog("ALARM: "..msg_body)
306 sens = splitStr(msg_body,",")
310 sensor_id = web_devid
312 for i,rec in ipairs(sens) do
313 recrd=splitStr(rec,'=')
319 elseif key=="ID" then
324 if not (alarm_type==nil or alarm_id==nil or alarm_type=='' or alarm_id=='') then
326 mqtt_path=string.gsub(mqtt_alarm_topic,"{(.-)}",
330 elseif name=="type" then
332 elseif name=="id" then
335 return '{'..name..'}'
338 mqtt_client:publish(mqtt_path,msg_body)
342 " \""..string.gsub(alarm_type,"\"","\\\"")..
343 "\" \""..string.gsub(alarm_id,"\"","\\\"")..
344 "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
348 printLog("Cannot parse alarm input: "..msg_body)
357 MQTT = require "mosquitto"
358 mqtt_client = MQTT.new(mqtt_id)
360 mqtt_client:login_set(mqtt_user, mqtt_passwd)
362 mqtt_client:connect(mqtt_host,mqtt_port)
366 serialin=io.open(serial_port,"r")
367 elseif input_file == "-" then
369 elseif input_file then
370 serialin=io.open(input_file,"r")
371 elseif input_exec then
372 serialin=io.popen(input_exec,"r")
374 printLog("No input selected")
378 line=serialin:read("*l")
382 printLog("Received: "..line);
383 if startswith(line,'{') then