wifiSetup.lua 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. function broadcastAP()
  2. SETUP = true
  3. wifi.sta.disconnect()
  4. debugMsg("WiFi disconnected")
  5. wifi.setmode(wifi.STATIONAP)
  6. stationConfig = {}
  7. stationConfig.ssid = "YoButtonSetup"
  8. stationConfig.pwd = "password"
  9. stationConfig.auth = wifi.OPEN
  10. wifi.ap.config(stationConfig)
  11. wifi.ap.dhcp.start()
  12. debugMsg("Wifi station + access point")
  13. end
  14. function updateWiFiCreds(payload)
  15. ssidIndex = {payload:find("newssid=")}
  16. passIndex = {payload:find("&newpass=")}
  17. recipientIndex = {payload:find("&newrecipient=")}
  18. submitIndex = {payload:find("&Submit=")}
  19. if(ssidIndex[1]~=nil and payload:find("?")~=nil) then
  20. wifi.setmode(wifi.STATION)
  21. debugMsg(ssidIndex[1]..", "..ssidIndex[2])
  22. debugMsg(passIndex[1]..", "..passIndex[2])
  23. debugMsg(recipientIndex[1]..", "..recipientIndex[2])
  24. debugMsg(submitIndex[1]..", "..submitIndex[2])
  25. newssid = string.gsub(string.sub(payload, ssidIndex[2]+1, passIndex[1]-1), "+", " ")
  26. newpassword = string.gsub(string.sub(payload, passIndex[2]+1, recipientIndex[1]-1), "+", " ")
  27. newrecipient = string.upper(string.sub(payload, recipientIndex[2]+1, submitIndex[1]-1))
  28. debugMsg(newssid)
  29. debugMsg(newpassword)
  30. debugMsg(newrecipient)
  31. wifi.sta.config(newssid, newpassword)
  32. file.open("yorecipient.txt", "w+")
  33. file.write(newrecipient)
  34. file.close()
  35. end
  36. SETUP = false -- currently: attempts to send Yos regardless of connection status, as long as form is submitted
  37. end
  38. function serveFile(c, f)
  39. end
  40. function setupServerResponses()
  41. if(srv~=nil) then
  42. srv:close()
  43. srv=nil
  44. end
  45. srv=net.createServer(net.TCP)
  46. srv:listen(80,function(conn)
  47. conn:on("receive",function(conn,payload)
  48. debugMsg("request received")
  49. ind = {string.find(payload, "\n")}
  50. if(ind[1] ~= nil)then
  51. payload = string.sub(payload, 1, ind[1])
  52. end
  53. --payload is reduced to the first line
  54. debugMsg(payload)
  55. updateWiFiCreds(payload)
  56. file.open("yorecipient.txt", "r")
  57. recipient = string.gsub(file.readline(), "\n", "", 1)
  58. file.close()
  59. ssid, password = wifi.sta.getconfig()
  60. ip = wifi.sta.getip()
  61. if(ip==nil) then
  62. ip="0.0.0.0"
  63. end
  64. conn:send("<body><h1>YO Button setup</h1>"
  65. .."Current wifi SSID: <br>"
  66. .."<input type=\"text\" value=\""..ssid .."\" readonly><br>"
  67. .."Current wifi password: <br>"
  68. .."<input type=\"text\" value=\""..password .."\" readonly><br>"
  69. .."Yo Button's IP address: <br>"
  70. .."<input type=\"text\" value=\""..ip.."\" readonly><br>"
  71. .."Yo recipient: <br>"
  72. .."<input type=\"text\" value=\""..recipient.."\" readonly><br>"
  73. .."<form>New SSID: <br>"
  74. .."<input type=\"text\" name=\"newssid\" value=\""..ssid .."\"><br>"
  75. .."New password: <br>"
  76. .."<input type=\"text\" name=\"newpass\" value=\""..password .."\"><br>"
  77. .."New recipient: <br>"
  78. .."<input type=\"text\" name=\"newrecipient\" value=\""..recipient.."\"><br>"
  79. .."<input type=\"submit\" name=\"Submit\">"
  80. .."</form></body>")
  81. end)
  82. conn:on("sent", function(conn)
  83. conn:close()
  84. end)
  85. end)
  86. end
  87. broadcastAP()
  88. setupServerResponses()