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 mqtt_encode(str)
26 str = string.gsub (str, "\n", "")
27 str = string.gsub (str, "/", "-")
32 function getConfig(configname)
34 local uci=require("uci")
35 local cur=uci.cursor()
43 web_url = cur.get(config,"web","url")
44 web_user = cur.get(config,"web","user")
45 web_pass = cur.get(config,"web","password")
46 web_devid = cur.get(config,"web","devid")
48 web_iface = cur.get(config,"web","iface")
52 command = '/sbin/ifconfig '..web_iface..' | grep \'\\<inet\\>\' | sed -n \'1p\' | tr -s \' \' | cut -d \' \' -f3 | cut -d \':\' -f2'
53 f=io.popen(command,'r')
61 io.input("/sys/class/net/"..web_iface.."/address")
63 io.input("/sys/class/net/eth0/address")
66 mac = io.read("*line")
67 mac = mac:gsub(":","")
74 logging = cur.get(config,"logging","enabled")
76 serial_port = cur.get(config,"serial","port")
77 serial_baud = cur.get(config,"serial","baud")
79 input_file = cur.get(config,"input","file")
80 input_exec = cur.get(config,"input","exec")
81 alarm_exec = cur.get(config,"alarm","exec")
85 command = "stty -F "..serial_port.." "..serial_baud
90 mqtt_host = cur.get(config,"mqtt","host")
91 mqtt_port = cur.get(config,"mqtt","port")
92 mqtt_id = cur.get(config,"mqtt","id")
93 mqtt_topic = cur.get(config,"mqtt","topic")
94 mqtt_alarm_topic = cur.get(config,"mqtt","alarm_topic")
96 mqtt_user = cur.get(config,"mqtt","user")
97 mqtt_passwd = cur.get(config,"mqtt","password")
99 if mqtt_host and not mqtt_id then
100 mqtt_id="weather-"..web_devid
103 if mqtt_host and not mqtt_port then
107 if mqtt_host and not mqtt_topic then
108 mqtt_topic = 'weathermon/{dev}/{type}/{id}/{param}'
111 if mqtt_host and not mqtt_alarm_topic then
112 mqtt_alarm_topic = 'alarm/{dev}/{type}/{id}'
118 socket.select(nil, nil, sec)
121 function splitStr(str,char)
127 pos = str:find(char);
132 res[idx]=str:sub(1,pos-1)
142 function printLog(str)
143 if logging=="on" then
144 os.execute("logger -t weathermon "..str)
150 function submitValue(type,id,param,val)
152 url = web_url.."?stype="..url_encode(type).."&sid="..url_encode(id).."¶m="..url_encode(param).."&value="..url_encode(val)
157 command = command.." --interface "..ip_addr
161 command = command.." -u "..web_user..":"..web_pass
164 command = command.." \""..url.."\""
170 function processJson(str)
176 for key,value in pairs(msg) do
180 elseif key=="id" then
182 elseif key=='time' then
190 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
191 for k,v in pairs(sensor) do
192 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
193 submitValue(sensor_type,sensor_id,k,v)
195 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
198 return mqtt_encode(web_devid)
199 elseif name=="type" then
200 return mqtt_encode(sensor_type)
201 elseif name=="id" then
202 return mqtt_encode(sensor_id)
203 elseif name=="param" then
206 return '{'..name..'}'
209 mqtt_client:publish(mqtt_path,v)
213 printLog("Cannot parse sensor input: "..msg_body)
218 function processLine(str)
220 msg=splitStr(line,':')
221 msg_type=msg[1] or ''
222 msg_body=msg[2] or ''
223 if msg_type=="STATUS" then
224 printLog("Status: "..msg_body)
225 elseif msg_type=="ERROR" then
226 printLog("Error: "..msg_body)
227 elseif msg_type=="SENSOR" then
228 printLog("SENSOR: "..msg_body)
229 sens = splitStr(msg_body,",")
233 sensor_id = web_devid
234 for i,rec in ipairs(sens) do
235 recrd=splitStr(rec,'=')
241 elseif key=="ID" then
248 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
249 for k,v in pairs(sensor) do
250 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
251 submitValue(sensor_type,sensor_id,k,v)
253 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
257 elseif name=="type" then
259 elseif name=="id" then
261 elseif name=="param" then
264 return '{'..name..'}'
267 mqtt_client:publish(mqtt_path,v)
271 printLog("Cannot parse sensor input: "..msg_body)
273 elseif msg_type=="ALARM" then
274 printLog("ALARM: "..msg_body)
275 sens = splitStr(msg_body,",")
279 sensor_id = web_devid
281 for i,rec in ipairs(sens) do
282 recrd=splitStr(rec,'=')
288 elseif key=="ID" then
293 if not (alarm_type==nil or alarm_id==nil or alarm_type=='' or alarm_id=='') then
295 mqtt_path=string.gsub(mqtt_alarm_topic,"{(.-)}",
299 elseif name=="type" then
301 elseif name=="id" then
304 return '{'..name..'}'
307 mqtt_client:publish(mqtt_path,msg_body)
311 " \""..string.gsub(alarm_type,"\"","\\\"")..
312 "\" \""..string.gsub(alarm_id,"\"","\\\"")..
313 "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
317 printLog("Cannot parse alarm input: "..msg_body)
326 MQTT = require "paho.mqtt"
327 mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
329 mqtt_client:auth(mqtt_user, mqtt_passwd)
331 mqtt_client:connect(mqtt_id)
335 serialin=io.open(serial_port,"r")
336 elseif input_file == "-" then
338 elseif input_file then
339 serialin=io.open(input_file,"r")
340 elseif input_exec then
341 serialin=io.popen(input_exec,"r")
343 printLog("No input selected")
351 printLog("Received: "..line);
352 if startswith(line,'{') then