diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-03 17:14:41 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-03 17:15:54 +0200 |
commit | 967254d72ba5e116f3019937660c346d202af7f7 (patch) | |
tree | 20a28ea674cdca521ad51eb7df54f475b0aa602a | |
parent | 1d8bec95253ee8a88a85cb88be521f28b23504aa (diff) | |
download | esp-playground-967254d72ba5e116f3019937660c346d202af7f7.tar.gz esp-playground-967254d72ba5e116f3019937660c346d202af7f7.tar.bz2 esp-playground-967254d72ba5e116f3019937660c346d202af7f7.tar.xz esp-playground-967254d72ba5e116f3019937660c346d202af7f7.zip |
wip
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | mqtt-bridge/inter.lua | 16 | ||||
-rw-r--r-- | mqtt-bridge/main.lua | 36 | ||||
-rw-r--r-- | mqtt-bridge/mq.lua | 20 | ||||
-rw-r--r-- | mqtt-bridge/wlan.lua | 35 | ||||
-rwxr-xr-x | upload.sh | 21 |
6 files changed, 92 insertions, 39 deletions
@@ -1,2 +1,5 @@ esptool nodemcu-uploader + +cfg-* +.cookie diff --git a/mqtt-bridge/inter.lua b/mqtt-bridge/inter.lua index eb64a98..7fc9f73 100644 --- a/mqtt-bridge/inter.lua +++ b/mqtt-bridge/inter.lua @@ -1,16 +1,16 @@ function inter_parse(line) - print("inter_parse: line="..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) + -- print("inter_parse: invalid line: no command, line="..line) return end local cmd = string.sub(line, 1, i - 1) - print("inter_parse: cmd="..cmd) + -- print("inter_parse: cmd="..cmd) local args = {} last = i+1 @@ -26,11 +26,11 @@ function inter_parse(line) if j == nil then key = arg args[key] = true - print("inter_parse: key="..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) + -- print("inter_parse: key="..key..", value="..value) end if i == nil then break @@ -42,8 +42,6 @@ function inter_parse(line) end local cb --- require('inter'); inter_init(); --- cmd:abc=123 xyz=qwe woot function inter_on_line(line) print("line:"..line) @@ -52,10 +50,6 @@ function inter_on_line(line) return end --- print("cmd="..cmd) --- for k, v in pairs(args) do --- print(k.."="..tostring(v)) --- end if cb then cb(cmd, args) end diff --git a/mqtt-bridge/main.lua b/mqtt-bridge/main.lua index d8773cb..86d51ab 100644 --- a/mqtt-bridge/main.lua +++ b/mqtt-bridge/main.lua @@ -1,13 +1,41 @@ +function panic(reason) + print("PANIC: "..reason) + -- This will trigger a restart, but not immediately + node.restart() + while true do +-- print("waiting for restart..") +-- tmr.delay(1 * 1000 * 1000) + end +end -function on_cmd(cmd, args) +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) +end - mq_publish("cmd="..cmd) +local function read_cfg(name) + local filename = "cfg-"..name + if not file.open(filename, "r") then + panic("Could not read configuration file: "..filename) + end + local value = file.readline() + file.close() + if value == nil or #value == 0 then + panic("Empty configuration file: "..filename) + end + return string.sub(value, 1, -2) end function main() + local wlan_ssid = read_cfg("wlan-ssid") + local wlan_password = read_cfg("wlan-password") + require('wlan') - wlan_init() + wlan_init(wlan_ssid, wlan_password) local client_id = "esp8266-"..wifi.sta.getmac() require('mq') @@ -18,5 +46,3 @@ function main() print("init done") end - -main() diff --git a/mqtt-bridge/mq.lua b/mqtt-bridge/mq.lua index 291bf01..20801e4 100644 --- a/mqtt-bridge/mq.lua +++ b/mqtt-bridge/mq.lua @@ -1,17 +1,25 @@ +local m +local topic +local connected = false + function mq_connected(con) print("MQTT connected, con="..tostring(con)) end function mq_disconnected(con) print("MQTT disconnected, con="..tostring(con)) + connected = false end function mq_client_connected(con) print("connected="..tostring(con)) -end -local m -local topic + connected = true + + 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) +end function mq_init(client_id) print("MQTT: Configuring") @@ -29,6 +37,8 @@ function mq_init(client_id) print("MQTT: Configured") end -function mq_publish(payload) - m:publish(topic, payload, 0, 0) +function mq_publish(path, payload) + if connected then + m:publish(topic.."/"..path, payload, 0, 0) + end end diff --git a/mqtt-bridge/wlan.lua b/mqtt-bridge/wlan.lua index 03fd923..5f45c1b 100644 --- a/mqtt-bridge/wlan.lua +++ b/mqtt-bridge/wlan.lua @@ -1,18 +1,33 @@ -local function is_connected() - local ipAddr = wifi.sta.getip() - return ipAddr ~= nil and ipAddr ~= "0.0.0.0" +local function wait_for_connection(timeout_ms) + local start = tmr.now() + local now + local timeout = timeout_ms * 1000 + + repeat + local ip = wifi.sta.getip() + + print("ip="..tostring(ip)) + + if ip then -- and ip ~= "0.0.0.0" then + return ip + end + + tmr.wdclr() + now = tmr.now() + until (now - start) > timeout + return false end -function wlan_init() - print("WLAN: Configuring") +function wlan_init(ssid, password) + print("WLAN: connecting to SSID:"..ssid..", password="..password) wifi.setmode(wifi.STATION) - wifi.sta.config("", "") + wifi.sta.config(ssid, password) - repeat - tmr.wdclr() - until (is_connected()) + local ip = wait_for_connection(10 * 1000) + if not ip then + panic("Could not establish wlan connection, restarting") + end - local ip = wifi.sta.getip() print("WLAN: Configured, ip="..ip) end @@ -1,22 +1,27 @@ #!/bin/bash set -e -dir=$1; shift +dir="$1"; shift -basedir=`pwd` +basedir=$(pwd) +cookie=$(cd $dir; echo $(pwd)/.cookie) cmd=() paths=() -for path in `git status --porcelain "$dir"/*.lua|sed -n 's,^.M \(.*\),\1,p'` + +for path in $(ls "$dir"/cfg-* "$dir"/*.lua) do - filename=`basename $path` - cmd+=("$path:$filename") - paths+=("$path") + if [[ $path -nt "$cookie" ]] + then + filename=`basename $path` + cmd+=("$path:$filename") + paths+=("$path") + fi done if [[ ${#cmd[@]} -gt 0 ]] then - "$basedir/nodemcu-uploader/nodemcu-uploader.py" upload "${cmd[@]}" - git add "${paths[@]}" + "$basedir/nodemcu-uploader/nodemcu-uploader.py" --baud 115200 upload "${cmd[@]}" fi +touch "$cookie" echo "require('main'); main();" |