init.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. -- init and clear ws2812
  2. ws2812.init()
  3. ws2812.write(string.char(0):rep(3096))
  4. -- Begin WiFi configuration
  5. local wifiConfig = {}
  6. -- wifi.STATION -- station: join a WiFi network
  7. -- wifi.SOFTAP -- access point: create a WiFi network
  8. -- wifi.STATIONAP -- both station and access point
  9. wifiConfig.mode = wifi.STATIONAP -- both station and access point
  10. wifiConfig.accessPointConfig = {}
  11. wifiConfig.accessPointConfig.ssid = "ESP-"..node.chipid() -- Name of the SSID you want to create
  12. wifiConfig.accessPointConfig.pwd = "Pass"..node.chipid() -- WiFi password - at least 8 characters
  13. wifiConfig.accessPointIpConfig = {}
  14. wifiConfig.accessPointIpConfig.ip = "192.168.111.1"
  15. wifiConfig.accessPointIpConfig.netmask = "255.255.255.0"
  16. wifiConfig.accessPointIpConfig.gateway = "0.0.0.0" -- avoid mobile cannot access internet issue
  17. wifiConfig.stationPointConfig = {}
  18. wifiConfig.stationPointConfig.ssid = "YourSSID" -- Name of the WiFi network you want to join
  19. wifiConfig.stationPointConfig.pwd = "PleaseInputYourPasswordHere" -- Password for the WiFi network
  20. -- Tell the chip to connect to the access point
  21. wifi.setmode(wifiConfig.mode)
  22. print('set (mode='..wifi.getmode()..')')
  23. if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
  24. print('AP MAC: ',wifi.ap.getmac())
  25. wifi.ap.config(wifiConfig.accessPointConfig)
  26. wifi.ap.setip(wifiConfig.accessPointIpConfig)
  27. end
  28. if (wifiConfig.mode == wifi.STATION) or (wifiConfig.mode == wifi.STATIONAP) then
  29. print('Client MAC: ',wifi.sta.getmac())
  30. wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd, 1)
  31. end
  32. print('chip: ',node.chipid())
  33. print('heap: ',node.heap())
  34. wifiConfig = nil
  35. collectgarbage()
  36. -- End WiFi configuration
  37. -- Compile server code and remove original .lua files.
  38. -- This only happens the first time afer the .lua files are uploaded.
  39. local compileAndRemoveIfNeeded = function(f)
  40. if file.open(f) then
  41. file.close()
  42. print('Compiling:', f)
  43. node.compile(f)
  44. file.remove(f)
  45. collectgarbage()
  46. end
  47. end
  48. local serverFiles = {
  49. 'httpserver.lua',
  50. 'httpserver-b64decode.lua',
  51. 'httpserver-basicauth.lua',
  52. 'httpserver-connection.lua',
  53. 'httpserver-error.lua',
  54. 'httpserver-header.lua',
  55. 'httpserver-request.lua',
  56. 'httpserver-static.lua',
  57. 'file-api.lua'
  58. }
  59. for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
  60. compileAndRemoveIfNeeded = nil
  61. serverFiles = nil
  62. i = nil
  63. f = nil
  64. collectgarbage()
  65. -- pre compile other lua files
  66. local l, f, s
  67. l = file.list();
  68. for f, s in pairs(l) do
  69. if ((string.sub(f, -4) == '.lua') and (f ~= 'config.lua') and (f ~= 'init.lua')) then
  70. print('Compiling:', f)
  71. node.compile(f)
  72. collectgarbage()
  73. end
  74. end
  75. l = nil
  76. f = nil
  77. s = nil
  78. collectgarbage()
  79. -- Connect to the WiFi access point.
  80. -- Once the device is connected, you may start the HTTP server.
  81. if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
  82. local joinCounter = 0
  83. local joinMaxAttempts = 5
  84. tmr.alarm(0, 3000, 1, function()
  85. local ip = wifi.sta.getip()
  86. if ip == nil and joinCounter < joinMaxAttempts then
  87. print('Connecting to WiFi Access Point ...')
  88. joinCounter = joinCounter +1
  89. else
  90. if joinCounter == joinMaxAttempts then
  91. print('Failed to connect to WiFi Access Point.')
  92. else
  93. print('IP: ',ip)
  94. end
  95. tmr.stop(0)
  96. joinCounter = nil
  97. joinMaxAttempts = nil
  98. collectgarbage()
  99. end
  100. end)
  101. end
  102. -- Uncomment to automatically start the server in port 80
  103. if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
  104. dofile("httpserver.lc")(80)
  105. collectgarbage()
  106. end