aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-10-03 22:56:24 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-10-03 22:56:24 +0200
commit9778f5d43ffca26e3def45f21a163c1a83feb7be (patch)
treefa17cccdeb69a83dfbfa9327bb104f81f7ceb608
parent967254d72ba5e116f3019937660c346d202af7f7 (diff)
downloadesp-playground-9778f5d43ffca26e3def45f21a163c1a83feb7be.tar.gz
esp-playground-9778f5d43ffca26e3def45f21a163c1a83feb7be.tar.bz2
esp-playground-9778f5d43ffca26e3def45f21a163c1a83feb7be.tar.xz
esp-playground-9778f5d43ffca26e3def45f21a163c1a83feb7be.zip
wip
-rw-r--r--mqtt-bridge/inter.lua42
-rw-r--r--mqtt-bridge/main.lua6
-rw-r--r--mqtt-bridge/mq.lua70
-rw-r--r--mqtt-bridge/wlan.lua19
4 files changed, 86 insertions, 51 deletions
diff --git a/mqtt-bridge/inter.lua b/mqtt-bridge/inter.lua
index 7fc9f73..3b117a9 100644
--- a/mqtt-bridge/inter.lua
+++ b/mqtt-bridge/inter.lua
@@ -3,36 +3,42 @@ function inter_parse(line)
line = string.sub(line, 1, string.find(line, "\r"))
- local cmd = nil;
+ local cmd;
local i = string.find(line, ":")
- if i == nil then
+ if not i then
-- print("inter_parse: invalid line: no command, line="..line)
return
end
local cmd = string.sub(line, 1, i - 1)
-- print("inter_parse: cmd="..cmd)
+ local last = i + 1
local args = {}
- last = i+1
- while i ~= nil do
+ while i do
local key
local value
i = string.find(line, " ", last)
-
arg = string.sub(line, last, i)
- print("inter_parse: arg: "..arg)
- j = string.find(arg, "=")
- if j == nil then
- key = arg
- args[key] = true
- -- print("inter_parse: key="..key.."=true")
- else
- key = string.sub(arg, 1, j-1)
- value = string.sub(arg, j+1)
- -- print("inter_parse: key="..key..", value="..value)
+ arg = arg:gsub("%s+", "")
+
+ if #arg > 0 then
+ print("inter_parse: arg: "..arg)
+ j = string.find(arg, "=")
+ if not j then
+ if #arg > 0 then
+ key = arg
+ args[key] = true
+ print("inter_parse: key="..key.."=true")
+ end
+ else
+ key = string.sub(arg, 1, j-1)
+ value = string.sub(arg, j+1)
+ print("inter_parse: key="..key..", value="..value)
+ end
end
- if i == nil then
+
+ if not i then
break
end
last = i + 1
@@ -46,7 +52,7 @@ function inter_on_line(line)
print("line:"..line)
local cmd, args = inter_parse(line)
- if cmd == nil then
+ if not cmd then
return
end
@@ -61,7 +67,5 @@ function inter_init(callback)
-- uart.setup(0, 9600, 8, 0, 1, 0)
cb = callback
- tmr.alarm(0, 1000, 0, function() print("still alive!"); end)
uart.on("data", "\r", inter_on_line, 0)
- print("inter ready: callback.."..tostring(cb))
end
diff --git a/mqtt-bridge/main.lua b/mqtt-bridge/main.lua
index 86d51ab..bd8d971 100644
--- a/mqtt-bridge/main.lua
+++ b/mqtt-bridge/main.lua
@@ -31,6 +31,10 @@ local function read_cfg(name)
end
function main()
+ local timers = {
+ inter = 0,
+ mqtt = 1
+ }
local wlan_ssid = read_cfg("wlan-ssid")
local wlan_password = read_cfg("wlan-password")
@@ -39,7 +43,7 @@ function main()
local client_id = "esp8266-"..wifi.sta.getmac()
require('mq')
- mq_init(client_id)
+ mq_init(timers.mqtt, client_id)
require('inter')
inter_init(on_cmd)
diff --git a/mqtt-bridge/mq.lua b/mqtt-bridge/mq.lua
index 20801e4..dbebe8f 100644
--- a/mqtt-bridge/mq.lua
+++ b/mqtt-bridge/mq.lua
@@ -1,44 +1,70 @@
local m
-local topic
-local connected = false
+local topic, cid
-function mq_connected(con)
- print("MQTT connected, con="..tostring(con))
+local function p(msg)
+ print("MQTT: "..msg)
end
-function mq_disconnected(con)
- print("MQTT disconnected, con="..tostring(con))
- connected = false
+local function mq_connected(con)
+-- p("connected")
end
-function mq_client_connected(con)
- print("connected="..tostring(con))
+local function mq_disconnected(con)
+-- p("disconnected")
+ m:close()
+ m = nil
+end
- connected = true
+local function mq_client_connected(con)
+ p("connected")
local majorVer, minorVer, devVer, chipId, flashId, flashSize, flashMode, flashSpeed = node.info()
- payload = "{version: '"..majorVer.."."..minorVer.."."..devVer.."', chipId:"..chipId..", flashId:"..flashId..", flashSize:"..flashSize..", flashMode:"..flashMode..", flashSpeed:"..flashSpeed.."}"
+ payload = '{"version": "'..majorVer..'.'..minorVer..'.'..devVer..'", "chipId":'..chipId..', "flashId":'..flashId..', "flashSize":'..flashSize..', "flashMode":'..flashMode..', "flashSpeed":'..flashSpeed.."}"
mq_publish("firmware", payload)
end
-function mq_init(client_id)
- print("MQTT: Configuring")
+function mq_on_timer()
+ -- This crashes the module:
+ -- local status = wifi.sta.status()
+ -- p("checking status, status="..status)
- topic = "/esp8266/"..client_id
+ local status = wifi.sta.getip()
+ p("checking status, status="..status)
+
+ if status and status ~= "0.0.0.0" then
+ if not m then
+ p("connecting")
- -- client id, keepalive, username, password
- m = mqtt.Client(client_id, 120)
- m:on("connect", mq_connected)
+ -- client id, keepalive, username, password
+ m = mqtt.Client(cid, 120)
+ m:on("connect", mq_connected)
+ m:on("offline", mq_disconnected)
+
+ -- host, port, secure, auto_reconnect, function(client), ssl=8883
+ m:connect("trygvis.io", 1883, 0, 0, mq_client_connected)
+ end
+ else
+ if m then
+ p("Lost wifi connection, disconnecting")
+ m:close()
+ m = nil
+ end
+ end
+end
+
+function mq_init(timer_id, client_id)
+ p("Configuring")
+
+ topic = "/esp8266/"..client_id
+ cid = client_id
- m:on("offline", mq_disconnected)
- -- host, port, secure, auto_reconnect, function(client), ssl=8883
- m:connect("trygvis.io", 1883, 0, 0, mq_client_connected)
+ tmr.alarm(timer_id, 3 * 1000, 1, mq_on_timer)
- print("MQTT: Configured")
+ p("Configured")
end
function mq_publish(path, payload)
- if connected then
+ if m then
m:publish(topic.."/"..path, payload, 0, 0)
end
end
diff --git a/mqtt-bridge/wlan.lua b/mqtt-bridge/wlan.lua
index 5f45c1b..2341301 100644
--- a/mqtt-bridge/wlan.lua
+++ b/mqtt-bridge/wlan.lua
@@ -4,12 +4,12 @@ local function wait_for_connection(timeout_ms)
local timeout = timeout_ms * 1000
repeat
- local ip = wifi.sta.getip()
+ local status = wifi.sta.status()
- print("ip="..tostring(ip))
+ print("status="..tostring(status))
- if ip then -- and ip ~= "0.0.0.0" then
- return ip
+ if ip == 5 then -- and ip ~= "0.0.0.0" then
+ return true
end
tmr.wdclr()
@@ -24,10 +24,11 @@ function wlan_init(ssid, password)
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid, password)
- local ip = wait_for_connection(10 * 1000)
- if not ip then
- panic("Could not establish wlan connection, restarting")
- end
+-- local ok = wait_for_connection(10 * 1000)
+-- if not ok then
+-- panic("Could not establish wlan connection, restarting")
+-- end
- print("WLAN: Configured, ip="..ip)
+-- local ip = wifi.sta.getip()
+-- print("WLAN: Configured, ip="..ip)
end