wifiSetup.lua 3.2 KB

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