Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <SoftwareSerial.h>
- const char* ssid = "Net-Virtua-6271";
- const char* password = "162562710";
- WiFiServer server(80); //Shield irá receber as requisições das páginas (o padrão WEB é a porta 80)
- bool led1 = false;
- String HTTP_req;
- String URLValue;
- String getURLRequest(String *requisicao);
- bool mainPageRequest(String *requisicao);
- void setup()
- {
- Serial.begin(115200);
- pinMode(4, OUTPUT); //LED
- //Conexão na rede WiFi
- Serial.println();
- Serial.print("Conectando a ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi conectado!");
- // Inicia o servidor WEB
- server.begin();
- Serial.println("Server iniciado");
- // Mostra o endereco IP
- Serial.println(WiFi.localIP());
- }
- void loop()
- {
- // INÍCIO DO SERVIÇO DE SERVIDOR
- WiFiClient client = server.available();
- if (Serial.available() > 0)
- {
- char data = Serial.read();
- if (data == '1')
- {
- digitalWrite(4, HIGH);
- led1 = true;
- }
- if (data == '0')
- {
- digitalWrite(4, LOW);
- led1 = false;
- }
- }
- if (client) {
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- HTTP_req += c;
- if (c == '\n' && currentLineIsBlank ) {
- if ( mainPageRequest(&HTTP_req) ) {
- URLValue = getURLRequest(&HTTP_req);
- Serial.println(HTTP_req);
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: keep-alive"); //<------ ATENCAO
- client.println();
- //Conteudo da Página HTML
- client.println("<!DOCTYPE html>");
- client.println("<html>");
- client.println("<head>");
- client.println("<title>Smartcine</title>");
- client.println("</head>");
- client.println("<body>"); //<------ALTERADO
- client.println("<br/>");
- client.println("<br/><br/>");
- client.println("<input type=\"text\" value=\"10\" id=\"countdown\" / > ");
- client.println("<input type=\"button\" value=\"Start\" onclick=\"startTimer(\'countdown\')\"/>");
- client.println("<input type=\"button\" value=\"Stop\" onclick=\"stopTimer(\'countdown\')\"/><br/>");
- client.println("<input type=\"checkbox\" name=\"smartcine\" value=\"led-01\"/>led-01<br/>");
- client.println("<input type=\"checkbox\" name=\"smartcine\" value=\"led-02\"/>led-02<br/>");
- client.println("<input type=\"checkbox\" name=\"smartcine\" value=\"led-03\"/>led-03<br/>");
- client.println("<script type=\"text/javascript\">");
- client.println("var intervalID;");
- client.println("function startTimer(Id){");
- client.println("var cbox = document.getElementsByName('smartcine');");
- client.println("var control = document.getElementById(Id);");
- client.println("var seconds = control.value;");
- client.println("seconds = seconds - 1;");
- if (led1) {
- client.println("cbox[0].checked = true;");
- client.println("cbox[1].checked = true;");
- client.println("cbox[2].checked = true;");
- }
- client.println("if(seconds == 0 && cbox[0].checked && cbox[1].checked && cbox[2].checked){");
- client.println("control.value = 'done';");
- client.println("document.write('<iframe width=\"420\" height=\"345\" src=\"https://www.youtube.com/embed/fJ9rUzIMcZQ?autoplay=1\"></iframe>');");
- client.println("return 1;");
- client.println("}");
- client.println("else");
- client.println("{");
- client.println("control.value = seconds;");
- client.println("}");
- client.println("if(seconds > 0 && (!cbox[0].checked || !cbox[1].checked || !cbox[2].checked)){");
- client.println("window.alert('erro, ligue os leds primeiro!');");
- client.println("control.value = 10;");
- client.println("return false;");
- client.println("}");
- client.println("intervalID = setTimeout(function(){startTimer(\"countdown\")}, 1000);");
- client.println("}");
- client.println("function stopTimer(){");
- client.println("clearTimeout(intervalID);");
- client.println("}");
- client.println("</script>");
- client.println("</body>");
- client.println("</html>");
- //FINAL DA PÁGINA HTML
- } else if (HTTP_req.indexOf("solicitacao_via_ajax") > -1) { //<----- NOVO
- Serial.println(HTTP_req);
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: keep-alive");
- client.println();
- } else {
- Serial.println(HTTP_req);
- client.println("HTTP/1.1 200 OK");
- }
- HTTP_req = "";
- break;
- }
- if (c == '\n') {
- currentLineIsBlank = true;
- }
- else if (c != '\r') {
- currentLineIsBlank = false;
- }
- }
- }
- delay(1);
- client.stop();
- }
- }
- String getURLRequest(String * requisicao) {
- int inicio, fim;
- String retorno;
- inicio = requisicao->indexOf("GET") + 3;
- fim = requisicao->indexOf("HTTP/") - 1;
- retorno = requisicao->substring(inicio, fim);
- retorno.trim();
- return retorno;
- }
- bool mainPageRequest(String * requisicao) {
- String valor;
- bool retorno = false;
- valor = getURLRequest(requisicao);
- valor.toLowerCase();
- if (valor == "/") {
- retorno = true;
- }
- if (valor.substring(0, 2) == "/?") {
- retorno = true;
- }
- if (valor.substring(0, 10) == "/index.htm") {
- retorno = true;
- }
- return retorno;
- }
Advertisement
Add Comment
Please, Sign In to add comment