wifiSetup.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. function broadcastAP()
  2. SETUP = true
  3. wifi.setmode(wifi.STATIONAP)
  4. gpio.mode(4, gpio.OUTPUT)
  5. gpio.write(4, gpio.LOW)
  6. local accessPointConfig = {}
  7. accessPointConfig.ssid = "YoButton-" .. node.chipid()
  8. accessPointConfig.pwd = "yobutton"
  9. accessPointConfig.max = 1
  10. accessPointConfig.auth = wifi.AUTH_WPA2_PSK
  11. wifi.ap.config(accessPointConfig)
  12. wifi.ap.dhcp.start()
  13. debugMsg("Wifi station + access point")
  14. end
  15. function stopBroadcastAP()
  16. tmr.stop(SETUP_INACTIVITY_TIMER)
  17. wifi.setmode(wifi.STATION)
  18. gpio.write(4, gpio.HIGH)
  19. srv:close()
  20. srv = nil
  21. SETUP = false
  22. end
  23. function restartSetupTimeout(millisec)
  24. local ms = millisec or SETUP_TIMEOUT
  25. tmr.unregister(SETUP_INACTIVITY_TIMER)
  26. tmr.alarm(SETUP_INACTIVITY_TIMER, ms, tmr.ALARM_SINGLE, function()
  27. debugMsg("Setup mode timed out")
  28. stopBroadcastAP()
  29. end)
  30. end
  31. function waitForWifiStatus(conn)
  32. tmr.alarm(WIFI_WAIT_TIMER, 1000, 1, function()
  33. if wifi.sta.status() ~= 0 and wifi.sta.status() ~= 1 then
  34. local newStatus = wifi.sta.status()
  35. debugMsg("Wifi status: " .. newStatus)
  36. tmr.stop(WIFI_WAIT_TIMER)
  37. sendIndex(conn)
  38. else
  39. debugMsg ("Waiting for Wifi status, currently " .. wifi.sta.status())
  40. restartSetupTimeout()
  41. end
  42. end)
  43. end
  44. function updateSettings(payload)
  45. if payload then
  46. local ssidIndex = {payload:find("ssid=")}
  47. local passIndex = {payload:find("&pass=")}
  48. local recipientIndex = {payload:find("&recipient=")}
  49. local submitIndex = {payload:find("&Submit=")}
  50. if ssidIndex[1] ~= nil then
  51. local newssid = string.gsub(string.sub(payload, ssidIndex[2]+1, passIndex[1]-1), "+", " ")
  52. local newpassword = string.gsub(string.sub(payload, passIndex[2]+1, recipientIndex[1]-1), "+", " ")
  53. local newrecipient = string.upper(string.sub(payload, recipientIndex[2]+1, submitIndex[1]-1))
  54. debugMsg(newssid)
  55. debugMsg(newpassword)
  56. debugMsg(newrecipient)
  57. -- require SSID name, Yo recipient, valid password length
  58. if newssid == nil or newssid == "" then
  59. return false
  60. end
  61. if string.len(newpassword) > 0 and string.len(newpassword) < 8 then
  62. return false
  63. end
  64. if newrecipient == nil or newrecipient == "" then
  65. return false
  66. end
  67. wifi.sta.config(newssid, newpassword)
  68. file.open("yorecipient.txt", "w+")
  69. file.write(newrecipient)
  70. file.close()
  71. return true
  72. end
  73. else
  74. return false
  75. end
  76. end
  77. function setupServer()
  78. srv = net.createServer(net.TCP)
  79. srv:listen(80, function(conn)
  80. conn:on("receive", function(conn, payload)
  81. debugMsg("request received")
  82. debugMsg(payload)
  83. restartSetupTimeout()
  84. updated = updateSettings(payload)
  85. waitForWifiStatus(conn)
  86. end)
  87. conn:on("sent", function(conn)
  88. conn:close()
  89. if updated and wifi.sta.status() == 5 then
  90. debugMsg("updated and connected")
  91. tmr.alarm(SUCCESS_SETUP_TIMER, 3000, tmr.ALARM_SINGLE, function()
  92. debugMsg("closing AP")
  93. stopBroadcastAP()
  94. end)
  95. end
  96. end)
  97. end)
  98. end
  99. function sendIndex(conn)
  100. local statusMessages = {}
  101. statusMessages[0] = 'not enabled'
  102. statusMessages[1] = 'connecting'
  103. statusMessages[2] = 'wrong password'
  104. statusMessages[3] = 'network not found'
  105. statusMessages[4] = 'connection fail'
  106. statusMessages[5] = 'connected'
  107. local ssid = wifi.sta.getconfig()
  108. local status = statusMessages[wifi.sta.status()]
  109. file.open("yorecipient.txt", "r")
  110. local recipient = string.gsub(file.readline(), "\n", "", 1)
  111. file.close()
  112. file.open('index.html')
  113. local indexhtml = file.read()
  114. file.close()
  115. indexhtml = string.gsub(indexhtml, "_S_", ssid)
  116. indexhtml = string.gsub(indexhtml, "_T_", status)
  117. indexhtml = string.gsub(indexhtml, "_R_", recipient)
  118. conn:send(indexhtml)
  119. end
  120. function setupMode()
  121. local srv = nil
  122. if SETUP ~= true then
  123. restartSetupTimeout()
  124. broadcastAP()
  125. setupServer()
  126. end
  127. end
  128. setupMode()