Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. wifi.setmode(wifi.STATION)
  2.  
  3. if file.open("password.txt", r) then
  4. local username = file.readline();
  5. local password = file.readline();
  6. file.close();
  7. username = string.sub(username, 1, #username - 1);
  8. password = string.sub(password, 1, #password - 1);
  9. wifi.sta.config(username, password);
  10. else
  11. print("[cannt open password.txt]");
  12. end
  13. -------------------------------------------------
  14. ledR = 1023;
  15. ledG = 1023;
  16. ledB = 1023;
  17. on = true;
  18.  
  19. brightness = 1;
  20.  
  21. function led(r, g, b)
  22. on = true;
  23. pwm.setduty(5, r * brightness)
  24. pwm.setduty(6, g * brightness)
  25. pwm.setduty(7, b * brightness)
  26. end
  27.  
  28. pwm.setup(5, 1000, 1023)
  29. pwm.setup(6, 1000, 1023)
  30. pwm.setup(7, 1000, 1023)
  31. pwm.start(5)
  32. pwm.start(6)
  33. pwm.start(7)
  34. ------------------------------------------------
  35.  
  36. local httpRequest = {}
  37. httpRequest["/"] = "index.html";
  38. httpRequest["/index.html"] = "index.html";
  39. httpRequest["/style.css"] = "style.css";
  40. httpRequest["/main.js"] = "main.js";
  41.  
  42.  
  43. local getContentType = {};
  44. getContentType["/"] = "text/html";
  45. getContentType["/index.html"] = "text/html";
  46. getContentType["/style.css"] = "text/css";
  47. getContentType["/main.js"] = "main.js";
  48.  
  49. local filePos = 0;
  50.  
  51. tmr.alarm(1, 1000, 1, function()
  52. if wifi.sta.getip() == nil then
  53. print("[IP unavaiable, waiting.]")
  54. else
  55. tmr.stop(1)
  56. print("[Connected, IP is " .. wifi.sta.getip() .. "]");
  57. end
  58. end)
  59.  
  60. if srv then srv:close() srv = nil end
  61. srv = net.createServer(net.TCP)
  62. srv:listen(80, function(conn)
  63.  
  64. conn:on("receive", function(conn, request)
  65. print("[New Request]");
  66. local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
  67. if (method == nil) then
  68. _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
  69. end
  70. local _GET = {}
  71. if (vars ~= nil) then
  72. for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
  73. print("[" .. k .. "=" .. v .. "]");
  74. _GET[k] = v
  75. end
  76. end
  77.  
  78.  
  79. if getContentType[path] then
  80. requestFile = httpRequest[path];
  81. print("[Sending file " .. requestFile .. "]");
  82. filePos = 0;
  83. conn:send("HTTP/1.1 200 OK\r\nContent-Type: " .. getContentType[path] .. "\r\n\r\n");
  84. elseif string.find(path, '/api') then
  85. conn:send("HTTP/1.1 200 OK\r\nContent-Type: text/html \r\n\r\n");
  86. if string.find(path, '/brightness') then
  87. conn:send(brightness * 100);
  88.  
  89. elseif string.find(path, '/color') then
  90. if (_GET.hex) then
  91. print("[_GET.hex " .. _GET.hex .. "]");
  92. local hex = _GET.hex;
  93. hex = hex:gsub("");
  94. local hexR = tonumber("0x" .. hex:sub(1, 2));
  95. local hexG = tonumber("0x" .. hex:sub(3, 4));
  96. local hexB = tonumber("0x" .. hex:sub(5, 6));
  97.  
  98. if hexR == NaN then
  99. hexR = 00;
  100. end
  101.  
  102. if hexG == NaN then
  103. hexG = 00;
  104. end
  105.  
  106. if hexB == NaN then
  107. hexB = 00;
  108. end
  109.  
  110. ledR = hexR * 1023 / 255;
  111. ledG = hexG * 1023 / 255;
  112. ledB = hexB * 1023 / 255;
  113.  
  114. print("[hexR " .. ledR .. "]");
  115. print("[hexG " .. ledG .. "]");
  116. print("[hexB " .. ledB .. "]");
  117. led(ledR, ledG, ledB);
  118. conn:send("ok");
  119. else
  120. local hexR = ledR * 255 / 1023;
  121. local hexG = ledG * 255 / 1023;
  122. local hexB = ledB * 255 / 1023;
  123.  
  124. local hex = string.format("%x%x%x", hexR, hexG, hexB);
  125. conn:send(hex);
  126. end
  127. elseif string.find(path, '/off') then
  128. led(0, 0, 0);
  129. conn:send("ok");
  130. elseif string.find(path, '/on') then
  131. led(ledR, ledG, ledB);
  132. conn:send("ok");
  133. elseif string.find(path, '/status') then
  134.  
  135. if on then
  136. conn:send("1");
  137.  
  138. else
  139. conn:send("0");
  140. end
  141.  
  142. conn:close();
  143. collectgarbage();
  144. end
  145.  
  146. else
  147. print("[File " .. path .. " not found]");
  148. conn:send("HTTP/1.1 404 Not Found\r\n\r\n")
  149. conn:close();
  150. collectgarbage();
  151. end
  152.  
  153. if (_GET.red) then
  154. led(1023, 0, 0)
  155. ledR = 1023;
  156. ledG = 0;
  157. ledB = 0;
  158. end
  159.  
  160. if (_GET.green) then
  161. led(0, 1023, 0)
  162. ledR = 0;
  163. ledG = 1023;
  164. ledB = 0;
  165. end
  166.  
  167. if (_GET.blue) then
  168. led(0, 0, 1023)
  169. ledR = 0;
  170. ledG = 0;
  171. ledB = 1023;
  172. end
  173.  
  174. if (_GET.on) then
  175. led(ledR, ledG, ledB)
  176. print(ledR)
  177. end
  178.  
  179.  
  180. if (_GET.r or _GET.g or _GET.b) then
  181. led(_GET.r, _GET.g, _GET.b);
  182. ledR = _GET.r;
  183. ledG = _GET.g;
  184. ledB = _GET.b;
  185. end
  186.  
  187. if (_GET.off) then
  188. led(0, 0, 0)
  189. on = false;
  190. end
  191.  
  192.  
  193. if (_GET.brightness) then
  194. brightness = _GET.brightness * 0.01;
  195. led(ledR, ledG, ledB);
  196. end
  197.  
  198.  
  199.  
  200. --close receive
  201. end)
  202.  
  203.  
  204. conn:on("sent", function(conn)
  205. if requestFile then
  206. if file.open(requestFile, r) then
  207. file.seek("set", filePos);
  208. local partial_data = file.read(512);
  209. file.close();
  210. if partial_data then
  211. filePos = filePos + #partial_data;
  212. print("[" .. filePos .. " bytes sent]");
  213. conn:send(partial_data);
  214. if (string.len(partial_data) == 512) then
  215. return;
  216. end
  217. end
  218. else
  219. print("[Error opening file" .. requestFile .. "]");
  220. end
  221. end
  222. print("[Connection closed]");
  223. conn:close();
  224. collectgarbage();
  225. end)
  226. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement