init.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 joincounter = 0
  9. cfg={}
  10. cfg.ssid="ESP-"..node.chipid()
  11. cfg.pwd="ESP-"..node.chipid()
  12. wifi.ap.config(cfg)
  13. cfg = nil
  14. -- Compile server code and remove original .lua files.
  15. -- This only happens the first time afer the .lua files are uploaded.
  16. local compileAndRemoveIfNeeded = function(f)
  17. if file.open(f) then
  18. file.close()
  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','http/ual.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. 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)