Guest User

Untitled

a guest
Jun 28th, 2018
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5. #include <SoftwareSerial.h>
  6.  
  7. const char* ssid = "Net-Virtua-6271";
  8. const char* password = "162562710";
  9.  
  10. WiFiServer server(80); //Shield irá receber as requisições das páginas (o padrão WEB é a porta 80)
  11.  
  12. bool led1 = false;
  13.  
  14. String HTTP_req;
  15. String URLValue;
  16.  
  17. String getURLRequest(String *requisicao);
  18. bool mainPageRequest(String *requisicao);
  19.  
  20. void setup()
  21. {
  22. Serial.begin(115200);
  23. pinMode(4, OUTPUT); //LED
  24.  
  25. //Conexão na rede WiFi
  26. Serial.println();
  27. Serial.print("Conectando a ");
  28. Serial.println(ssid);
  29.  
  30. WiFi.begin(ssid, password);
  31.  
  32. while (WiFi.status() != WL_CONNECTED) {
  33. delay(500);
  34. Serial.print(".");
  35. }
  36. Serial.println("");
  37. Serial.println("WiFi conectado!");
  38.  
  39. // Inicia o servidor WEB
  40. server.begin();
  41. Serial.println("Server iniciado");
  42.  
  43. // Mostra o endereco IP
  44. Serial.println(WiFi.localIP());
  45. }
  46.  
  47. void loop()
  48. {
  49. // INÍCIO DO SERVIÇO DE SERVIDOR
  50. WiFiClient client = server.available();
  51.  
  52. if (Serial.available() > 0)
  53. {
  54. char data = Serial.read();
  55. if (data == '1')
  56. {
  57. digitalWrite(4, HIGH);
  58. led1 = true;
  59. }
  60. if (data == '0')
  61. {
  62. digitalWrite(4, LOW);
  63. led1 = false;
  64. }
  65. }
  66.  
  67. if (client) {
  68. boolean currentLineIsBlank = true;
  69. while (client.connected()) {
  70. if (client.available()) {
  71. char c = client.read();
  72. HTTP_req += c;
  73.  
  74. if (c == '\n' && currentLineIsBlank ) {
  75.  
  76. if ( mainPageRequest(&HTTP_req) ) {
  77. URLValue = getURLRequest(&HTTP_req);
  78. Serial.println(HTTP_req);
  79.  
  80. client.println("HTTP/1.1 200 OK");
  81. client.println("Content-Type: text/html");
  82. client.println("Connection: keep-alive"); //<------ ATENCAO
  83. client.println();
  84.  
  85.  
  86.  
  87.  
  88. //Conteudo da Página HTML
  89.  
  90. client.println("<!DOCTYPE html>");
  91. client.println("<html>");
  92. client.println("<head>");
  93. client.println("<title>Smartcine</title>");
  94. client.println("</head>");
  95. client.println("<body>"); //<------ALTERADO
  96. client.println("<br/>");
  97.  
  98. client.println("<br/><br/>");
  99.  
  100. client.println("<input type=\"text\" value=\"10\" id=\"countdown\" / > ");
  101. client.println("<input type=\"button\" value=\"Start\" onclick=\"startTimer(\'countdown\')\"/>");
  102. client.println("<input type=\"button\" value=\"Stop\" onclick=\"stopTimer(\'countdown\')\"/><br/>");
  103. client.println("<input type=\"checkbox\" name=\"smartcine\" value=\"led-01\"/>led-01<br/>");
  104. client.println("<input type=\"checkbox\" name=\"smartcine\" value=\"led-02\"/>led-02<br/>");
  105. client.println("<input type=\"checkbox\" name=\"smartcine\" value=\"led-03\"/>led-03<br/>");
  106. client.println("<script type=\"text/javascript\">");
  107. client.println("var intervalID;");
  108. client.println("function startTimer(Id){");
  109. client.println("var cbox = document.getElementsByName('smartcine');");
  110. client.println("var control = document.getElementById(Id);");
  111. client.println("var seconds = control.value;");
  112. client.println("seconds = seconds - 1;");
  113. if (led1) {
  114. client.println("cbox[0].checked = true;");
  115. client.println("cbox[1].checked = true;");
  116. client.println("cbox[2].checked = true;");
  117. }
  118. client.println("if(seconds == 0 && cbox[0].checked && cbox[1].checked && cbox[2].checked){");
  119. client.println("control.value = 'done';");
  120. client.println("document.write('<iframe width=\"420\" height=\"345\" src=\"https://www.youtube.com/embed/fJ9rUzIMcZQ?autoplay=1\"></iframe>');");
  121. client.println("return 1;");
  122. client.println("}");
  123. client.println("else");
  124. client.println("{");
  125. client.println("control.value = seconds;");
  126. client.println("}");
  127. client.println("if(seconds > 0 && (!cbox[0].checked || !cbox[1].checked || !cbox[2].checked)){");
  128. client.println("window.alert('erro, ligue os leds primeiro!');");
  129. client.println("control.value = 10;");
  130. client.println("return false;");
  131. client.println("}");
  132. client.println("intervalID = setTimeout(function(){startTimer(\"countdown\")}, 1000);");
  133. client.println("}");
  134. client.println("function stopTimer(){");
  135. client.println("clearTimeout(intervalID);");
  136. client.println("}");
  137. client.println("</script>");
  138. client.println("</body>");
  139. client.println("</html>");
  140.  
  141.  
  142.  
  143. //FINAL DA PÁGINA HTML
  144.  
  145. } else if (HTTP_req.indexOf("solicitacao_via_ajax") > -1) { //<----- NOVO
  146.  
  147. Serial.println(HTTP_req);
  148.  
  149. client.println("HTTP/1.1 200 OK");
  150. client.println("Content-Type: text/html");
  151. client.println("Connection: keep-alive");
  152. client.println();
  153.  
  154. } else {
  155.  
  156. Serial.println(HTTP_req);
  157. client.println("HTTP/1.1 200 OK");
  158. }
  159. HTTP_req = "";
  160. break;
  161. }
  162.  
  163. if (c == '\n') {
  164. currentLineIsBlank = true;
  165. }
  166. else if (c != '\r') {
  167. currentLineIsBlank = false;
  168. }
  169. }
  170. }
  171. delay(1);
  172. client.stop();
  173. }
  174.  
  175.  
  176. }
  177.  
  178. String getURLRequest(String * requisicao) {
  179. int inicio, fim;
  180. String retorno;
  181.  
  182. inicio = requisicao->indexOf("GET") + 3;
  183. fim = requisicao->indexOf("HTTP/") - 1;
  184. retorno = requisicao->substring(inicio, fim);
  185. retorno.trim();
  186.  
  187. return retorno;
  188. }
  189.  
  190. bool mainPageRequest(String * requisicao) {
  191. String valor;
  192. bool retorno = false;
  193.  
  194. valor = getURLRequest(requisicao);
  195. valor.toLowerCase();
  196.  
  197. if (valor == "/") {
  198. retorno = true;
  199. }
  200.  
  201. if (valor.substring(0, 2) == "/?") {
  202. retorno = true;
  203. }
  204.  
  205. if (valor.substring(0, 10) == "/index.htm") {
  206. retorno = true;
  207. }
  208.  
  209. return retorno;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment