|
|
@@ -1,6 +1,7 @@
|
|
|
-function startSetup()
|
|
|
+function broadcastAP()
|
|
|
+ SETUP = true
|
|
|
wifi.sta.disconnect()
|
|
|
- print("Wifi disconnected")
|
|
|
+ debugMsg("WiFi disconnected")
|
|
|
wifi.setmode(wifi.STATIONAP)
|
|
|
stationConfig = {}
|
|
|
stationConfig.ssid = "YoButtonSetup"
|
|
|
@@ -8,33 +9,34 @@ function startSetup()
|
|
|
stationConfig.auth = wifi.OPEN
|
|
|
wifi.ap.config(stationConfig)
|
|
|
wifi.ap.dhcp.start()
|
|
|
- print("Wifi station set up")
|
|
|
- setupServerResponses()
|
|
|
+ debugMsg("Wifi station + access point")
|
|
|
end
|
|
|
|
|
|
-function updateWithNewValues(payload)
|
|
|
+function updateWiFiCreds(payload)
|
|
|
ssidIndex = {payload:find("newssid=")}
|
|
|
passIndex = {payload:find("&newpass=")}
|
|
|
recipientIndex = {payload:find("&newrecipient=")}
|
|
|
submitIndex = {payload:find("&Submit=")}
|
|
|
|
|
|
if(ssidIndex[1]~=nil and payload:find("?")~=nil) then
|
|
|
- print(ssidIndex[1]..", "..ssidIndex[2])
|
|
|
- print(passIndex[1]..", "..passIndex[2])
|
|
|
- print(recipientIndex[1]..", "..recipientIndex[2])
|
|
|
- print(submitIndex[1]..", "..submitIndex[2])
|
|
|
+ wifi.setmode(wifi.STATION)
|
|
|
+
|
|
|
+ debugMsg(ssidIndex[1]..", "..ssidIndex[2])
|
|
|
+ debugMsg(passIndex[1]..", "..passIndex[2])
|
|
|
+ debugMsg(recipientIndex[1]..", "..recipientIndex[2])
|
|
|
+ debugMsg(submitIndex[1]..", "..submitIndex[2])
|
|
|
newssid = string.gsub(string.sub(payload, ssidIndex[2]+1, passIndex[1]-1), "+", " ")
|
|
|
newpassword = string.gsub(string.sub(payload, passIndex[2]+1, recipientIndex[1]-1), "+", " ")
|
|
|
newrecipient = string.upper(string.sub(payload, recipientIndex[2]+1, submitIndex[1]-1))
|
|
|
- print(newssid)
|
|
|
- print(newpassword)
|
|
|
- print(newrecipient)
|
|
|
+ debugMsg(newssid)
|
|
|
+ debugMsg(newpassword)
|
|
|
+ debugMsg(newrecipient)
|
|
|
wifi.sta.config(newssid, newpassword)
|
|
|
file.open("yorecipient.txt", "w+")
|
|
|
file.write(newrecipient)
|
|
|
file.close()
|
|
|
end
|
|
|
- setupStarted=0
|
|
|
+ SETUP = false -- currently: attempts to send Yos regardless of connection status, as long as form is submitted
|
|
|
end
|
|
|
|
|
|
function serveFile(c, f)
|
|
|
@@ -49,20 +51,14 @@ function setupServerResponses()
|
|
|
srv=net.createServer(net.TCP)
|
|
|
srv:listen(80,function(conn)
|
|
|
conn:on("receive",function(conn,payload)
|
|
|
- print("request received")
|
|
|
+ debugMsg("request received")
|
|
|
ind = {string.find(payload, "\n")}
|
|
|
if(ind[1] ~= nil)then
|
|
|
payload = string.sub(payload, 1, ind[1])
|
|
|
end
|
|
|
--payload is reduced to the first line
|
|
|
- print(payload)
|
|
|
- --[[requestedFile = string.match(payload, "/(.+) HTTP")
|
|
|
- if file.open(requestedFile, "r") then
|
|
|
- serveFile(conn, requestedFile)
|
|
|
- return
|
|
|
- end
|
|
|
- --]]
|
|
|
- updateWithNewValues(payload)
|
|
|
+ debugMsg(payload)
|
|
|
+ updateWiFiCreds(payload)
|
|
|
|
|
|
file.open("yorecipient.txt", "r")
|
|
|
recipient = string.gsub(file.readline(), "\n", "", 1)
|
|
|
@@ -73,6 +69,7 @@ function setupServerResponses()
|
|
|
if(ip==nil) then
|
|
|
ip="0.0.0.0"
|
|
|
end
|
|
|
+
|
|
|
conn:send("<body><h1>YO Button setup</h1>"
|
|
|
.."Current wifi SSID: <br>"
|
|
|
.."<input type=\"text\" value=\""..ssid .."\" readonly><br>"
|
|
|
@@ -91,8 +88,11 @@ function setupServerResponses()
|
|
|
.."<input type=\"submit\" name=\"Submit\">"
|
|
|
.."</form></body>")
|
|
|
end)
|
|
|
- conn:on("sent",function(conn) conn:close() end)
|
|
|
+ conn:on("sent", function(conn)
|
|
|
+ conn:close()
|
|
|
+ end)
|
|
|
end)
|
|
|
end
|
|
|
|
|
|
-startSetup()
|
|
|
+broadcastAP()
|
|
|
+setupServerResponses()
|