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/'..web_devid
77 if mqtt_host and not mqtt_alarm_topic then
78 mqtt_alarm_topic = 'alarm/'..web_devid
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 nil
143 msg_body=msg[2] or nil
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,'=')
158 value=recrd[2] or nil
162 elseif key=="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)
175 mqtt_msg = { type=sensor_type, id=sensor_id, data=mqtt_param }
177 serializedString = json.encode(mqtt_msg)
178 mqtt_client:publish(mqtt_topic,serializedString)
180 elseif msg_type=="ALARM" then
181 printLog("ALARM: "..msg_body)
182 sens = splitStr(msg_body,",")
186 sensor_id = web_devid
188 for i,rec in ipairs(sens) do
189 recrd=splitStr(rec,'=')
191 value=recrd[2] or nil
195 elseif key=="ID" then
198 mqtt_param[key]=value
203 mqtt_msg = { type = alarm_type, id = alarm_id, data = mqtt_param }
204 serializedString = json.encode(mqtt_msg)
205 mqtt_client:publish(mqtt_alarm_topic,serializedString)
209 " \""..string.gsub(alarm_type,"\"","\\\"")..
210 "\" \""..string.gsub(alarm_id,"\"","\\\"")..
211 "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
221 MQTT = require "paho.mqtt"
222 mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
224 mqtt_client:auth(mqtt_user, mqtt_passwd)
226 mqtt_client:connect(mqtt_id)
227 json = require( "json" )
231 serialin=io.open(serial_port,"r")
232 elseif input_file == "-" then
234 elseif input_file then
235 serialin=io.open(input_file,"r")
236 elseif input_exec then
237 serialin=io.popen(input_exec,"r")
239 printLog("No input selected")