init.lua 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. -- Tel--l the chip to connect to the access point
  2. --wifi.setmode(wifi.STATIONAP)
  3. wifi.setmode(wifi.STATION)
  4. print('set (mode='..wifi.getmode()..')')
  5. print('MAC: ',wifi.sta.getmac())
  6. print('chip: ',node.chipid())
  7. print('heap: ',node.heap())
  8. cfg={}
  9. cfg.ssid="ESP-"..node.chipid()
  10. cfg.pwd="ESP-"..node.chipid()
  11. wifi.ap.config(cfg)
  12. cfg = nil
  13. -- Compile server code and remove original .lua files.
  14. -- This only happens the first time afer the .lua files are uploaded.
  15. local compileAndRemoveIfNeeded = function(f)
  16. if file.open(f) then
  17. file.close()
  18. node.compile(f)
  19. file.remove(f)
  20. end
  21. end
  22. local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua'}
  23. for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
  24. compileAndRemoveIfNeeded = nil
  25. serverFiles = nil
  26. -- Connect to the WiFi access point. Once the device is connected,
  27. -- you may start the HTTP server.
  28. local joincounter = 0
  29. tmr.alarm(0, 3000, 1, function()
  30. if wifi.sta.getip() == nil and joincounter < 5 then
  31. print("Connecting to AP...")
  32. joincounter = joincounter +1
  33. else
  34. tmr.stop(0)
  35. -- print('IP: ',wifi.sta.getip())
  36. -- Uncomment to automatically start the server in port 80
  37. joincounter = nil
  38. collectgarbage()
  39. dofile("httpserver.lc")(80)
  40. end
  41. end)