Advertisement
tbatista

WebServer Moises

Jan 4th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.32 KB | None | 0 0
  1.  
  2. #include <ESP8266WiFi.h>
  3.  
  4. //Nome da sua rede Wifi
  5.  
  6. const char* ssid = "moises";
  7. //Senha da rede
  8. const char* password = "moises";
  9.  
  10.  
  11. //IPAddress ip(000000000000000);
  12. //IPAddress gateway(000000000000);
  13. //IPAddress subnet(255, 255, 255, 0);
  14. WiFiServer server(80);
  15.  
  16.  
  17. //Pino do NodeMCU que estara conectado ao rele
  18. const int pin = D4; //Equivalente ao D2 no NodeMCU
  19.  
  20. //Funcao que sera executada apenas ao ligar o ESP8266
  21. void setup() {
  22.   WiFi.persistent(false);
  23.   //Preparando o pino, que esta lidago ao rele
  24.   pinMode(pin, OUTPUT);
  25.   digitalWrite(pin, HIGH);
  26.  
  27.   //Conectando a rede Wifi
  28.   //  WiFi.config(ip, gateway, subnet);
  29.   WiFi.begin(ssid, password);
  30.  
  31.  
  32.   //  //Verificando se esta conectado,
  33.   //  //caso contrario, espera um pouco e verifica de novo.
  34.   //  while (WiFi.status() != WL_CONNECTED) {
  35.   while (WiFi.status() != WL_CONNECTED) {
  36.     delay(500);
  37.     Serial.print(".");
  38.   }
  39.  
  40.   while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  41.     Serial.println(F("Connection Failed! Rebooting..."));
  42.     delay(5000);
  43.     ESP.restart();
  44.   }
  45.  
  46.   // Serial.println(F("Conectado"));
  47.   Serial.print("IP: ");
  48.   Serial.println(WiFi.localIP());
  49.  
  50.   //Iniciando o servidor Web
  51.   server.begin();
  52. }
  53.  
  54. //Funcao que sera executada indefinidamente enquanto o NodeMCU estiver ligado.
  55. void loop() {
  56.   //Verificando se o servidor esta pronto.
  57.   WiFiClient client = server.available();
  58.   if (!client) {
  59.     return;
  60.   }
  61.  
  62.   //Verificando se o servidor recebeu alguma requisicao
  63.   while (!client.available()) {
  64.     delay(1);
  65.   }
  66.  
  67.   //Obtendo a requisicao vinda do browser
  68.   String req = client.readStringUntil('\r');
  69.  
  70.   //Sugestao dada por Enrico Orlando
  71.   if (req == "GET /favicon.ico HTTP/1.1") {
  72.     req = client.readStringUntil('\r');
  73.   }
  74.  
  75.   client.flush();
  76.  
  77.   //Iniciando o buffer que ira conter a pagina HTML que sera enviada para o browser.
  78.   String buf = "";
  79.  
  80.   buf += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";
  81.   buf += "<head> ";
  82.   buf += "<meta charset='UTF-8'> ";
  83.   buf += "<meta http-equiv='cache-control' content='max-age=0' /> ";
  84.   buf += "<meta http-equiv='cache-control' content='no-cache' /> ";
  85.   buf += "<meta http-equiv='expires' content='0' /> ";
  86.   buf += "<meta http-equiv='expires' content='Tue, 01 Jan 1980 1:00:00 GMT' /> ";
  87.   buf += "<meta http-equiv='pragma' content='no-cache' /> ";
  88.   buf += "<title>Automa&ccedil;&atilde;o Residencial</title> ";
  89.   buf += "<style> ";
  90.   buf += "body{font-family:Open Sans; color:#555555;} ";
  91.   buf += "h1{font-size:24px; font-weight:normal; margin:0.4em 0;} ";
  92.   buf += ".container { width: 100%; margin: 0 auto; } ";
  93.   buf += ".container .row { float: left; clear: both; width: 100%; } ";
  94.   buf += ".container .col { float: left; margin: 0 0 1.2em; padding-right: 1.2em; padding-left: 1.2em; } ";
  95.   buf += ".container .col.four, .container .col.twelve { width: 100%; } ";
  96.   buf += "@media screen and (min-width: 767px) { ";
  97.   buf += ".container{width: 100%; max-width: 1080px; margin: 0 auto;} ";
  98.   buf += ".container .row{width:100%; float:left; clear:both;} ";
  99.   buf += ".container .col{float: left; margin: 0 0 1em; padding-right: .5em; padding-left: .5em;} ";
  100.   buf += ".container .col.four { width: 50%; } ";
  101.   buf += ".container .col.tweleve { width: 100%; } ";
  102.   buf += "} ";
  103.   buf += "* {-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;} ";
  104.   buf += "a{text-decoration:none;} ";
  105.   buf += ".btn {font-size: 18px; white-space:nowrap; width:100%; padding:.8em 1.5em; font-family: Open Sans, Helvetica,Arial,sans-serif; ";
  106.   buf += "line-height:18px; display: inline-block;zoom: 1; color: #fff; text-align: center; position:relative; ";
  107.   buf += "-webkit-transition: border .25s linear, color .25s linear, background-color .25s linear; ";
  108.   buf += "transition: border .25s linear, color .25s linear, background-color .25s linear;} ";
  109.   buf += ".btn.btn-sea{background-color: #08bc9a; border-color: #08bc9a; -webkit-box-shadow: 0 3px 0 #088d74; box-shadow: 0 3px 0 #088d74;} ";
  110.   buf += ".btn.btn-sea:hover{background-color:#01a183;} ";
  111.   buf += ".btn.btn-sea:active{ top: 3px; outline: none; -webkit-box-shadow: none; box-shadow: none;} ";
  112.   buf += "</style> ";
  113.   buf += "</head> ";
  114.   buf += "<body> ";
  115.   buf += "<div class='container'> ";
  116.   buf += "<div class='row'> ";
  117.   buf += "<div class='col twelve'> ";
  118.   buf += "<p align='center'><font size='20'>Controle de l&acirc;mpadas</font></p> ";
  119.   buf += "</div> ";
  120.   buf += "</div> ";
  121.   buf += "<div class='row'> ";
  122.   buf += "<div class='col four'> ";
  123.   buf += "<a href='?f=on' class='btn btn-sea'>Ligar</a> ";
  124.   buf += "</div> ";
  125.   buf += "<div class='col four'> ";
  126.   buf += "<a href='?f=off' class='btn btn-sea'>Desligar</a> ";
  127.   buf += "</div> ";
  128.   buf += "</div> ";
  129.   buf += "<div class='col twelve'> ";
  130.  
  131.   buf += "<p align='center'><font size='5'>Automation</font></p> ";
  132.   buf += "</div> ";
  133.   buf += "</div> ";
  134.   buf += "</body> ";
  135.   buf += "</html> ";
  136.  
  137.   //Enviando para o browser a 'pagina' criada.
  138.   client.print(buf);
  139.   client.flush();
  140.  
  141.   //Analisando a requisicao recebida para decidir se liga ou desliga a lampada
  142.   if (req.indexOf("on") != -1)
  143.   {
  144.     digitalWrite(pin, LOW);
  145.   }
  146.   else if (req.indexOf("off") != -1)
  147.   {
  148.     digitalWrite(pin, HIGH);
  149.   }
  150.   else
  151.   {
  152.     //Requisicao invalida!
  153.     client.stop();
  154.   }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement