init.lua 3.3 KB

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