Advertisement
RuiViana

ESP_Control_4_Ports

Jan 29th, 2017
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 33.94 KB | None | 0 0
  1. //#include <SHT1x.h>                                          // https://github.com/practicalarduino/SHT1x
  2. //#define dataPin  0                                          // GPIO 0  D3  Port para o Data do SHT10
  3. //#define clockPin 15                                         // GPIO 15 D8  Port para o Clock do SHT10
  4. //SHT1x sht1x(dataPin, clockPin);                             // Instancia shtx1
  5. #include <ESP8266WebServer.h>                               // Bibioteca para Servidor Web ESP8266
  6. #include <ESP8266mDNS.h>                                    // Bibioteca para ..... ESP8266
  7. MDNSResponder mdns;                                         // ......
  8. const char* ssid = "xxxxxx";                              // Use sua credenciais de rede wifi
  9. const char* password = "yyyyyyyyy";
  10. ESP8266WebServer server(80);                                // Instancia server
  11. //  Strings com HTML
  12.  
  13. String Q_1 = "<!DOCTYPE HTML><html><head><meta http-equiv='refresh' content='1;URL=/Controle'/></head><h1><center>ESP8266 Controle de luz</center>";
  14. String Q_2 = "</p></center><h3><BR></h3><html>\r\n";
  15.  
  16.  
  17. String QlSlBlCl = "";   // 0F                                    // Quarto ligado Sala igada Banheiro ligado Carregador ligado
  18. String QlSlBlCd = "";   // 0E                                    // Quarto ligado Sala ligada Banheiro ligado Carregador desligado
  19. String QlSlBdCl = "";   // 0D                                    // Quarto ligado Sala ligada Banheiro desligado Carregador ligado
  20. String QlSlBdCd = "";   // 0C                                    // Quarto ligado Sala ligada Banheiro desligado Carregador desligado
  21. String QlSdBlCl = "";   // 0B                                    // Quarto ligado Sala desligada Banheiro ligado Carregador ligado
  22. String QlSdBlCd = "";   // 0A                                    // Quarto ligado Sala desligada Banheiro ligado Carregador desligado
  23. String QlSdBdCl = "";   // 09                                    // Quarto ligado Sala desligada Banheiro desligado Carregador ligado
  24. String QlSdBdCd = "";   // 08                                    // Quarto ligado Sala desligada Banheiro desligado Carregador desligado
  25. String QdSlBlCl = "";   // 07                                    // Quarto desligado Sala ligada Banheiro ligado Carregador ligado
  26. String QdSlBlCd = "";   // 06                                    // Quarto desligado Sala ligada Banheiro ligado Carregador desligado
  27. String QdSlBdCl = "";   // 05                                    // Quarto desligado Sala ligada Banheiro desligado Carregador ligado
  28. String QdSlBdCd = "";   // 04                                    // Quarto desligado Sala ligada Banheiro desligado Carregador desligado
  29. String QdSdBlCl = "";   // 03                                    // Quarto desligado Sala desligada Banheiro ligado Carregador ligado
  30. String QdSdBlCd = "";   // 02                                    // Quarto desligado Sala desligada Banheiro ligado Carregador desligado
  31. String QdSdBdCl = "";   // 01                                    // Quarto desligado Sala desligada Banheiro desligado Carregador ligado
  32. String QdSdBdCd = "";   // 00                                    // Quarto desligado Sala desligada Banheiro desligado Carregador desligado
  33.  
  34. String Qi = "";
  35. String Qif = "";
  36.  
  37. //Qif +=  "<p><center>Temperatura: " + String((float)temp_c) + "C </p>";
  38. //Qif +=  "<p><center>Umidade: " + String((int)humidity) + "% </p>";
  39. //Qif += Q_2;
  40.  
  41. String LuzQuarto;
  42. String LuzSala;
  43. String LuzBanho;
  44. String Carga;
  45.  
  46. #define Diag   2                                        // GPIO2 Port somente para visualizacao durante teste. Pode ser comentado na versao para instalar
  47.  
  48. float temp_c;                                               // Variavel para temperatura
  49. int humidity;                                               // Variavel para umidade
  50. #define Saida1 4                                        // GPIO4 Port para ligar Controle do triac (Pino 2 do MOC3023)
  51. const int Switch1 = 5;                                  // GPIO5 Port para ligar o interruptor
  52.  
  53. #define Saida2   13                                     // GPIO13 Port para ligar Controle do triac (Pino 2 do MOC3023)
  54. const int Switch2 = 14;                                 // GPIO14 Port para ligar o interruptor
  55.  
  56. #define Saida3 16                                       // GPIO12 Port para ligar Controle do triac (Pino 2 do MOC3023)
  57. const int Switch3 = 12;                                 // GPIO16 Port para ligar o interruptor
  58.  
  59. #define Saida4 15                                        // GPIO0 Port para ligar Controle do triac (Pino 2 do MOC3023)
  60. const int Switch4 = 0;                                 // GPIO15 Port para ligar o interruptor
  61.  
  62. byte Switch1_atual = 0;                                     // Variavel para staus de GPIO5 (Status do interruptor)
  63. byte Switch2_atual = 0;                                     // Variavel para staus de GPIO14 (Status do interruptor)
  64. byte Switch3_atual = 0;                                     // Variavel para staus de GPIO16 (Status do interruptor)
  65. byte Switch4_atual = 0;                                     // Variavel para staus de GPIO15 (Status do interruptor)
  66.  
  67. unsigned long previousMillis = 0;                           // Variavel para medir periodos
  68. const long interval = 2000;                                 // Periodo de leitura da temperatura e umidade
  69. //---------------------------------------
  70. /*void gettemperature()                                       // Funcoa para ler temperatura e umidade
  71.   {
  72.   if (millis() - previousMillis >= interval)                // Se passou o intervalo
  73.   {
  74.     previousMillis = millis();                              // Restaura valor de previousMillis
  75.     humidity = sht1x.readHumidity();                        // Le umidade em SHT10
  76.     temp_c = sht1x.readTemperatureC();                      // Le temperatura em SHT10
  77.   }
  78.   }*/
  79. //---------------------------------------
  80. void setup(void)
  81. {
  82.  // Qif +=  "<p><center>Temperatura: " + String((float)temp_c) + "C </p>";
  83.  // Qif +=  "<p><center>Umidade: " + String((int)humidity) + "% </p>";
  84.   Qif += Q_2;
  85.  
  86.   QlSlBlCl += Q_1;       // 0F                                       // Monta tela pra informar que a luz
  87.   QlSlBlCl += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  88.   QlSlBlCl += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  89.   QlSlBlCl += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  90.   QlSlBlCl += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  91.  
  92.   QlSlBlCd += Q_1;       // 0E                                       // Monta tela pra informar que a luz
  93.   QlSlBlCd += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  94.   QlSlBlCd += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  95.   QlSlBlCd += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  96.   QlSlBlCd += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  97.  
  98.   QlSlBdCl += Q_1;       // 0D                                       // Monta tela pra informar que a luz
  99.   QlSlBdCl += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  100.   QlSlBdCl += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  101.   QlSlBdCl += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  102.   QlSlBdCl += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  103.  
  104.   QlSlBdCd += Q_1;       // 0C                                       // Monta tela pra informar que a luz
  105.   QlSlBdCd += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  106.   QlSlBdCd += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  107.   QlSlBdCd += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  108.   QlSlBdCd += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  109.  
  110.   QlSdBlCl += Q_1;       // 0B                                       // Monta tela pra informar que a luz
  111.   QlSdBlCl += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  112.   QlSdBlCl += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  113.   QlSdBlCl += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  114.   QlSdBlCl += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  115.  
  116.   QlSdBlCd += Q_1;       // 0A                                       // Monta tela pra informar que a luz
  117.   QlSdBlCd += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  118.   QlSdBlCd += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  119.   QlSdBlCd += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  120.   QlSdBlCd += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  121.  
  122.   QlSdBdCl += Q_1;       // 09                                       // Monta tela pra informar que a luz
  123.   QlSdBdCl += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  124.   QlSdBdCl += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  125.   QlSdBdCl += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  126.   QlSdBdCl += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  127.  
  128.   QlSdBdCd += Q_1;       // 08                                       // Monta tela pra informar que a luz
  129.   QlSdBdCd += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  130.   QlSdBdCd += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  131.   QlSdBdCd += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  132.   QlSdBdCd += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  133.  
  134.   QdSlBlCl += Q_1;       // 07                                       // Monta tela pra informar que a luz
  135.   QdSlBlCl += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  136.   QdSlBlCl += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  137.   QdSlBlCl += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  138.   QdSlBlCl += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  139.  
  140.   QdSlBlCd += Q_1;       // 06                                       // Monta tela pra informar que a luz
  141.   QdSlBlCd += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  142.   QdSlBlCd += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  143.   QdSlBlCd += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  144.   QdSlBlCd += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  145.  
  146.   QdSlBdCl += Q_1;       // 05                                       // Monta tela pra informar que a luz
  147.   QdSlBdCl += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  148.   QdSlBdCl += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  149.   QdSlBdCl += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  150.   QdSlBdCl += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  151.  
  152.   QdSlBdCd += Q_1;       // 04                                        // Monta tela pra informar que a luz
  153.   QdSlBdCd += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  154.   QdSlBdCd += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  155.   QdSlBdCd += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  156.   QdSlBdCd += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  157.  
  158.   QdSdBlCl += Q_1;      // 03                                        // Monta tela pra informar que a luz
  159.   QdSdBlCl += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  160.   QdSdBlCl += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  161.   QdSdBlCl += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  162.   QdSdBlCl += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  163.  
  164.   QdSdBlCd += Q_1;      // 02                                        // Monta tela pra informar que a luz
  165.   QdSdBlCd += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  166.   QdSdBlCd += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  167.   QdSdBlCd += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  168.   QdSdBlCd += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  169.  
  170.   QdSdBdCl += Q_1;      // 01                                        // Monta tela pra informar que a luz
  171.   QdSdBdCl += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  172.   QdSdBdCl += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  173.   QdSdBdCl += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  174.   QdSdBdCl += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=on \"><button style=\"background-color: rgb(0, 255, 0);height: 65px; width: 200px;\"><h1>Apagada</h1></button></a>";
  175.  
  176.   QdSdBdCd += Q_1;      // 00                                        // Monta tela pra informar que a luz
  177.   QdSdBdCd += "<p><center>Quarto<a href=\"/Controle?LuzQuarto=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  178.   QdSdBdCd += "<p><center>Sala&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Controle?LuzSala=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  179.   QdSdBdCd += "<p><center>Banho&nbsp;<a href=\"/Controle?LuzBanho=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  180.   QdSdBdCd += "<p><center>Carga&nbsp;<a href=\"/Controle?Carga=off \"><button style=\"background-color: rgb(255, 0, 0);height: 65px; width: 200px;\"><h1>Acesa</h1></button></a>";
  181.  
  182.   pinMode(Switch1, INPUT_PULLUP);                           // Define GPIO5 como entrada e liga o resistor de pullup
  183.   pinMode(Switch2, INPUT_PULLUP);                           // Define GPIO como entrada e liga o resistor de pullup
  184.   pinMode(Switch3, INPUT_PULLUP);                           // Define GPIO como entrada e liga o resistor de pullup
  185.   pinMode(Switch4, INPUT_PULLUP);                           // Define GPIO como entrada e liga o resistor de pullup
  186.   Switch1_atual = digitalRead(Switch1);                     // Atualisa status de GPIO5
  187.   Switch2_atual = digitalRead(Switch2);                     // Atualisa status de GPIO
  188.   Switch3_atual = digitalRead(Switch3);                     // Atualisa status de GPIO
  189.   Switch4_atual = digitalRead(Switch4);                     // Atualisa status de GPIO
  190.   pinMode(Diag, OUTPUT);                                    // Define GPIO2 como saida (So pra diagnostico)
  191.   pinMode(Saida1, OUTPUT);                                  // Define GPIO4 como saida
  192.   pinMode(Saida2, OUTPUT);                                  // Define GPIO como saida
  193.   pinMode(Saida3, OUTPUT);                                  // Define GPIO como saida
  194.   pinMode(Saida4, OUTPUT);                                  // Define GPIO como saida
  195.   digitalWrite(Saida1, HIGH);                               // Liga GPIO
  196.   digitalWrite(Saida2, HIGH);                               // Liga GPIO
  197.   digitalWrite(Saida3, HIGH);                               // Liga GPIO
  198.   digitalWrite(Saida4, HIGH);                               // Liga GPIO
  199.  
  200.   Serial.begin(115200);                                     // Inicialisa o serial port em 115200
  201.   WiFi.begin(ssid, password);                               // Inicialisa o wifi
  202.   Serial.println("");                                       // Imprime
  203.   while (WiFi.status() != WL_CONNECTED)                     // Aguarda conexão
  204.   {
  205.     delay(500);                                             // Delay
  206.     Serial.print(".");                                      // Imprime . enquanto não conecta
  207.   }
  208. //  Serial.print("Switch3 "); Serial.println(digitalRead(Switch3));
  209.   Serial.println("");                                       // Imprime
  210.   Serial.print("Connected to ");
  211.   Serial.println(ssid);
  212.   Serial.print("IP address: ");
  213.   Serial.println(WiFi.localIP());
  214.   if (mdns.begin("esp8266", WiFi.localIP()))                // Se ......????
  215.   {
  216.     Serial.println("MDNS responder started");               // Imprime
  217.   }
  218.   server.on("/", []()                                       // Ao requeste
  219.   {
  220.     server.send(200, "text/html", QlSlBlCl);                    // Executa o HTML Ql (Quarto ligado)
  221.   });
  222.   server.on("/Controle", []()                               // Ao requeste /Quarto
  223.   {
  224.     //gettemperature();                                     // Le temperatura e umidade. Comentar se tiver sem sensor pois fica lento
  225.     LuzQuarto = server.arg("LuzQuarto");                    // Recupera o valor do parametro luz enviado
  226.     LuzSala = server.arg("LuzSala");                        // Recupera o valor do parametro luz enviado
  227.     LuzBanho = server.arg("LuzBanho");                      // Recupera o valor do parametro luz enviado
  228.     Carga = server.arg("Carga");                            // Recupera o valor do parametro luz enviado
  229.     if (LuzQuarto == "off") digitalWrite(Saida1, HIGH);     // Se o valor de luz e off desliga a saida
  230.     if (LuzQuarto == "on") digitalWrite(Saida1, LOW);       // Se o valor de luz e on liga a saida
  231.     if (LuzSala == "off") digitalWrite(Saida2, HIGH);       // Se o valor de luz e off desliga a saida
  232.     if (LuzSala == "on") digitalWrite(Saida2, LOW);         // Se o valor de luz e on liga a saida
  233.     if (LuzBanho == "off") digitalWrite(Saida3, HIGH);      // Se o valor de luz e off desliga a saida
  234.     if (LuzBanho == "on") digitalWrite(Saida3, LOW);        // Se o valor de luz e on liga a saida
  235.     if (Carga == "off") digitalWrite(Saida4, HIGH);         // Se o valor de luz e off desliga a saida
  236.     if (Carga == "on") digitalWrite(Saida4, LOW);           // Se o valor de luz e on liga a saida
  237.  
  238.     digitalWrite(Diag, digitalRead(Saida2));                // Gpio2 = Gpio4 Diagnostico
  239.  
  240.     if ((digitalRead(Saida1) == LOW)  && (digitalRead(Saida2) == LOW)  && (digitalRead(Saida3) == LOW)  && (digitalRead(Saida4) == LOW))  // Se a saida esta ligada, carrega a pagina "ligada"
  241.     {
  242.       Qi += QdSdBdCd;                 // 00                         // Monta tela nova quarto ligado
  243.       Qi += Qif;
  244.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  245.       Qi = "";                                             // Limpa valor de temperatura e umidade
  246.     }
  247.     if ((digitalRead(Saida1) == LOW)  && (digitalRead(Saida2) == LOW)  && (digitalRead(Saida3) == LOW)  && (digitalRead(Saida4) == HIGH))  // Se a saida esta ligada, carrega a pagina "ligada"
  248.     {
  249.       Qi += QdSdBdCl;                  // 01                        // Monta tela nova quarto ligado
  250.       Qi += Qif;
  251.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  252.       Qi = "";                                             // Limpa valor de temperatura e umidade
  253.     }
  254.     if ((digitalRead(Saida1) == LOW)  && (digitalRead(Saida2) == LOW)  && (digitalRead(Saida3) == HIGH)  && (digitalRead(Saida4) == LOW))   // Se a saida esta ligada, carrega a pagina "ligada"
  255.     {
  256.       Qi += QdSdBlCd;                 // 02                         // Monta tela nova quarto ligado
  257.       Qi += Qif;
  258.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  259.       Qi = "";                                             // Limpa valor de temperatura e umidade
  260.     }
  261.     if ((digitalRead(Saida1) == LOW)  && (digitalRead(Saida2) == LOW)  && (digitalRead(Saida3) == HIGH)  && (digitalRead(Saida4) == HIGH))  // Se a saida esta ligada, carrega a pagina "ligada"
  262.     {
  263.       Qi += QdSdBlCl;                   // 03                       // Monta tela nova quarto ligado
  264.       Qi += Qif;
  265.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  266.       Qi = "";                                             // Limpa valor de temperatura e umidade
  267.     }
  268.     if ((digitalRead(Saida1) == LOW)  && (digitalRead(Saida2) == HIGH)  && (digitalRead(Saida3) == LOW)  && (digitalRead(Saida4) == LOW))  // Se a saida esta ligada, carrega a pagina "ligada"
  269.     {
  270.       Qi += QdSlBdCd;                   // 04                       // Monta tela nova quarto ligado
  271.       Qi += Qif;
  272.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  273.       Qi = "";                                             // Limpa valor de temperatura e umidade
  274.     }
  275.     if ((digitalRead(Saida1) == LOW)  && (digitalRead(Saida2) == HIGH)  && (digitalRead(Saida3) == LOW)  && (digitalRead(Saida4) == HIGH))  // Se a saida esta ligada, carrega a pagina "ligada"
  276.     {
  277.       Qi += QdSlBdCl;                   // 05                       // Monta tela nova quarto ligado
  278.       Qi += Qif;
  279.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  280.       Qi = "";                                             // Limpa valor de temperatura e umidade
  281.     }
  282.     if ((digitalRead(Saida1) == LOW)  && (digitalRead(Saida2) == HIGH)  && (digitalRead(Saida3) == HIGH)  && (digitalRead(Saida4) == LOW))   // Se a saida esta ligada, carrega a pagina "ligada"
  283.     {
  284.       Qi += QdSlBlCd;                    // 06                      // Monta tela nova quarto ligado
  285.       Qi += Qif;
  286.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  287.       Qi = "";                                             // Limpa valor de temperatura e umidade
  288.     }
  289.     if ((digitalRead(Saida1) == LOW)  && (digitalRead(Saida2) == HIGH)  && (digitalRead(Saida3) == HIGH)  && (digitalRead(Saida4) == HIGH))   // Se a saida esta ligada, carrega a pagina "ligada"
  290.     {
  291.       Qi += QdSlBlCl;                    // 07                      // Monta tela nova quarto ligado
  292.       Qi += Qif;
  293.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  294.       Qi = "";                                             // Limpa valor de temperatura e umidade
  295.     }
  296.     if ((digitalRead(Saida1) == HIGH)  && (digitalRead(Saida2) == LOW)  && (digitalRead(Saida3) == LOW)  && (digitalRead(Saida4) == LOW))   // Se a saida esta ligada, carrega a pagina "ligada"
  297.     {
  298.       Qi += QlSdBdCd;                    // 08                      // Monta tela nova quarto ligado
  299.       Qi += Qif;
  300.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  301.       Qi = "";                                             // Limpa valor de temperatura e umidade
  302.     }
  303.     if ((digitalRead(Saida1) == HIGH)  && (digitalRead(Saida2) == LOW)  && (digitalRead(Saida3) == LOW)  && (digitalRead(Saida4) == HIGH))   // Se a saida esta desligada, carrega a pagina "desligada"
  304.     {
  305.       Qi += QlSdBdCl;                    // 09                      // Monta tela nova quarto desligado
  306.       Qi += Qif;
  307.       server.send(200, "text/html", Qi);                   // Mostra Quarto desligado
  308.       Qi = "";                                             // Limpa valor de temperatura e umidade
  309.     }
  310.     if ((digitalRead(Saida1) == HIGH)  && (digitalRead(Saida2) == LOW)  && (digitalRead(Saida3) == HIGH)  && (digitalRead(Saida4) == LOW))   // Se a saida esta ligada, carrega a pagina "ligada"
  311.     {
  312.       Qi += QlSdBlCd;                   // 0A                       // Monta tela nova quarto ligado
  313.       Qi += Qif;
  314.       server.send(200, "text/html", Qi);                   // Mostra Quarto ligado
  315.       Qi = "";                                             // Limpa valor de temperatura e umidade
  316.     }
  317.     if ((digitalRead(Saida1) == HIGH)  && (digitalRead(Saida2) == LOW)  && (digitalRead(Saida3) == HIGH)  && (digitalRead(Saida4) == HIGH))   // Se a saida esta desligada, carrega a pagina "desligada"
  318.     {
  319.       Qi += QlSdBlCl;                    // 0B                      // Monta tela nova quarto desligado
  320.       Qi += Qif;
  321.       server.send(200, "text/html", Qi);                   // Mostra Quarto desligado
  322.       Qi = "";                                             // Limpa valor de temperatura e umidade
  323.     }
  324.     if ((digitalRead(Saida1) == HIGH)  && (digitalRead(Saida2) == HIGH)  && (digitalRead(Saida3) == LOW)  && (digitalRead(Saida4) == LOW))   // Se a saida esta desligada, carrega a pagina "desligada"
  325.     {
  326.       Qi += QlSlBdCd;                   // 0C                       // Monta tela nova quarto desligado
  327.       Qi += Qif;
  328.       server.send(200, "text/html", Qi);                   // Mostra Quarto desligado
  329.       Qi = "";                                             // Limpa valor de temperatura e umidade
  330.     }
  331.     if ((digitalRead(Saida1) == HIGH)  && (digitalRead(Saida2) == HIGH)  && (digitalRead(Saida3) == LOW)  && (digitalRead(Saida4) == HIGH))   // Se a saida esta desligada, carrega a pagina "desligada"
  332.     {
  333.       Qi += QlSlBdCl;                     // 0D                     // Monta tela nova quarto desligado
  334.       Qi += Qif;
  335.       server.send(200, "text/html", Qi);                   // Mostra Quarto desligado
  336.       Qi = "";                                             // Limpa valor de temperatura e umidade
  337.     }
  338.     if ((digitalRead(Saida1) == HIGH)  && (digitalRead(Saida2) == HIGH)  && (digitalRead(Saida3) == HIGH)  && (digitalRead(Saida4) == LOW))   // Se a saida esta desligada, carrega a pagina "desligada"
  339.     {
  340.       Qi += QlSlBlCd;                      // 0E                    // Monta tela nova quarto desligado
  341.       Qi += Qif;
  342.       server.send(200, "text/html", Qi);                   // Mostra Quarto desligado
  343.       Qi = "";                                             // Limpa valor de temperatura e umidade
  344.     }
  345.     if ((digitalRead(Saida1) == HIGH)  && (digitalRead(Saida2) == HIGH)  && (digitalRead(Saida3) == HIGH)  && (digitalRead(Saida4) == HIGH))   // Se a saida esta desligada, carrega a pagina "desligada"
  346.     {
  347.       Qi += QlSlBlCl;                     // 0F                     // Monta tela nova quarto desligado
  348.       Qi += Qif;
  349.       server.send(200, "text/html", Qi);                   // Mostra Quarto desligado
  350.       Qi = "";                                             // Limpa valor de temperatura e umidade
  351.     }
  352.     delay(100);                                             // Delay
  353.   });
  354.   server.begin();                                           // Inicaliza servidor
  355.   Serial.println("HTTP server started");                    // Imprime
  356. }
  357. //--------------------------------------------------
  358. void loop(void)
  359. {
  360. //  Serial.print("Switch3 "); Serial.println(digitalRead(Switch3));
  361. //  Serial.print("Switch4 "); Serial.println(digitalRead(Switch4));
  362.   server.handleClient();                                    // Executa instancia
  363.   if (digitalRead(Switch1) !=  Switch1_atual)               // Se o valor do SW alterou
  364.   {
  365.     delay(40);                                              // Delay
  366.     if (digitalRead(Switch1) !=  Switch1_atual)             // Se o valor do SW alterou
  367.     {
  368.       digitalWrite(Saida1, !digitalRead(Saida1));           // Inverte a saida lamp1
  369.       digitalWrite(Diag, digitalRead(Saida1));              // Gpio2 = Gpio4     Diagnostico
  370.       Switch1_atual = digitalRead(Switch1);                 // Atualisa o Gpio5 atual
  371.     }
  372.   }
  373.   if (digitalRead(Switch2) !=  Switch2_atual)                 // Se o valor do SW alterou
  374.   {
  375.     delay(40);                                              // Delay
  376.     if (digitalRead(Switch2) !=  Switch2_atual)             // Se o valor do SW alterou
  377.     {
  378.       digitalWrite(Saida2, !digitalRead(Saida2));           // Inverte a saida lamp2
  379.       Switch2_atual = digitalRead(Switch2);                 // Atualisa o Gpio14 atual
  380.     }
  381.   }
  382.   if (digitalRead(Switch3) !=  Switch3_atual)                 // Se o valor do SW alterou
  383.   {
  384.     delay(40);                                              // Delay
  385.     if (digitalRead(Switch3) !=  Switch3_atual)             // Se o valor do SW alterou
  386.     {
  387.       digitalWrite(Saida3, !digitalRead(Saida3));           // Inverte a saida lamp3
  388.       Switch3_atual = digitalRead(Switch3);                 // Atualisa o Gpio atual
  389.     }
  390.   }
  391.   if (digitalRead(Switch4) !=  Switch4_atual)                 // Se o valor do SW alterou
  392.   {
  393.     delay(40);                                              // Delay
  394.     if (digitalRead(Switch4) !=  Switch4_atual)             // Se o valor do SW alterou
  395.     {
  396.       digitalWrite(Saida4, !digitalRead(Saida4));           // Inverte a saida lamp2
  397.       Switch4_atual = digitalRead(Switch4);                 // Atualisa o Gpio14 atual
  398.     }
  399.   }
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement