aboutsummaryrefslogtreecommitdiff
path: root/mqtt-bridge
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-10-02 23:52:52 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-10-03 17:15:23 +0200
commit1d8bec95253ee8a88a85cb88be521f28b23504aa (patch)
tree5d61cd1d4f091286a4b423523bbb56b492b8022e /mqtt-bridge
downloadesp-playground-1d8bec95253ee8a88a85cb88be521f28b23504aa.tar.gz
esp-playground-1d8bec95253ee8a88a85cb88be521f28b23504aa.tar.bz2
esp-playground-1d8bec95253ee8a88a85cb88be521f28b23504aa.tar.xz
esp-playground-1d8bec95253ee8a88a85cb88be521f28b23504aa.zip
wip of serial commands to mqtt bridge.
Diffstat (limited to 'mqtt-bridge')
-rw-r--r--mqtt-bridge/init.lua4
-rw-r--r--mqtt-bridge/inter.lua73
-rw-r--r--mqtt-bridge/main.lua22
-rw-r--r--mqtt-bridge/mq.lua34
-rw-r--r--mqtt-bridge/wlan.lua18
5 files changed, 151 insertions, 0 deletions
diff --git a/mqtt-bridge/init.lua b/mqtt-bridge/init.lua
new file mode 100644
index 0000000..0b29341
--- /dev/null
+++ b/mqtt-bridge/init.lua
@@ -0,0 +1,4 @@
+-- Upload this file if you want the bridge to autostart
+uart.setup(0, 115200, 8, 0, 1)
+
+require('main')
diff --git a/mqtt-bridge/inter.lua b/mqtt-bridge/inter.lua
new file mode 100644
index 0000000..eb64a98
--- /dev/null
+++ b/mqtt-bridge/inter.lua
@@ -0,0 +1,73 @@
+function inter_parse(line)
+ print("inter_parse: line="..line)
+
+ line = string.sub(line, 1, string.find(line, "\r"))
+
+ local cmd = nil;
+ local i = string.find(line, ":")
+ if i == nil 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 args = {}
+ last = i+1
+ while i ~= nil 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)
+ end
+ if i == nil then
+ break
+ end
+ last = i + 1
+ end
+
+ return cmd, args
+end
+
+local cb
+-- require('inter'); inter_init();
+-- cmd:abc=123 xyz=qwe woot
+function inter_on_line(line)
+ print("line:"..line)
+
+ local cmd, args = inter_parse(line)
+ if cmd == nil then
+ return
+ end
+
+-- print("cmd="..cmd)
+-- for k, v in pairs(args) do
+-- print(k.."="..tostring(v))
+-- end
+ if cb then
+ cb(cmd, args)
+ 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)
+ 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
new file mode 100644
index 0000000..d8773cb
--- /dev/null
+++ b/mqtt-bridge/main.lua
@@ -0,0 +1,22 @@
+
+function on_cmd(cmd, args)
+ print("on_cmd: "..cmd)
+
+ mq_publish("cmd="..cmd)
+end
+
+function main()
+ require('wlan')
+ wlan_init()
+
+ local client_id = "esp8266-"..wifi.sta.getmac()
+ require('mq')
+ mq_init(client_id)
+
+ require('inter')
+ inter_init(on_cmd)
+
+ print("init done")
+end
+
+main()
diff --git a/mqtt-bridge/mq.lua b/mqtt-bridge/mq.lua
new file mode 100644
index 0000000..291bf01
--- /dev/null
+++ b/mqtt-bridge/mq.lua
@@ -0,0 +1,34 @@
+function mq_connected(con)
+ print("MQTT connected, con="..tostring(con))
+end
+
+function mq_disconnected(con)
+ print("MQTT disconnected, con="..tostring(con))
+end
+
+function mq_client_connected(con)
+ print("connected="..tostring(con))
+end
+
+local m
+local topic
+
+function mq_init(client_id)
+ print("MQTT: Configuring")
+
+ topic = "/esp8266/"..client_id
+
+ -- client id, keepalive, username, password
+ m = mqtt.Client(client_id, 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)
+
+ print("MQTT: Configured")
+end
+
+function mq_publish(payload)
+ m:publish(topic, payload, 0, 0)
+end
diff --git a/mqtt-bridge/wlan.lua b/mqtt-bridge/wlan.lua
new file mode 100644
index 0000000..03fd923
--- /dev/null
+++ b/mqtt-bridge/wlan.lua
@@ -0,0 +1,18 @@
+local function is_connected()
+ local ipAddr = wifi.sta.getip()
+ return ipAddr ~= nil and ipAddr ~= "0.0.0.0"
+end
+
+function wlan_init()
+ print("WLAN: Configuring")
+
+ wifi.setmode(wifi.STATION)
+ wifi.sta.config("", "")
+
+ repeat
+ tmr.wdclr()
+ until (is_connected())
+
+ local ip = wifi.sta.getip()
+ print("WLAN: Configured, ip="..ip)
+end