wifiSetup.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 or wifi.sta.status() == 1 then
  34. debugMsg ("Waiting for Wifi status, currently " .. wifi.sta.status())
  35. restartSetupTimeout()
  36. else
  37. debugMsg("Wifi status: " .. wifi.sta.status())
  38. tmr.stop(WIFI_WAIT_TIMER)
  39. sendIndex(conn)
  40. end
  41. end)
  42. end
  43. function updateSettings(payload)
  44. if payload then
  45. local ssidIndex = {payload:find("s=")}
  46. local passIndex = {payload:find("&p=")}
  47. local recipientIndex = {payload:find("&r=")}
  48. local submitIndex = {payload:find("&s=")}
  49. if ssidIndex[1] ~= nil then
  50. local newssid = string.gsub(string.sub(payload, ssidIndex[2]+1, passIndex[1]-1), "+", " ")
  51. local newpassword = string.gsub(string.sub(payload, passIndex[2]+1, recipientIndex[1]-1), "+", " ")
  52. local newrecipient = string.upper(string.sub(payload, recipientIndex[2]+1, submitIndex[1]-1))
  53. debugMsg(newssid)
  54. debugMsg(newpassword)
  55. debugMsg(newrecipient)
  56. -- require SSID name, Yo recipient, valid password length
  57. if newssid == nil or newssid == "" then
  58. return false
  59. end
  60. if string.len(newpassword) > 0 and string.len(newpassword) < 8 then
  61. return false
  62. end
  63. if newrecipient == nil or newrecipient == "" then
  64. return false
  65. end
  66. debugMsg("updating settings")
  67. wifi.sta.config(newssid, newpassword)
  68. YO_RECIPIENT = newrecipient
  69. file.open('yorecipient.txt', "w+")
  70. file.write(newrecipient)
  71. file.close()
  72. return true
  73. end
  74. else
  75. return false
  76. end
  77. end
  78. function setupServer()
  79. local updated
  80. srv = net.createServer(net.TCP, 60)
  81. srv:listen(80, function(conn)
  82. conn:on("receive", function(conn, payload)
  83. debugMsg("request received")
  84. debugMsg(payload)
  85. restartSetupTimeout()
  86. updated = updateSettings(payload)
  87. waitForWifiStatus(conn)
  88. end)
  89. conn:on("sent", function(conn)
  90. conn:close()
  91. debugMsg("sent: " .. tostring(updated) .. ' ' .. wifi.sta.status())
  92. if updated and wifi.sta.status() == 5 then
  93. debugMsg("updated and connected")
  94. tmr.alarm(SUCCESS_SETUP_TIMER, 1000, tmr.ALARM_SINGLE, function()
  95. debugMsg("closing AP")
  96. stopBroadcastAP()
  97. end)
  98. end
  99. end)
  100. end)
  101. end
  102. function sendIndex(conn)
  103. local statusMessages = {}
  104. statusMessages[0] = 'not enabled'
  105. statusMessages[1] = 'connecting'
  106. statusMessages[2] = 'wrong password'
  107. statusMessages[3] = 'network not found'
  108. statusMessages[4] = 'connection fail'
  109. statusMessages[5] = 'connected'
  110. debugMsg('preparing indexhtml')
  111. local ssid = wifi.sta.getconfig()
  112. local status = statusMessages[wifi.sta.status()]
  113. local recipient = YO_RECIPIENT
  114. if not recipient then
  115. recipient = ''
  116. debugMsg("recipient: " .. recipient)
  117. end
  118. file.open('index.html')
  119. local indexhtml = file.read()
  120. file.close()
  121. indexhtml = string.gsub(indexhtml, "S_", ssid)
  122. indexhtml = string.gsub(indexhtml, "T_", status)
  123. indexhtml = string.gsub(indexhtml, "R_", recipient)
  124. debugMsg('sending indexhtml')
  125. debugMsg('____________')
  126. debugMsg(indexhtml)
  127. debugMsg('____________')
  128. conn:send(indexhtml)
  129. end
  130. function setupMode()
  131. local srv = nil
  132. if SETUP ~= true then
  133. restartSetupTimeout()
  134. broadcastAP()
  135. setupServer()
  136. end
  137. end
  138. setupMode()