aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--.gitignore2
-rw-r--r--README.md53
-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
-rwxr-xr-xupload.sh22
8 files changed, 228 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..67b1caa
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+esptool
+nodemcu-uploader
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ca445c5
--- /dev/null
+++ b/README.md
@@ -0,0 +1,53 @@
+# Installing `esptool`
+
+ $ git clone https://github.com/themadinventor/esptool.git
+ $ cd esptool
+ $ python setup.py install -local
+
+# NodeMCU Uploader
+
+This tool uploads the Lua files. Requires that the device is runnig the Lua firmware.
+
+ $ git clone https://github.com/kmpm/nodemcu-uploader.git
+ $ cd nodemcu-uploader
+
+https://github.com/nodemcu/nodemcu-firmware
+
+# Talking to the Lua firmware
+
+On unix Screen is a useful tool to talk to serial ports. Usage:
+
+ $ screen <device> <baud rate>
+
+ $ screen /dev/ttyUSB0 9600
+
+After starting screen and pressing reset you should get some output similar to this:
+
+ <��������������x�
+
+ NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
+ lua: cannot open init.lua
+ >
+
+The first garbage output is expected and is from the bootloader trying to autodetect the baud rate.
+
+# First Code
+
+Open screen and paste this code:
+
+ -- Get the current IP
+ ip = wifi.sta.getip()
+ -- This should print nil
+ print(ip)
+
+ -- Configure the device
+ wifi.setmode(wifi.STATION)
+ wifi.sta.config("...", "...")
+
+ -- Now check that the IP is assigned. It might take some time so try again.
+ ip = wifi.sta.getip()
+ print(ip)
+
+# References
+
+ * [Lua API](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en)
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
diff --git a/upload.sh b/upload.sh
new file mode 100755
index 0000000..be9e1be
--- /dev/null
+++ b/upload.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+set -e
+dir=$1; shift
+
+basedir=`pwd`
+cmd=()
+paths=()
+for path in `git status --porcelain "$dir"/*.lua|sed -n 's,^.M \(.*\),\1,p'`
+do
+ filename=`basename $path`
+ cmd+=("$path:$filename")
+ paths+=("$path")
+done
+
+if [[ ${#cmd[@]} -gt 0 ]]
+then
+ "$basedir/nodemcu-uploader/nodemcu-uploader.py" upload "${cmd[@]}"
+ git add "${paths[@]}"
+fi
+
+echo "require('main'); main();"