Advertisement
cleberjz

ControlePortaoESP8266

Jul 29th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. #include <ESP8266WebServer.h>
  2. #include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
  3.                                 //Creditos Pedro Minatel
  4. //IP do ESP (para voce acessar pelo browser)
  5. IPAddress ip(192, 168, 0, 50);
  6.  
  7. //IP do roteador da sua rede wifi
  8. IPAddress gateway(192, 168, 0, 1);
  9.  
  10. //Mascara de rede da sua rede wifi
  11. IPAddress subnet(255, 255, 255, 0);
  12.  
  13. //Inicializacao do WiFiManager
  14. WiFiManager wifiManager;
  15. //Inicializacao so servidor http na porta 80
  16. WiFiServer server(80);
  17. //Status da GPIO
  18. uint8_t status_gpio = 0;
  19. const int pin = 12;
  20. const int fimc1 = 13;
  21. const int fimc2 = 16;
  22. void setup() {
  23.  
  24.   //Configura a serial
  25.   Serial.begin(115200);
  26.  
  27.   // configura os pinos 13 e 16 para os fim de curso
  28.   pinMode(fimc1, INPUT_PULLUP);
  29.   pinMode(fimc2, INPUT_PULLUP);
  30.  
  31.   //Configura a GPIO como saida
  32.   pinMode(pin, OUTPUT);
  33.   //Coloca a GPIO em sinal logico baixo
  34.   digitalWrite(pin, LOW);
  35.  
  36.   WiFi.config(ip, gateway, subnet);
  37.  
  38.   //Define o auto connect e o SSID do modo AP
  39.   wifiManager.setConfigPortalTimeout(180);
  40.   wifiManager.autoConnect("MeuWebServer");
  41.   //Log na serial se conectar
  42.   Serial.println("Conectado");
  43.   //Inicia o webserver de controle da GPIO
  44.   server.begin();
  45. }
  46.  
  47. void reset_config(void) {
  48.   //Reset das definicoes de rede
  49.   wifiManager.resetSettings();
  50.   delay(1500);
  51.   ESP.reset();
  52. }
  53.  
  54. void loop() {
  55.  
  56.   //Aguarda uma nova conexao
  57.   WiFiClient client = server.available();
  58.   if (!client) {
  59.     return;
  60.   }
  61.  
  62.   Serial.println("Nova conexao requisitada...");
  63.  
  64.   while (!client.available()) {
  65.     delay(1);
  66.   }
  67.  
  68.   Serial.println("Nova conexao OK...");
  69.  
  70.   //Le a string enviada pelo cliente
  71.   String req = client.readStringUntil('\r');
  72.   //Mostra a string enviada
  73.   Serial.println(req);
  74.   //Limpa dados/buffer
  75.   client.flush();
  76.  
  77.   const int efimc1 = digitalRead(fimc1);
  78.   const int efimc2 = digitalRead(fimc2);
  79.   Serial.print("Estado Fim curso 1");
  80.   Serial.println(efimc1);
  81.   Serial.print("Estado Fim curso 2");
  82.   Serial.println(efimc1);
  83.  
  84.   //Trata a string do cliente em busca de comandos
  85.  
  86.   if (req.indexOf("abre") != -1) {
  87.     if (efimc1 == LOW) {
  88.       yield();
  89.       digitalWrite(pin, HIGH);
  90.       status_gpio = HIGH;
  91.     }
  92.     if (efimc2 == LOW) {
  93.       yield();
  94.       digitalWrite(pin, LOW);
  95.       status_gpio = LOW;
  96.     }
  97.   }
  98.   //para resetar as configurações de rede wifi para poder habilitar o webconfig
  99.   //reset_config();
  100.  
  101.   else if (req.indexOf("fecha") != -1) {
  102.     if (efimc2 == LOW) {
  103.       yield();
  104.       digitalWrite(pin, HIGH);
  105.       status_gpio = HIGH;
  106.     }
  107.     if (efimc1 == LOW) {
  108.       yield();
  109.       digitalWrite(pin, LOW);
  110.       status_gpio = LOW;
  111.     }
  112.   }
  113.  
  114.     else {
  115.       Serial.println("Requisicao invalida");
  116.     }
  117.  
  118.  
  119.  
  120.     //Prepara a resposta para o cliente
  121.     String buf = "";
  122.     buf += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n";
  123.     buf += "<html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>\r\n";
  124.     buf += "<title>Porta da Escada</title>";
  125.     buf += "<style>.c{text-align: center;} div,input{padding:5px;font-size:1em;} input{width:80%;} body{text-align: center;font-family:verdana;} button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;} .q{float: right;width: 64px;text-align: right;}</style>";
  126.     buf += "</head>";
  127.     buf += "<h3>Porta da Escada</h3>";
  128.  
  129.     //De acordo com o status da GPIO envia o comando
  130.     if (status_gpio)
  131.       buf += "<div><h4>Porta</h4><a href=\"?function=abre\"><button>Fecha</button></a></div>";
  132.     else
  133.       buf += "<div><h4>Porta</h4><a href=\"?function=fecha\"><button>Abre</button></a></div>";
  134.  
  135.     buf += "<h4>Criado por Pedro Minatel</h4>";
  136.     buf += "</html>\n";
  137.  
  138.     //Envia a resposta para o cliente
  139.     client.print(buf);
  140.     client.flush();
  141.     client.stop();
  142.  
  143.     Serial.println("Cliente desconectado!");
  144.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement