wifiSetup.lua 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. function broadcastAP()
  2. SETUP = true
  3. gpio.mode(4, gpio.OUTPUT)
  4. gpio.write(4, gpio.LOW)
  5. wifi.sta.disconnect()
  6. debugMsg("WiFi disconnected")
  7. wifi.setmode(wifi.STATIONAP)
  8. stationConfig = {}
  9. stationConfig.ssid = "YoButtonSetup"
  10. stationConfig.pwd = "password"
  11. stationConfig.auth = wifi.OPEN
  12. wifi.ap.config(stationConfig)
  13. wifi.ap.dhcp.start()
  14. debugMsg("Wifi station + access point")
  15. end
  16. function updateWiFiCreds(payload)
  17. ssidIndex = {payload:find("newssid=")}
  18. passIndex = {payload:find("&newpass=")}
  19. recipientIndex = {payload:find("&newrecipient=")}
  20. submitIndex = {payload:find("&Submit=")}
  21. if(ssidIndex[1]~=nil and payload:find("?")~=nil) then
  22. wifi.setmode(wifi.STATION)
  23. debugMsg(ssidIndex[1]..", "..ssidIndex[2])
  24. debugMsg(passIndex[1]..", "..passIndex[2])
  25. debugMsg(recipientIndex[1]..", "..recipientIndex[2])
  26. debugMsg(submitIndex[1]..", "..submitIndex[2])
  27. newssid = string.gsub(string.sub(payload, ssidIndex[2]+1, passIndex[1]-1), "+", " ")
  28. newpassword = string.gsub(string.sub(payload, passIndex[2]+1, recipientIndex[1]-1), "+", " ")
  29. newrecipient = string.upper(string.sub(payload, recipientIndex[2]+1, submitIndex[1]-1))
  30. debugMsg(newssid)
  31. debugMsg(newpassword)
  32. debugMsg(newrecipient)
  33. wifi.sta.config(newssid, newpassword)
  34. file.open("yorecipient.txt", "w+")
  35. file.write(newrecipient)
  36. file.close()
  37. gpio.write(4, gpio.HIGH)
  38. end
  39. SETUP = false -- currently: attempts to send Yos regardless of connection status, as long as form is submitted
  40. end
  41. function setupServerResponses()
  42. if(srv~=nil) then
  43. srv:close()
  44. srv=nil
  45. end
  46. srv=net.createServer(net.TCP)
  47. srv:listen(80,function(conn)
  48. conn:on("receive",function(conn,payload)
  49. debugMsg("request received")
  50. ind = {string.find(payload, "\n")}
  51. if(ind[1] ~= nil)then
  52. payload = string.sub(payload, 1, ind[1])
  53. end
  54. --payload is reduced to the first line
  55. debugMsg(payload)
  56. updateWiFiCreds(payload)
  57. file.open("yorecipient.txt", "r")
  58. recipient = string.gsub(file.readline(), "\n", "", 1)
  59. file.close()
  60. ssid, password = wifi.sta.getconfig()
  61. ip = wifi.sta.getip()
  62. if(ip==nil) then
  63. ip="0.0.0.0"
  64. end
  65. file.open('index.html')
  66. indexhtml = file.read()
  67. file.close()
  68. indexhtml = string.gsub(indexhtml, "SSID_T", ssid)
  69. indexhtml = string.gsub(indexhtml, "PASSWORD_T", password)
  70. indexhtml = string.gsub(indexhtml, "IP_T", ip)
  71. indexhtml = string.gsub(indexhtml, "RECIPIENT_T", recipient)
  72. conn:send(indexhtml)
  73. end)
  74. conn:on("sent", function(conn)
  75. conn:close()
  76. end)
  77. end)
  78. end
  79. broadcastAP()
  80. setupServerResponses()