diff options
Diffstat (limited to 'diller/main.lua')
-rw-r--r-- | diller/main.lua | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/diller/main.lua b/diller/main.lua new file mode 100644 index 0000000..112284c --- /dev/null +++ b/diller/main.lua @@ -0,0 +1,134 @@ +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 + if args.value then + mq.publish(args.id.."/value", args.value) + end + if args.name then + mq.publish(args.id.."/name", args.name) + end + if args.description then + mq.publish(args.id.."/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 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) + +-- 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 |