Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. tmr.alarm(6, sec, 2, function ()
  2. if count>=1 then
  3. timer_on(1)
  4. count=count-1
  5. print("counter")
  6. end
  7.  
  8. end)
  9. if count==1 then
  10. print("led on")
  11. count=6
  12. end
  13. end
  14.  
  15. timer_on(1)
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. if msg.restart then
  26. node.restart()
  27. end
  28. if msg.reset then
  29. file.remove("config")
  30. tmr.alarm(5, 100, 0, function ()
  31. node.restart()
  32. end)
  33. end
  34. end
  35. ws:send(sjson.encode({state=gpio.read(PINS.OP), config=read_config()}))
  36.  
  37. print("AP Started")
  38.  
  39. local hex_to_char = function(x)
  40. return string.char(tonumber(x, 16))
  41. end
  42.  
  43. local unescape = function(url)
  44. return url:gsub("%%(%x%x)", hex_to_char)
  45. end
  46.  
  47. srv = net.createServer(net.TCP)
  48. srv:listen(80,function(conn)
  49. conn:on("receive", function(resp, data)
  50. local body = ""
  51. data = data:gsub("GET /", "")
  52. data = data:gsub("HTTP/1.1", "")
  53. data = split(data, "n")[0]
  54. data = unescape(data)
  55.  
  56. print("data recieved: " .. data)
  57.  
  58.  
  59. local json_ok, jdata = pcall(sjson.decode, data)
  60.  
  61. if json_ok then
  62. body = sjson.encode({
  63. deviceId = deviceId,
  64. success = true
  65. })
  66. resp:send(table.concat (
  67. {
  68. "HTTP/1.1 200 OK",
  69. "Content-Type: application/json",
  70. "Content-length: " .. #body,
  71. "",
  72. body
  73. }, "rn"))
  74. tmr.alarm(3, 100, 0, function()
  75. wifi_connect({ssid=jdata.ssid,pwd=jdata.pwd}, function(success)
  76. if success then
  77. file.open("config","w")
  78. file.write(data)
  79. file.close()
  80. print("OK")
  81. else
  82. ap_config_start()
  83. print("NOT_OK")
  84. end
  85. end)
  86. end)
  87. else
  88. body = sjson.encode({
  89. success = false,
  90. message = "INVALID_INPUT"
  91. })
  92. resp:send(table.concat (
  93. {
  94. "HTTP/1.1 200 OK",
  95. "Content-Type: application/json",
  96. "Content-length: " .. #body,
  97. "",
  98. body
  99. }, "rn"))
  100. end
  101. end)
  102. end)
  103.  
  104. local ws = websocket.createClient()
  105. local wsUrl = "ws://"..SERVER.."/?deviceId="..deviceId
  106.  
  107. ws:on("connection", function(wss)
  108. print("Connected")
  109. local signin = sjson.encode({
  110. signin = true,
  111. deviceId = deviceId,
  112. config = read_config().config
  113. })
  114. print(signin)
  115. ws:send(signin)
  116. tmr.stop(4)
  117. gpio.write(PINS.IN, 1)
  118. end)
  119. ws:on("receive", function(wss, msg)
  120.  
  121. print("Received: ", msg)
  122. handle_push(ws, msg)
  123. gpio.write(PINS.IN, 0)
  124. tmr.alarm(4, 100, 0, function()
  125. gpio.write(PINS.IN, 1)
  126. end)
  127. end)
  128. ws:on("close", function(wss, status)
  129. if RS_F == false then
  130. gpio.write(PINS.IN, 0)
  131. print("Closed! Retrying..")
  132. if wifi.sta.status() == 2 or wifi.sta.status() == 3 or wifi.sta.status() == 4 then
  133. wifi_connect(WIFI_CNF)
  134. else
  135. ws:connect(wsUrl)
  136. end
  137. end
  138. end)
  139.  
  140. ws:connect(wsUrl)
  141.  
  142. wifi_connect(FI)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement