wifiSetup.lua 3.3 KB

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