init.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.STATION
  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 after server .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 = {
  46. 'httpserver.lua',
  47. 'httpserver-b64decode.lua',
  48. 'httpserver-basicauth.lua',
  49. 'httpserver-conf.lua',
  50. 'httpserver-connection.lua',
  51. 'httpserver-error.lua',
  52. 'httpserver-header.lua',
  53. 'httpserver-request.lua',
  54. 'httpserver-static.lua',
  55. }
  56. for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
  57. compileAndRemoveIfNeeded = nil
  58. serverFiles = nil
  59. collectgarbage()
  60. -- Connect to the WiFi access point.
  61. -- Once the device is connected, start the HTTP server.
  62. startServer = function(ip, hostname)
  63. local serverPort = 80
  64. if (dofile("httpserver.lc")(serverPort)) then
  65. print("nodemcu-httpserver running at:")
  66. print(" http://" .. ip .. ":" .. serverPort)
  67. if (mdns) then
  68. mdns.register(hostname, { description="A tiny server", service="http", port=serverPort, location='Earth' })
  69. print (' http://' .. hostname .. '.local.:' .. serverPort)
  70. end
  71. end
  72. end
  73. if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
  74. local joinCounter = 0
  75. local joinMaxAttempts = 5
  76. tmr.alarm(0, 3000, 1, function()
  77. local ip = wifi.sta.getip()
  78. if (not ip) then ip = wifi.ap.getip() end
  79. if ip == nil and joinCounter < joinMaxAttempts then
  80. print('Connecting to WiFi Access Point ...')
  81. joinCounter = joinCounter + 1
  82. else
  83. if joinCounter == joinMaxAttempts then
  84. print('Failed to connect to WiFi Access Point.')
  85. else
  86. print("IP = " .. ip)
  87. if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
  88. -- Second parameter is for mDNS (aka Zeroconf aka Bonjour) registration.
  89. -- If the mdns module is compiled in the firmware, advertise the server with this name.
  90. -- If no mdns is compiled, then parameter is ignored.
  91. startServer(ip, "nodemcu")
  92. end
  93. end
  94. tmr.stop(0)
  95. joinCounter = nil
  96. joinMaxAttempts = nil
  97. collectgarbage()
  98. end
  99. end)
  100. else
  101. startServer()
  102. end