5 local uci=require("uci")
7 local config="weathermon"
9 web_url = cur.get(config,"web","url")
10 web_user = cur.get(config,"web","user")
11 web_pass = cur.get(config,"web","password")
12 web_devid = cur.get(config,"web","devid")
14 web_iface = cur.get(config,"web","iface")
18 command = '/sbin/ifconfig '..web_iface..' | grep \'\\<inet\\>\' | sed -n \'1p\' | tr -s \' \' | cut -d \' \' -f3 | cut -d \':\' -f2'
19 f=io.popen(command,'r')
27 io.input("/sys/class/net/"..web_iface.."/address")
29 io.input("/sys/class/net/eth0/address")
32 mac = io.read("*line")
33 mac = mac:gsub(":","")
40 logging = cur.get(config,"logging","enabled")
42 serial_port = cur.get(config,"serial","port")
43 serial_baud = cur.get(config,"serial","baud")
45 input_file = cur.get(config,"input","file")
46 input_exec = cur.get(config,"input","exec")
47 alarm_exec = cur.get(config,"alarm","exec")
51 command = "stty -F "..serial_port.." "..serial_baud
56 mqtt_host = cur.get(config,"mqtt","host")
57 mqtt_port = cur.get(config,"mqtt","port")
58 mqtt_id = cur.get(config,"mqtt","id")
59 mqtt_topic = cur.get(config,"mqtt","topic")
60 mqtt_alarm_topic = cur.get(config,"mqtt","alarm_topic")
62 mqtt_user = cur.get(config,"mqtt","user")
63 mqtt_passwd = cur.get(config,"mqtt","password")
65 if mqtt_host and not mqtt_id then
66 mqtt_id="weather-"..web_devid
69 if mqtt_host and not mqtt_port then
73 if mqtt_host and not mqtt_topic then
74 mqtt_topic = 'weathermon/{dev}/{type}/{id}/{param}'
77 if mqtt_host and not mqtt_alarm_topic then
78 mqtt_alarm_topic = 'alarm/{dev}/{type}/{id}'
86 socket.select(nil, nil, sec)
89 function splitStr(str,char)
101 res[idx]=str:sub(1,pos-1)
111 function printLog(str)
113 if logging=="on" then
114 os.execute("logger -t weathermon "..str)
118 function submitValue(type,id,param,val)
120 url = web_url.."?stype="..type.."&sid="..id.."¶m="..param.."&value="..val
125 command = command.." --interface "..ip_addr
129 command = command.." -u "..web_user..":"..web_pass
132 command = command.." \""..url.."\""
139 function processLine(str)
141 msg=splitStr(line,':')
142 msg_type=msg[1] or ''
143 msg_body=msg[2] or ''
144 if msg_type=="STATUS" then
145 printLog("Status: "..msg_body)
146 elseif msg_type=="ERROR" then
147 printLog("Error: "..msg_body)
148 elseif msg_type=="SENSOR" then
149 printLog("SENSOR: "..msg_body)
150 sens = splitStr(msg_body,",")
154 sensor_id = web_devid
155 for i,rec in ipairs(sens) do
156 recrd=splitStr(rec,'=')
162 elseif key=="ID" then
169 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
170 for k,v in pairs(sensor) do
171 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
172 submitValue(sensor_type,sensor_id,k,v)
174 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
178 elseif name=="type" then
180 elseif name=="id" then
182 elseif name=="param" then
185 return '{'..name..'}'
188 mqtt_client:publish(mqtt_path,v)
192 printLog("Cannot parse sensor input: "..msg_body)
194 elseif msg_type=="ALARM" then
195 printLog("ALARM: "..msg_body)
196 sens = splitStr(msg_body,",")
200 sensor_id = web_devid
202 for i,rec in ipairs(sens) do
203 recrd=splitStr(rec,'=')
209 elseif key=="ID" then
214 if not (alarm_type==nil or alarm_id==nil or alarm_type=='' or alarm_id=='') then
216 mqtt_path=string.gsub(mqtt_alarm_topic,"{(.-)}",
220 elseif name=="type" then
222 elseif name=="id" then
225 return '{'..name..'}'
228 mqtt_client:publish(mqtt_path,msg_body)
232 " \""..string.gsub(alarm_type,"\"","\\\"")..
233 "\" \""..string.gsub(alarm_id,"\"","\\\"")..
234 "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
238 printLog("Cannot parse alarm input: "..msg_body)
247 MQTT = require "paho.mqtt"
248 mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
250 mqtt_client:auth(mqtt_user, mqtt_passwd)
252 mqtt_client:connect(mqtt_id)
256 serialin=io.open(serial_port,"r")
257 elseif input_file == "-" then
259 elseif input_file then
260 serialin=io.open(input_file,"r")
261 elseif input_exec then
262 serialin=io.popen(input_exec,"r")
264 printLog("No input selected")