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 for k,v in pairs(sensor) do
170 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
171 submitValue(sensor_type,sensor_id,k,v)
173 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
177 elseif name=="type" then
179 elseif name=="id" then
181 elseif name=="param" then
184 return '{'..name..'}'
187 mqtt_client:publish(mqtt_path,v)
190 elseif msg_type=="ALARM" then
191 printLog("ALARM: "..msg_body)
192 sens = splitStr(msg_body,",")
196 sensor_id = web_devid
198 for i,rec in ipairs(sens) do
199 recrd=splitStr(rec,'=')
205 elseif key=="ID" then
211 mqtt_path=string.gsub(mqtt_alarm_topic,"{(.-)}",
215 elseif name=="type" then
217 elseif name=="id" then
220 return '{'..name..'}'
223 mqtt_client:publish(mqtt_path,msg_body)
227 " \""..string.gsub(alarm_type,"\"","\\\"")..
228 "\" \""..string.gsub(alarm_id,"\"","\\\"")..
229 "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
239 MQTT = require "paho.mqtt"
240 mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
242 mqtt_client:auth(mqtt_user, mqtt_passwd)
244 mqtt_client:connect(mqtt_id)
245 json = require( "json" )
249 serialin=io.open(serial_port,"r")
250 elseif input_file == "-" then
252 elseif input_file then
253 serialin=io.open(input_file,"r")
254 elseif input_exec then
255 serialin=io.popen(input_exec,"r")
257 printLog("No input selected")