init.lua 3.2 KB

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