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")
87 serial_port = cur.get(config,"serial","port")
88 serial_baud = cur.get(config,"serial","baud")
90 input_file = cur.get(config,"input","file")
91 input_exec = cur.get(config,"input","exec")
92 alarm_exec = cur.get(config,"alarm","exec")
96 command = "stty -F "..serial_port.." "..serial_baud
101 mqtt_host = cur.get(config,"mqtt","host")
102 mqtt_port = cur.get(config,"mqtt","port")
103 mqtt_id = cur.get(config,"mqtt","id")
104 mqtt_topic = cur.get(config,"mqtt","topic")
105 mqtt_alarm_topic = cur.get(config,"mqtt","alarm_topic")
107 mqtt_user = cur.get(config,"mqtt","user")
108 mqtt_passwd = cur.get(config,"mqtt","password")
110 if mqtt_host and not mqtt_id then
111 mqtt_id="weather-"..web_devid
114 if mqtt_host and not mqtt_port then
118 if mqtt_host and not mqtt_topic then
119 mqtt_topic = 'weathermon/{dev}/{type}/{id}/{param}'
122 if mqtt_host and not mqtt_alarm_topic then
123 mqtt_alarm_topic = 'alarm/{dev}/{type}/{id}'
129 socket.select(nil, nil, sec)
132 function splitStr(str,char)
138 pos = str:find(char);
143 res[idx]=str:sub(1,pos-1)
153 function printLog(str)
154 if logging=="on" then
155 capture("logger -t weathermon "..str)
161 function submitValue(type,id,param,val)
163 url = web_url.."?stype="..url_encode(type).."&sid="..url_encode(id).."¶m="..url_encode(param).."&value="..url_encode(val)
168 command = command.." --interface "..ip_addr
172 command = command.." -u "..web_user..":"..web_pass
175 command = command.." \""..url.."\" 2>&1"
177 result = capture(command)
181 function processJson(str)
187 for key,value in pairs(msg) do
189 if key=="model" or key=="device" then
191 elseif key=="id" then
193 elseif key=='time' then
201 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
202 if next(sensor)==nil then
203 sensor["command"]="alarm"
205 for k,v in pairs(sensor) do
206 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = \""..v.."\"")
207 submitValue(sensor_type,sensor_id,k,v)
209 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
212 return mqtt_encode(web_devid)
213 elseif name=="type" then
214 return mqtt_encode(sensor_type)
215 elseif name=="id" then
216 return mqtt_encode(sensor_id)
217 elseif name=="param" then
220 return '{'..name..'}'
223 mqtt_client:publish(mqtt_path,v)
227 printLog("Cannot parse sensor input: "..msg_body)
232 function processLine(str)
234 msg=splitStr(line,':')
235 msg_type=msg[1] or ''
236 msg_body=msg[2] or ''
237 if msg_type=="STATUS" then
238 printLog("Status: "..msg_body)
239 elseif msg_type=="ERROR" then
240 printLog("Error: "..msg_body)
241 elseif msg_type=="SENSOR" then
242 printLog("SENSOR: "..msg_body)
243 sens = splitStr(msg_body,",")
247 sensor_id = web_devid
248 for i,rec in ipairs(sens) do
249 recrd=splitStr(rec,'=')
255 elseif key=="ID" then
262 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
263 for k,v in pairs(sensor) do
264 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
265 submitValue(sensor_type,sensor_id,k,v)
267 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
271 elseif name=="type" then
273 elseif name=="id" then
275 elseif name=="param" then
278 return '{'..name..'}'
281 mqtt_client:publish(mqtt_path,v)
285 printLog("Cannot parse sensor input: "..msg_body)
287 elseif msg_type=="ALARM" then
288 printLog("ALARM: "..msg_body)
289 sens = splitStr(msg_body,",")
293 sensor_id = web_devid
295 for i,rec in ipairs(sens) do
296 recrd=splitStr(rec,'=')
302 elseif key=="ID" then
307 if not (alarm_type==nil or alarm_id==nil or alarm_type=='' or alarm_id=='') then
309 mqtt_path=string.gsub(mqtt_alarm_topic,"{(.-)}",
313 elseif name=="type" then
315 elseif name=="id" then
318 return '{'..name..'}'
321 mqtt_client:publish(mqtt_path,msg_body)
325 " \""..string.gsub(alarm_type,"\"","\\\"")..
326 "\" \""..string.gsub(alarm_id,"\"","\\\"")..
327 "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
331 printLog("Cannot parse alarm input: "..msg_body)
340 MQTT = require "mosquitto"
341 mqtt_client = MQTT.new(mqtt_id)
343 mqtt_client:login_set(mqtt_user, mqtt_passwd)
345 mqtt_client:connect(mqtt_host,mqtt_port)
349 serialin=io.open(serial_port,"r")
350 elseif input_file == "-" then
352 elseif input_file then
353 serialin=io.open(input_file,"r")
354 elseif input_exec then
355 serialin=io.popen(input_exec,"r")
357 printLog("No input selected")
365 printLog("Received: "..line);
366 if startswith(line,'{') then