init.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. -- Begin WiFi configuration
  2. local wifiConfig = {}
  3. -- Uncomment the WiFi mode you want.
  4. wifiConfig.mode = wifi.STATION -- station: join a WiFi network
  5. -- wifiConfig.mode = wifi.AP -- access point: create a WiFi network
  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. -- Configure fixed IP address
  11. wifi.sta.setip({ip="10.0.7.111q", netmask="255.255.224.0", gateway="24.55.0.1"})
  12. wifiConfig.stationPointConfig = {}
  13. wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join
  14. wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network
  15. -- Tell the chip to connect to the access point
  16. wifi.setmode(wifiConfig.mode)
  17. print('set (mode='..wifi.getmode()..')')
  18. print('MAC: ',wifi.sta.getmac())
  19. print('chip: ',node.chipid())
  20. print('heap: ',node.heap())
  21. wifi.ap.config(wifiConfig.accessPointConfig)
  22. wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd)
  23. wifiConfig = nil
  24. collectgarbage()
  25. -- End WiFi configuration
  26. -- Compile server code and remove original .lua files.
  27. -- This only happens the first time afer the .lua files are uploaded.
  28. local compileAndRemoveIfNeeded = function(f)
  29. if file.open(f) then
  30. file.close()
  31. print('Compiling:', f)
  32. node.compile(f)
  33. file.remove(f)
  34. collectgarbage()
  35. end
  36. end
  37. 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'}
  38. for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
  39. compileAndRemoveIfNeeded = nil
  40. serverFiles = nil
  41. collectgarbage()
  42. -- Connect to the WiFi access point.
  43. -- Once the device is connected, you may start the HTTP server.
  44. local joinCounter = 0
  45. local joinMaxAttempts = 5
  46. tmr.alarm(0, 3000, 1, function()
  47. local ip = wifi.sta.getip()
  48. if ip == nil and joinCounter < joinMaxAttempts then
  49. print('Connecting to WiFi Access Point ...')
  50. joinCounter = joinCounter +1
  51. else
  52. if joinCounter == joinMaxAttempts then
  53. print('Failed to connect to WiFi Access Point.')
  54. else
  55. print('IP: ',ip)
  56. -- Uncomment to automatically start the server in port 80
  57. --dofile("httpserver.lc")(80)
  58. end
  59. tmr.stop(0)
  60. joinCounter = nil
  61. joinMaxAttempts = nil
  62. collectgarbage()
  63. end
  64. end)