function panic(reason) print("PANIC: "..reason) -- This will trigger a restart, but not immediately node.restart() end info = function(msg) print("info "..msg) end local P = {} local inter = require('inter') local mq = require('mq') local properties = {} local function read_cfg(name, required) local filename = "cfg-"..name if not file.open(filename, "r") then if required then panic("Could not read configuration file: "..filename) else return nil end end local value = file.readline() file.close() if value == nil or #value == 0 then if required then panic("Empty configuration file: "..filename) else return nil end end return string.sub(value, 1, -2) end local function write_cfg(name, value) local filename = "cfg-"..name file.open(filename, "w+") local ok = file.writeline(value) file.flush() file.close() return ok end local function configure_wlan() local wlan_ssid = read_cfg("wlan-ssid", false) local wlan_password = read_cfg("wlan-password", false) if not wlan_ssid or not wlan_password then info("Missing SSID and/or password configuration, use 'wlan' to configure") return end info("Connecting to SSID: "..wlan_ssid) wifi.setmode(wifi.STATION) wifi.sta.config(wlan_ssid, wlan_password) end local function on_cmd(cmd, args) if not cmd then return end info("on_cmd: '"..cmd.."', #args="..tostring(table.getn(args))) for k, v in pairs(args) do info(k.."="..tostring(v)) end if cmd == "reset" then print("ok reset") panic("Reset requested") elseif cmd == "wlan" then local ssid = args.ssid password = args.password local reconfigured = false if args.ssid then write_cfg("wlan-ssid", args.ssid) end if args.password then write_cfg("wlan-password", args.password) end if args.ssid and args.password then configure_wlan() 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 property = properties[id] local value_path = "property/"..id.."/value" local name_path = "property/"..id.."/name" local description_path = "property/"..id.."/description" if not property then info("new property: "..id) property = {} properties[id] = property mq.subscribe(name_path, function() info('message on '..path) end) mq.subscribe(description_path, function() info('message on '..path) end) end if args.value then mq.publish(value_path, args.value) end if args.name and property.name ~= args.name then info("publishing name") mq.publish(name_path, args.name) property.name = args.name end if args.description and property.description ~= args.description then info("publishing description") mq.publish(description_path, args.description) property.description = 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 print_status() ip, nm, gw = wifi.sta.getip() print("status uptime="..tmr.time().." heap-left="..node.heap().." ip="..tostring(ip).." netmask="..tostring(nm).." 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 } configure_wlan(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) info("init done") end return P