init.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. -- Begin WiFi configuration
  2. local wifiConfig = {}
  3. -- Possible modes: wifi.STATION : station: join a WiFi network
  4. -- wifi.SOFTAP : access point: create a WiFi network
  5. -- wifi.STATIONAP : both station and access point
  6. wifiConfig.mode = wifi.STATION
  7. if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
  8. wifiConfig.accessPointConfig = {}
  9. wifiConfig.accessPointConfig.ssid = "ESP-"..node.chipid() -- Name of the SSID you want to create
  10. wifiConfig.accessPointConfig.pwd = "ESP-"..node.chipid() -- WiFi password - at least 8 characters
  11. wifiConfig.accessPointIpConfig = {}
  12. wifiConfig.accessPointIpConfig.ip = "192.168.111.1"
  13. wifiConfig.accessPointIpConfig.netmask = "255.255.255.0"
  14. wifiConfig.accessPointIpConfig.gateway = "192.168.111.1"
  15. end
  16. if (wifiConfig.mode == wifi.STATION) or (wifiConfig.mode == wifi.STATIONAP) then
  17. wifiConfig.stationConfig = {}
  18. wifiConfig.stationConfig.ssid = "Internet" -- Name of the WiFi network you want to join
  19. wifiConfig.stationConfig.pwd = "" -- Password for the WiFi network
  20. end
  21. -- Tell the chip to connect to the access point
  22. wifi.setmode(wifiConfig.mode)
  23. --print('set (mode='..wifi.getmode()..')')
  24. if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
  25. print('AP MAC: ',wifi.ap.getmac())
  26. wifi.ap.config(wifiConfig.accessPointConfig)
  27. wifi.ap.setip(wifiConfig.accessPointIpConfig)
  28. end
  29. if (wifiConfig.mode == wifi.STATION) or (wifiConfig.mode == wifi.STATIONAP) then
  30. print('Client MAC: ',wifi.sta.getmac())
  31. wifi.sta.config(wifiConfig.stationConfig.ssid, wifiConfig.stationConfig.pwd, 1)
  32. end
  33. print('chip: ',node.chipid())
  34. print('heap: ',node.heap())
  35. wifiConfig = nil
  36. collectgarbage()
  37. -- End WiFi configuration
  38. -- Compile server code and remove original .lua files.
  39. -- This only happens the first time after server .lua files are uploaded.
  40. local compileAndRemoveIfNeeded = function(f)
  41. if file.open(f) then
  42. file.close()
  43. print('Compiling:', f)
  44. node.compile(f)
  45. file.remove(f)
  46. collectgarbage()
  47. end
  48. end
  49. local serverFiles = {
  50. 'httpserver.lua',
  51. 'httpserver-b64decode.lua',
  52. 'httpserver-basicauth.lua',
  53. 'httpserver-conf.lua',
  54. 'httpserver-connection.lua',
  55. 'httpserver-error.lua',
  56. 'httpserver-header.lua',
  57. 'httpserver-request.lua',
  58. 'httpserver-static.lua',
  59. }
  60. for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
  61. compileAndRemoveIfNeeded = nil
  62. serverFiles = nil
  63. collectgarbage()
  64. -- Function for starting the server.
  65. -- If you compiled the mdns module, then it will register the server with that name.
  66. local startServer = function(ip, hostname)
  67. local serverPort = 80
  68. if (dofile("httpserver.lc")(serverPort)) then
  69. print("nodemcu-httpserver running at:")
  70. print(" http://" .. ip .. ":" .. serverPort)
  71. if (mdns) then
  72. mdns.register(hostname, { description="A tiny server", service="http", port=serverPort, location='Earth' })
  73. print (' http://' .. hostname .. '.local.:' .. serverPort)
  74. end
  75. end
  76. end
  77. if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
  78. -- Connect to the WiFi access point and start server once connected.
  79. -- If the server loses connectivity, server will restart.
  80. wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(args)
  81. print("Connected to WiFi Access Point. Got IP: " .. args["IP"])
  82. startServer(args["IP"], "nodemcu")
  83. wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(args)
  84. print("Lost connectivity! Restarting...")
  85. node.restart()
  86. end)
  87. end)
  88. -- What if after a while (30 seconds) we didn't connect? Restart and keep trying.
  89. local watchdogTimer = tmr.create()
  90. watchdogTimer:register(30000, tmr.ALARM_SINGLE, function (watchdogTimer)
  91. local ip = wifi.sta.getip()
  92. if (not ip) then ip = wifi.ap.getip() end
  93. if ip == nil then
  94. print("No IP after a while. Restarting...")
  95. node.restart()
  96. else
  97. --print("Successfully got IP. Good, no need to restart.")
  98. watchdogTimer:unregister()
  99. end
  100. end)
  101. watchdogTimer:start()
  102. else
  103. startServer(wifi.ap.getip(), "nodemcu")
  104. end