init.lua 1.1 KB

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