_init.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. --SAFETRIM
  2. -- function _init(self, args)
  3. local self, args = ...
  4. -- The config is read from config.json but can be overridden by explicitly
  5. -- setting the following args. Setting to "nil" deletes the config arg.
  6. --
  7. -- ssid, spwd Credentials for the WiFi
  8. -- server, port, secret Provisioning server:port and signature secret
  9. -- leave If true then the Wifi is left connected
  10. -- espip, gw, nm, nsserver These need set if you are not using DHCP
  11. local wifi, file, json, tmr = wifi, file, sjson, tmr
  12. local log, sta, config = self.log, wifi.sta, nil
  13. print ("\nStarting Provision Checks")
  14. log("Starting Heap:", node.heap())
  15. if file.open(self.prefix .. "config.json", "r") then
  16. local s; s, config = pcall(json.decode, file.read())
  17. if not s then print("Invalid configuration:", config) end
  18. file.close()
  19. end
  20. if type(config) ~= "table" then config = {} end
  21. for k,v in pairs(args or {}) do config[k] = (v ~= "nil" and v) end
  22. config.id = node.chipid()
  23. config.a = "HI"
  24. self.config = config
  25. self.secret = config.secret
  26. config.secret = nil
  27. log("Config is:",json.encode(self.config))
  28. log("Mode is", wifi.setmode(wifi.STATION, false), config.ssid, config.spwd)
  29. log("Config status is", sta.config(
  30. { ssid = config.ssid, pwd = config.spwd, auto = false, save = false } ))
  31. if config.espip then
  32. log( "Static IP setup:", sta.setip(
  33. { ip = config.espip, gateway = config.gw, netmask = config.nm }))
  34. end
  35. sta.connect(1)
  36. package.loaded[self.modname] = nil
  37. self.modname=nil
  38. self.timer:alarm( 500, tmr.ALARM_AUTO, self:_doTick())
  39. -- end