function panic(reason) print("PANIC: "..reason) -- This will trigger a restart, but not immediately node.restart() end local P = {} local inter = require('inter') local mq = require('mq') local function on_cmd(cmd, args) if not cmd then return end print("on_cmd: '"..cmd.."', #args="..tostring(table.getn(args))) for k, v in pairs(args) do print(k.."="..tostring(v)) end if cmd == "reset" then print("ok") panic("Reset requested") elseif cmd == "wlan" then local ssid = args.ssid password = args.password if args.ssid then wifi.sta.config(args.ssid, args.password, 1) end ssid, password, bssid_set, bssid = wifi.sta.getconfig() print("ok ssid="..(ssid or '')) elseif cmd == "network" then local ip = args.ip nm = args.netmask gw = args.gateway if args.ip then wifi.sta.setip({ip=ip, netmask=nm, gateway=gw}) end ip, nm, gw = wifi.sta.getip() print("ok ip="..(ip or '').." netmask="..(nm or '').." gateway="..(gw or '')) elseif cmd == "set-property" then local id = args.id if not id then print("fail status=missing-id") else local name_path = id.."/name" mq.subscribe(name_path, function() print('message on '..path) end) local description_path = id.."/description" mq.subscribe(description_path, function() print('message on '..path) end) if args.value then local path = id.."/value" mq.publish(path, args.value) end if args.name then mq.publish(name_path, args.name) end if args.description then mq.publish(description_path, args.description) end end elseif cmd == "publish" then -- print("Publishing, topic="..tostring(cmd.topic)..", payload="..tostring(cmd.payload)) ok, msg = mq.publish(cmd.topic, cmd.payload) if ok then print("ok status="..msg) else print("failed status="..msg) end else print("failed status=unknown-command") end 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 mac = wifi.sta.getmac() local client_id = "esp8266-"..mac mq.init(timers.mqtt, mac, client_id) inter.init(on_cmd) tmr.alarm(timers.status, 10 * 1000, 1, print_status) -- local majorVer, minorVer, devVer, chipId, flashId, flashSize, flashMode, flashSpeed, buildDate = node.info() -- payload = '{"version": "'..majorVer..'.'..minorVer..'.'..devVer..'", "chipId":'..chipId..', "flashId":'..flashId..', "flashSize":'..flashSize..', "flashMode":'..flashMode..', "flashSpeed":'..flashSpeed -- -- if buildDate then -- payload = payload..', "buildDate": "'..buildDate..'"' -- end -- -- if node.info_versions then -- major, minor, dev, buildDate, sdkVersion = node.info_versions() -- payload = payload..'", versions": {"major": '..major..', "minor": '..minor..', "dev": '..dev..', "buildDate": "'..buildDate..'", "sdk": "'..sdkVersion..'"}' -- end -- payload = payload.."}" -- P.publish("firmware", payload) print("init done") end return P