Advertisement
RuiViana

Controle de agua

May 29th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.52 KB | None | 0 0
  1. #include <ESP8266WebServer.h>                                 // Bibioteca para Servidor Web ESP8266
  2. #include <ESP8266mDNS.h>                                      // Bibioteca para ..... ESP8266
  3. MDNSResponder mdns;                                           // ......
  4. const char* ssid = "xpto";                                // Use sua credenciais de rede wifi
  5. const char* password = "minhoca;
  6. ESP8266WebServer server(80);                                  // Instancia server
  7.  
  8. //  Strings com HTML
  9. String A_1 = "<!DOCTYPE HTML><html><head><meta http-equiv='refresh' content='1;URL=/Controle'/></head><h1><center> Controle de Agua</center>";
  10. String A_2 = "</p></center><h3><BR></h3><html>\r\n";
  11. String Al = "";                                               // Agua ligada
  12. String Ad = "";                                               // Agua desligada
  13. String Ail = "";
  14. String Aid = "";
  15. String Agua;
  16. #define Saida1 2                                              // GPIO Port para ligar Controle do triac (Pino 2 do MOC3023)
  17. const int Switch1 = 4;                                        // GPIO Port para ligar o interruptor
  18. byte Switch1_atual = 0;                                       // Variavel para staus de GPIO (Status do interruptor)
  19. //---------------------------------------
  20. void setup(void)
  21. {
  22.  Al += A_1;                                                  // Monta tela pra informar que a luz
  23.  Al += "<p><center>Agua</p><p><a href=\"/Controle?Agua=off \"><button style=\"background-color: rgb(255, 0,   0);height: 100px; width: 200px;\"><h1> ON</h1></button></a>";
  24.  
  25.   Ad += A_1;                                                  // Monta tela pra informar que a luz
  26.   Ad += "<p><center>Agua</p><p><a href=\"/Controle?Agua=on \"><button style=\"background-color: rgb(0, 255,   0);height: 100px; width: 200px;\"><h1> OFF</h1></button></a>";
  27.  
  28.   pinMode(Switch1, INPUT_PULLUP);                             // Define GPIO5 como entrada e liga o resistor de pullup
  29.   Switch1_atual = digitalRead(Switch1);                       // Atualisa status de GPIO
  30.   pinMode(Saida1, OUTPUT);                                    // Define GPIO como saida
  31.   digitalWrite(Saida1, LOW);                                  // Liga GPIO
  32.   Serial.begin(115200);                                       // Inicialisa o serial port em 115200
  33.   WiFi.begin(ssid, password);                                 // Inicialisa o wifi
  34. //  IPAddress subnet(255, 255, 255, 0);                                             // Acrescentei para fixar o IP  12/5/2017
  35. //  WiFi.config(IPAddress(192, 168, 0, 28), IPAddress(192, 168, 0, 1), subnet);     // Idem
  36.  
  37.   Serial.println("");                                         // Imprime
  38.   while (WiFi.status() != WL_CONNECTED)                       // Aguarda conexão
  39.   {
  40.     delay(500);                                               // Delay
  41.     Serial.print(".");                                        // Imprime . enquanto não conecta
  42.   }
  43.   Serial.println("");                                         // Imprime
  44.   Serial.print("Connected to ");
  45.   Serial.println(ssid);
  46.   Serial.print("IP address: ");
  47.   Serial.println(WiFi.localIP());
  48.   if (mdns.begin("esp8266", WiFi.localIP()))                  // Se ......????
  49.   {
  50.     Serial.println("MDNS responder started");                 // Imprime
  51.   }
  52.   server.on("/", []()                                         // Ao requeste
  53.   { server.send(200, "text/html", Al);                        // Executa o HTML Al (Agua ligada)
  54.   });
  55.   server.on("/Controle", []()                                 // Ao requeste /Agua
  56.   {
  57.     Agua = server.arg("Agua");                                // Recupera o valor do parametro luz enviado
  58.     if (Agua == "off") digitalWrite(Saida1, LOW);             // Se o valor de Agua e off desliga a saida
  59.     if (Agua == "on") digitalWrite(Saida1, HIGH);             // Se o valor de Agua e on liga a saida
  60.     if (digitalRead(Saida1) == HIGH)                          // Se a saida esta ligada, carrega a pagina "ligada"
  61.     {
  62.         Ail += Al;                                            // Monta tela nova Agua ligada
  63.         Ail += A_2;
  64.         server.send(200, "text/html", Ail);                   // Mostra Agua ligada
  65.         Ail = "";                                             // Limpa valor de temperatura e umidade
  66.     }
  67.     if (digitalRead(Saida1) == LOW)    // Se a saida esta desligada, carrega a pagina "desligada"
  68.     {
  69.         Aid += Ad;                                            // Monta tela nova Agua desligada
  70.         Aid += A_2;
  71.         server.send(200, "text/html", Aid);                   // Mostra Agua desligada
  72.         Aid = "";                                             // Limpa valor de temperatura e umidade
  73.     }
  74.     delay(100);                                               // Delay
  75.   });
  76.   server.begin();                                             // Inicaliza servidor
  77.   Serial.println("HTTP server started");                      // Imprime
  78. }
  79. //--------------------------------------------------
  80. void loop(void)
  81. {
  82.   server.handleClient();                                      // Executa instancia
  83.   if (digitalRead(Switch1) !=  Switch1_atual)                 // Se o valor do SW alterou
  84.   {
  85.     delay(40);                                                // Delay
  86.     if (digitalRead(Switch1) !=  Switch1_atual)               // Se o valor do SW alterou
  87.     {
  88.       digitalWrite(Saida1, !digitalRead(Saida1));             // Inverte a saida lamp1
  89.       Switch1_atual = digitalRead(Switch1);                   // Atualisa o Gpio atual
  90.     }
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement