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")
19 io.input("/sys/class/net/"..web_iface.."/address")
21 io.input("/sys/class/net/eth0/address")
24 mac = io.read("*line")
25 mac = mac:gsub(":","")
32 logging = cur.get(config,"logging","enabled")
34 serial_port = cur.get(config,"serial","port")
35 serial_baud = cur.get(config,"serial","baud")
39 command = "stty -F "..serial_port.." "..serial_baud
44 mqtt_host = cur.get(config,"mqtt","host")
45 mqtt_port = cur.get(config,"mqtt","port")
46 mqtt_id = cur.get(config,"mqtt","id")
47 mqtt_topic = cur.get(config,"mqtt","topic")
49 if mqtt_host and not mqtt_id then
50 mqtt_id="weather-"..web_devid
53 if mqtt_host and not mqtt_port then
57 if mqtt_host and not mqtt_topic then
58 mqtt_topic = 'weathermon/'..web_devid
66 socket.select(nil, nil, sec)
69 function splitStr(str,char)
81 res[idx]=str:sub(1,pos-1)
91 function printLog(str)
94 os.execute("logger -t weathermon "..str)
98 function submitValue(type,id,param,val)
100 url = web_url.."?stype="..type.."&sid="..id.."¶m="..param.."&value="..val
105 command = command.." --interface "..web_iface
109 command = command.." -u "..web_user..":"..web_pass
112 command = command.." \""..url.."\""
120 function processLine(str)
122 msg=splitStr(line,':')
123 msg_type=msg[1] or nil
124 msg_body=msg[2] or nil
125 if msg_type=="STATUS" then
126 printLog("Status: "..msg_body)
127 elseif msg_type=="ERROR" then
128 printLog("Error: "..msg_body)
129 elseif msg_type=="SENSOR" then
130 printLog("SENSOR: "..msg_body)
131 sens = splitStr(msg_body,",")
135 sensor_id = web_devid
136 for i,rec in ipairs(sens) do
137 recrd=splitStr(rec,'=')
139 value=recrd[2] or nil
143 elseif key=="ID" then
151 for k,v in pairs(sensor) do
152 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
153 submitValue(sensor_type,sensor_id,k,v)
156 mqtt_msg = { type=sensor_type, data=mqtt_param }
158 serializedString = json.encode(mqtt_msg)
159 mqtt_client:publish(mqtt_topic,serializedString)
168 MQTT = require "paho.mqtt"
169 mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
170 mqtt_client:connect(mqtt_id)
171 json = require( "json" )
174 serialin=io.open(serial_port,"r")