Advertisement
Guest User

init.lua

a guest
Feb 11th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. print("WIFI control")
  2. wifi.setmode(wifi.SOFTAP)
  3. print("ESP8266 mode is: " .. wifi.getmode())
  4. cfg={}
  5. cfg.ssid="ESP"
  6. cfg.pwd="11111111"
  7. if ssid and password then
  8. print("ESP8266 SSID is: " .. cfg.ssid .. " and PASSWORD is: " .. cfg.password)
  9. end
  10. wifi.ap.config(cfg)
  11. ap_mac = wifi.ap.getmac()
  12. sv=net.createServer(net.TCP)
  13. sv:listen(80,function(c)
  14. c:on("receive", function(c, pl)
  15. print(pl)
  16. print(string.len(pl))
  17. print(string.match(pl,"GET"))
  18. ssid_start,ssid_end=string.find(pl,"SSID=")
  19. if ssid_start and ssid_end then
  20. amper1_start, amper1_end =string.find(pl,"&", ssid_end+1)
  21. if amper1_start and amper1_end then
  22. http_start, http_end =string.find(pl,"HTTP/1.1", ssid_end+1)
  23. if http_start and http_end then
  24. ssid=string.sub(pl,ssid_end+1, amper1_start-1)
  25. password=string.sub(pl,amper1_end+10, http_start-2)
  26. print("ESP8266 connecting to SSID: " .. ssid .. " with PASSWORD: " .. password)
  27. if ssid and password then
  28. sv:close()
  29. wifi.setmode(wifi.STATIONAP)
  30. print("ESP8266 mode now is: " .. wifi.getmode())
  31. wifi.sta.config(ssid,password)
  32. print("Setting up ESP8266 for station mode…Please wait.")
  33. tmr.delay(10000000)
  34. print("ESP8266 STATION IP now is: " .. wifi.sta.getip())
  35. print("ESP8266 AP IP now is: " .. wifi.ap.getip())
  36. gpio.mode(8,gpio.OUTPUT)
  37. gpio.mode(9,gpio.OUTPUT)
  38. tmr.delay(10)
  39. gpio.write(8,gpio.HIGH)
  40. tmr.delay(10)
  41. gpio.write(8,gpio.LOW)
  42. sv=net.createServer(net.TCP, 30)
  43. sv:listen(9999,function(c)
  44. c:on("receive", function(c, pl)
  45. if tonumber(pl) ~= nil then
  46. if tonumber(pl) >= 1 and tonumber(pl) <= 16 then
  47. print(tonumber(pl))
  48. tmr.delay(10)
  49. gpio.write(8,gpio.HIGH)
  50. tmr.delay(10)
  51. gpio.write(8,gpio.LOW)
  52. for count =1,tonumber(pl) do
  53.  print(count)
  54. tmr.delay(10)
  55.  gpio.write(9,gpio.LOW)
  56. tmr.delay(10)
  57. gpio.write(9,gpio.HIGH)
  58. c:send("Sequence finished")
  59. end
  60. end
  61. end
  62. print("ESP8266 STATION IP now is: " .. new_ip)
  63. c:send("ESP8266 STATION IP now is: " .. new_ip)
  64. c:send("Action completed")
  65. end)
  66. end)
  67. end
  68. end
  69. end
  70. end
  71. print("Pagina")
  72. c:send("<!DOCTYPE html>")
  73. c:send("<html lang=’en’>")
  74. c:send('<head><meta  content="text/html; charset=utf-8">')
  75. c:send('<title>ESP8266</title></head>')
  76. c:send("<body>")
  77. c:send("<h1>ESP8266 Wireless control setup</h1><BR>")
  78. mac_mess1 = "The module MAC address is: " .. ap_mac
  79. mac_mess2 = "You will need this MAC address to find the IP address of the module, please take note of it."
  80. c:send("<h2>" .. mac_mess1 .. "</h2> ")
  81. c:send("<h2>" .. mac_mess2 .. "</h2> ")
  82. c:send("<h2>Enter SSID and Password for your WIFI router</h2> ")
  83. c:send('<form action="" method="GET">')  
  84. c:send("SSID:")
  85. c:send('<input type="text" name="SSID" value="" maxlength="100" />')
  86. c:send("<br />")
  87. c:send("Password:")
  88. c:send('<input type="text" name="Password" value="" maxlength="100" />')
  89. c:send('<input type="submit" value="Submit" />')
  90. c:send("</form> </body> </html>")
  91. c:send("</body> </html>")
  92. end)
  93. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement