diff options
Diffstat (limited to 'diller/main.lua')
-rw-r--r-- | diller/main.lua | 80 |
1 files changed, 57 insertions, 23 deletions
diff --git a/diller/main.lua b/diller/main.lua index 533ecc8..dc1032d 100644 --- a/diller/main.lua +++ b/diller/main.lua @@ -11,6 +11,50 @@ 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 + print("Missing SSID and/or password configuration, use 'wlan' to configure") + return + end + + print("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 @@ -22,13 +66,20 @@ local function on_cmd(cmd, args) end if cmd == "reset" then - print("ok") + print("ok reset") panic("Reset requested") elseif cmd == "wlan" then local ssid = args.ssid password = args.password + local reconfigured = false if args.ssid then - wifi.sta.config(args.ssid, args.password, 1) + 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 '')) @@ -48,9 +99,9 @@ local function on_cmd(cmd, args) else property = properties[id] - local value_path = "sensors/"..id.."/value" - local name_path = "sensors/"..id.."/name" - local description_path = "sensors/"..id.."/description" + local value_path = "property/"..id.."/value" + local name_path = "property/"..id.."/name" + local description_path = "property/"..id.."/description" if not property then print("new property: "..id) @@ -87,19 +138,6 @@ local function on_cmd(cmd, args) 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()) @@ -121,11 +159,7 @@ function P.main() 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) + configure_wlan(wlan_ssid, wlan_password) local mac = wifi.sta.getmac() local client_id = "esp8266-"..mac |