init.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. local 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. print(f)
  19. node.compile(f)
  20. file.remove(f)
  21. end
  22. end
  23. local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua'}
  24. for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
  25. compileAndRemoveIfNeeded = nil
  26. serverFiles = nil
  27. -- Connect to the WiFi access point. Once the device is connected,
  28. -- you may start the HTTP server.
  29. local joincounter = 0
  30. tmr.alarm(0, 3000, 1, function()
  31. if wifi.sta.getip() == nil and joincounter < 5 then
  32. print("Connecting to AP...")
  33. joincounter = joincounter +1
  34. else
  35. tmr.stop(0)
  36. -- print('IP: ',wifi.sta.getip())
  37. -- Uncomment to automatically start the server in port 80
  38. joincounter = nil
  39. collectgarbage()
  40. dofile("httpserver.lc")(80)
  41. end
  42. end)