init.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. -- init adc
  2. if adc.force_init_mode(adc.INIT_VDD33) then
  3. node.restart()
  4. end
  5. -- init and clear ws2812
  6. ws2812.init()
  7. ws2812.write(string.char(0):rep(3096))
  8. -- init config
  9. dofile('config.lua')
  10. -- init WiFi
  11. -- Tell the chip to connect to the access point
  12. wifi.setmode(conf.wifi.mode)
  13. print('set (mode='..wifi.getmode()..')')
  14. if (conf.wifi.mode == wifi.SOFTAP) or (conf.wifi.mode == wifi.STATIONAP) then
  15. print('AP MAC: ', wifi.ap.getmac())
  16. wifi.ap.config(conf.wifi.ap)
  17. wifi.ap.setip(conf.wifi.apip)
  18. end
  19. if (conf.wifi.mode == wifi.STATION) or (conf.wifi.mode == wifi.STATIONAP) then
  20. print('Client MAC: ', wifi.sta.getmac())
  21. wifi.sta.sethostname(conf.hostname)
  22. wifi.sta.config(conf.wifi.stassid, conf.wifi.stapwd, 1)
  23. end
  24. collectgarbage()
  25. -- show system info
  26. print('chip: ',node.chipid())
  27. print('heap: ',node.heap())
  28. -- Compile server code and remove original .lua files.
  29. -- This only happens the first time afer the .lua files are uploaded.
  30. local compileAndRemoveIfNeeded = function(f)
  31. if file.open(f) then
  32. file.close()
  33. print('Compiling:', f)
  34. node.compile(f)
  35. file.remove(f)
  36. collectgarbage()
  37. end
  38. end
  39. local serverFiles = {
  40. 'httpserver.lua',
  41. 'httpserver-basicauth.lua',
  42. 'httpserver-connection.lua',
  43. 'httpserver-error.lua',
  44. 'httpserver-header.lua',
  45. 'httpserver-request.lua',
  46. 'httpserver-static.lua',
  47. 'httpserver-websocket.lua',
  48. 'file-api.lua'
  49. }
  50. for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
  51. compileAndRemoveIfNeeded = nil
  52. serverFiles = nil
  53. i = nil
  54. f = nil
  55. collectgarbage()
  56. -- pre-compile other lua files
  57. local l, f, s
  58. l = file.list();
  59. for f, s in pairs(l) do
  60. if ((string.sub(f, -4) == '.lua') and (f ~= 'config.lua') and (f ~= 'init.lua')) then
  61. print('Pre-compiling:', f)
  62. node.compile(f)
  63. collectgarbage()
  64. end
  65. end
  66. l = nil
  67. f = nil
  68. s = nil
  69. collectgarbage()
  70. -- check and show STATION mode obtained IP
  71. if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
  72. local joinCounter = 0
  73. local joinMaxAttempts = 5
  74. tmr.alarm(0, 3000, 1, function()
  75. local ip = wifi.sta.getip()
  76. if ip == nil and joinCounter < joinMaxAttempts then
  77. print('Connecting to WiFi Access Point ...')
  78. joinCounter = joinCounter + 1
  79. else
  80. if joinCounter == joinMaxAttempts then
  81. print('Failed to connect to WiFi Access Point.')
  82. print('Fall back to SOFTAP.')
  83. wifi.setmode(wifi.SOFTAP)
  84. wifi.ap.config(conf.wifi.ap)
  85. wifi.ap.setip(conf.wifi.apip)
  86. else
  87. print('IP: ',ip)
  88. mdns.register(conf.hostname, { description="NodeMCU WebIDE", service="http", port=80, location='In your ESP board' })
  89. sntp.sync('pool.ntp.org')
  90. end
  91. tmr.stop(0)
  92. joinCounter = nil
  93. joinMaxAttempts = nil
  94. collectgarbage()
  95. end
  96. end)
  97. end
  98. -- start the nodemcu-httpserver in port 80
  99. if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
  100. dofile("httpserver.lc")(80)
  101. collectgarbage()
  102. end
  103. --dofile('led-stick.lc')()