aboutsummaryrefslogtreecommitdiff
path: root/mqtt-bridge
diff options
context:
space:
mode:
Diffstat (limited to 'mqtt-bridge')
-rw-r--r--mqtt-bridge/inter.lua13
-rw-r--r--mqtt-bridge/main.lua45
-rw-r--r--mqtt-bridge/mq.lua10
-rw-r--r--mqtt-bridge/wlan.lua34
4 files changed, 47 insertions, 55 deletions
diff --git a/mqtt-bridge/inter.lua b/mqtt-bridge/inter.lua
index 3b117a9..2c6a5ad 100644
--- a/mqtt-bridge/inter.lua
+++ b/mqtt-bridge/inter.lua
@@ -1,4 +1,6 @@
-function inter_parse(line)
+local P = {}
+
+function P.parse(line)
-- print("inter_parse: line="..line)
line = string.sub(line, 1, string.find(line, "\r"))
@@ -51,7 +53,7 @@ local cb
function inter_on_line(line)
print("line:"..line)
- local cmd, args = inter_parse(line)
+ local cmd, args = P.parse(line)
if not cmd then
return
end
@@ -61,11 +63,10 @@ function inter_on_line(line)
end
end
-function inter_init(callback)
- -- uart.setup(id, baud, databits, parity, stopbits, echo)
- -- uart.setup(0, 115200, 8, 0, 1, 0)
- -- uart.setup(0, 9600, 8, 0, 1, 0)
+function P.init(callback)
cb = callback
uart.on("data", "\r", inter_on_line, 0)
end
+
+return P
diff --git a/mqtt-bridge/main.lua b/mqtt-bridge/main.lua
index bd8d971..7a639ba 100644
--- a/mqtt-bridge/main.lua
+++ b/mqtt-bridge/main.lua
@@ -8,13 +8,18 @@ function panic(reason)
end
end
+local P = {}
+
+local inter = require('inter')
+local mq = require('mq')
+
local function on_cmd(cmd, args)
print("on_cmd: "..cmd)
for k, v in pairs(args) do
print(k.."="..tostring(v))
end
- mq_publish("cmd", "cmd="..cmd)
+ mq.publish("cmd", "cmd="..cmd)
end
local function read_cfg(name)
@@ -30,23 +35,41 @@ local function read_cfg(name)
return string.sub(value, 1, -2)
end
-function main()
+local function print_status()
+ print("System Status")
+ print("Uptime : "..tmr.time())
+ print("Heap left: "..node.heap())
+ ip, nm, gw = wifi.sta.getip()
+ print("IP : "..tostring(ip))
+ print("Netmask : "..tostring(nm))
+ print("Gateway : "..tostring(gw))
+end
+
+-- uart.setup(id, baud, databits, parity, stopbits, echo)
+-- uart.setup(0, 115200, 8, 0, 1, 0)
+-- uart.setup(0, 9600, 8, 0, 1, 0)
+
+function P.main()
local timers = {
- inter = 0,
- mqtt = 1
+ status = 0,
+ inter = 1,
+ mqtt = 2
}
+
local wlan_ssid = read_cfg("wlan-ssid")
local wlan_password = read_cfg("wlan-password")
-
- require('wlan')
- wlan_init(wlan_ssid, wlan_password)
+ print("Connecting to SSID: "..wlan_ssid)
+ wifi.setmode(wifi.STATION)
+ wifi.sta.config(wlan_ssid, wlan_password)
local client_id = "esp8266-"..wifi.sta.getmac()
- require('mq')
- mq_init(timers.mqtt, client_id)
+ mq.init(timers.mqtt, client_id)
- require('inter')
- inter_init(on_cmd)
+ inter.init(on_cmd)
+
+ tmr.alarm(timers.status, 10 * 1000, 1, print_status)
print("init done")
end
+
+return P
diff --git a/mqtt-bridge/mq.lua b/mqtt-bridge/mq.lua
index dbebe8f..da77063 100644
--- a/mqtt-bridge/mq.lua
+++ b/mqtt-bridge/mq.lua
@@ -1,3 +1,4 @@
+local P = {}
local m
local topic, cid
@@ -20,7 +21,7 @@ local function mq_client_connected(con)
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.."}"
- mq_publish("firmware", payload)
+ P.publish("firmware", payload)
end
function mq_on_timer()
@@ -29,7 +30,6 @@ function mq_on_timer()
-- p("checking status, status="..status)
local status = wifi.sta.getip()
- p("checking status, status="..status)
if status and status ~= "0.0.0.0" then
if not m then
@@ -52,7 +52,7 @@ function mq_on_timer()
end
end
-function mq_init(timer_id, client_id)
+function P.init(timer_id, client_id)
p("Configuring")
topic = "/esp8266/"..client_id
@@ -63,8 +63,10 @@ function mq_init(timer_id, client_id)
p("Configured")
end
-function mq_publish(path, payload)
+function P.publish(path, payload)
if m then
m:publish(topic.."/"..path, payload, 0, 0)
end
end
+
+return P
diff --git a/mqtt-bridge/wlan.lua b/mqtt-bridge/wlan.lua
deleted file mode 100644
index 2341301..0000000
--- a/mqtt-bridge/wlan.lua
+++ /dev/null
@@ -1,34 +0,0 @@
-local function wait_for_connection(timeout_ms)
- local start = tmr.now()
- local now
- local timeout = timeout_ms * 1000
-
- repeat
- local status = wifi.sta.status()
-
- print("status="..tostring(status))
-
- if ip == 5 then -- and ip ~= "0.0.0.0" then
- return true
- end
-
- tmr.wdclr()
- now = tmr.now()
- until (now - start) > timeout
- return false
-end
-
-function wlan_init(ssid, password)
- print("WLAN: connecting to SSID:"..ssid..", password="..password)
-
- wifi.setmode(wifi.STATION)
- wifi.sta.config(ssid, password)
-
--- local ok = wait_for_connection(10 * 1000)
--- if not ok then
--- panic("Could not establish wlan connection, restarting")
--- end
-
--- local ip = wifi.sta.getip()
--- print("WLAN: Configured, ip="..ip)
-end