function mqtt_encode(str)
if (str) then
str = string.gsub (str, "\n", "")
+ str = string.gsub (str, ":", "-")
str = string.gsub (str, "/", "-")
+ str = string.gsub (str, " ", "_")
end
return str
end
function open_dump()
- f = assert(io.popen ("hcidump --raw"))
- run_command("kill `pgrep hcitool`")
- run_command("hciconfig hci0 down")
- run_command("hciconfig hci0 up")
- f_null = assert(io.popen ("hcitool lescan --duplicates"))
+ run_command("/bin/kill `/usr/bin/pgrep hcidump`")
+ run_command("/bin/kill `/usr/bin/pgrep hcitool`")
+ f = assert(io.popen ("/usr/bin/hcidump --raw"))
+ run_command("/usr/bin/hciconfig hci0 down")
+ run_command("/usr/bin/hciconfig hci0 up")
+ f_null = assert(io.popen ("/usr/bin/hcitool lescan --duplicates"))
return f
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
--- s = s .. '['..k..'] = ' .. dump(v) .. ','
- s = s .. dump(v) .. ','
+ s = s .. '['..k..'] = ' .. dump(v) .. ','
+-- s = s .. dump(v) .. ','
end
return s .. '} '
else
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
+function mqtt_pub(path,value)
+ printLog("Pub "..path.." "..value)
+ return mqtt_client:publish(path,value)
+end
+
function process_packet(packet)
local bytes={}
else
type='unknown'
details=dump(bytes)
- printLog(details)
+-- printLog(details)
end
- if mqtt_client then
- if not mqtt_client.connected then
- mqtt_client:connect(mqtt_id)
- end
+ if not (type=="unknown") then
mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
function (name)
if name=="type" then
return '{'..name..'}'
end
end)
- mqtt_client:publish(mqtt_path,tx)
+
+ if not pcall(mqtt_pub,mqtt_path,tx) then
+ printLog('Reconnecting MQTT...')
+ mqtt_client:connect(mqtt_id)
+ end
+
end
end
end
if mqtt_user then
mqtt_client:auth(mqtt_user, mqtt_passwd)
end
+ mqtt_client:connect(mqtt_id)
end
inp = open_dump()
+
read_loop()