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 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) end 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 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 = { status = 0, inter = 1, mqtt = 2 } local wlan_ssid = read_cfg("wlan-ssid") local wlan_password = read_cfg("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() mq.init(timers.mqtt, client_id) inter.init(on_cmd) tmr.alarm(timers.status, 10 * 1000, 1, print_status) print("init done") end return P