From dcba99769089f3ebccf839f4be6c605da52e64d7 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 4 Oct 2015 13:13:58 +0200 Subject: o More idiomatic Lua code. --- mqtt-bridge/main.lua | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) (limited to 'mqtt-bridge/main.lua') 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 -- cgit v1.2.3