config={} function config.parse(filename,cb,param) local f=file.open(filename) if (f == nil) then print("Failed to open",filename) return false end while(true) do line=f:readline() if (line == nil) then break end line=line:sub(1,-2) local data={line=line,param=param} if (not line:match('^%s*-')) then k,v,c=line:match('^%s*([^ ]+)%s*=%s*([^ ]+)%s*(--.*)') if (k ~= nil and v~= nil) then data.key=k data.rawvalue=v data.comment=c c=c:sub(3):match("^%s*(.-)%s*$") if ((v:sub(1,1) == "'" or v:sub(1,1) == '"') and v:sub(-1,-1) == v:sub(1,1)) then v=v:sub(2,-2) if (c == '') then c='string' end end data.type=c data.value=v end end if (cb(data) == false) then print("cb failed") return false end if (tmr.wdclr) then tmr.wdclr() end end f:close() print("Success") return true end function config.update_cp(from,tmp,to,changes) local f=file.open(tmp,'w') if (f == nil) then print("failed to open",tmp) return false end local ret=config.parse(from,function(data) local s=data.param if (data.key) then if (s[data.key]) then data.value=s[data.key] end data.rawvalue=(data.type == 'string' or data.type == 'password') and '"'..data.value..'"' or data.value data.line=data.key..'='..tostring(data.rawvalue)..(data.comment ~= '' and ' '..data.comment or '') end return f:write(data.line .. '\n') end,changes) f:close() if (ret) then file.remove(to) ret=file.rename(tmp,to) end return(ret) end function config.move_to_old() file.remove('config_old.lua') return file.rename('config.lua','config_old.lua') end function config.update_template(template,changes) local ret=config.move_to_old() if (ret) then ret=config.update_cp(template,'config.tmp','config.lua',changes) end return ret end function config.update(changes) return config.update_template('config_old.lua',changes) end function config.save() return config.update_template('config_default.lua',_G) end -- Initializing GPIO 0 for configuration reset trigger feature. Pulling IO0 to 3V3 before rebooting will reset the config to config_default. gpio.config( { gpio={0}, dir=gpio.IN, pull=gpio.PULL_DOWN }) dofile "board.lua" dofile "calibration.lua" if (not file.exists('config.lua')) then print("Initializing config.lua from config_default.lua") config.update_cp('config_default.lua','config.tmp','config.lua',{}) end if gpio.read(0) == 1 then print("Resetting config.lua to config_default.lua as per user request! IO0 is pulled to HIGH") print("Backup of (possibly broken) config.lua to config_broken.lua") file.remove('config_broken.lua') file.rename('config.lua','config_broken.lua') config.update_cp('config_default.lua','config.tmp','config.lua',{}) end dofile "config.lua" -- if encwebkey == 1 then encrypted_webkey = true else if encwebkey then encrypted_webkey = false end end if osprint == 1 then enable_osprint = true else if osprint then enable_osprint = false end end if ow18b20 == 1 then ow18b20 = true else if ow18b20 then ow18b20 = false end end if mqttnbld == 1 then mqtt_enabled = true else if mqttnbld then mqtt_enabled = false end end if brkr1cls == 1 then mqtt_broker1_close = true else if brkr1cls then mqtt_broker1_close = false end end if brkr1shrt == 1 then mqtt_broker1_short = true else if brkr1shrt then mqtt_broker1_short = false end end if brker1jsn == 1 then mqtt_broker1_json = true else if brker1jsn then mqtt_broker1_json = false end end if brkr1jsn == 1 then mqtt_broker1_json = true else if brkr1jsn then mqtt_broker1_json = false end end if brkr2cls == 1 then mqtt_broker2_close = true else if brkr2cls then mqtt_broker2_close = false end end if brkr2shrt == 1 then mqtt_broker2_short = true else if brkr2shrt then mqtt_broker2_short = false end end if brkr2json == 1 then mqtt_broker2_json = true else if brkr2json then mqtt_broker2_json = false end end if brkr2jsn == 1 then mqtt_broker2_json = true else if brkr2jsn then mqtt_broker2_json = false end end if battah then rated_batt_capacity = battah end if solarwp then solar_module_capacity = solarwp end if avrpwrcnsmptn then average_power_consumption = avrpwrcnsmptn end if wmode then wlanmode = wmode end if stssid then sta_ssid = stssid end if stpwd then sta_pwd = stpwd end if sthstnm then sta_hostname = sthstnm end if apssid then ap_ssid = apssid end if appwd then ap_pwd = appwd end if apchn then ap_channel = apchn end if apip then ap_ip = apip end if apnmsk then ap_netmask = apnmsk end if apgw then ap_gateway = apgw end if apdns then ap_dns = apdns end if aphstnm then ap_hostname = aphstnm end if brkr1hst then mqtt_broker1_host = brkr1hst end if brkr1prt then mqtt_broker1_port = brkr1prt end if brkr1chan then mqtt_broker1_channel = brkr1chan end if brkr2hst then mqtt_broker2_host = brkr2hst end if brkr2prt then mqtt_broker2_port = brkr2prt end if brkr2chan then mqtt_broker2_channel = brkr2chan end -- encwebkey = nil encrypted_webkey = false osprint = nil mqttnbld = nil brkr1cls = nil brkr1shrt = nil brker1jsn = nil brkr1jsn = nil brkr2cls = nil brkr2shrt = nil brkr2json = nil brkr2jsn = nil brkr1hst = nil brkr1prt = nil brkr1chan = nil brkr2hst = nil brkr2prt = nil brkr2chan = nil battah = nil solarwp = nil avrpwrcns = nil wmode = nil stssid = nil stpwd = nil sthstnm = nil apssid = nil appwd = nil apchn = nil apip = nil apnmsk = nil apgw = nil apdns = nil aphstnm = nil if (enable_osprint ~= nil) then node.osprint(enable_osprint) end if (timezone ~= nil) then time.settimezone(timezone) end