Advertisement
Guest User

Tiago Alexandre (v.01)

a guest
Apr 4th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.64 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include "RTClib.h"
  3. #include <Wire.h>
  4.  
  5. //rtc
  6. RTC_DS1307 rtc;
  7. //char hora[30];
  8.  
  9.  
  10. //rele
  11.  
  12. #define DEBUG false        // Se "true" o codigo printa a resposta do modulo a cada operacao
  13. #define rele1_pino 5       // Pino conectado ao rele 1
  14. #define rele2_pino 6
  15. #define rele3_pino 7
  16. #define rele4_pino 8
  17.  
  18.  
  19. //########## MUDE AQUI PARA OS PARAMETROS DA SUA REDE WIFI #################
  20. String meu_IP = "192.168.0.142"; // Escolha um IP compativel com sua rede local
  21. String minha_SSID = "xxxx";// SSID da sua rede wifi
  22. String minha_senha = "xxxx"; // senha da sua rede wifi
  23. //##########################################################################
  24.  
  25. SoftwareSerial Debug(10, 11); // Cria serial em 10 e 11 para debug. Permite leitura das respostas do modulo
  26.  
  27. bool rele1 = false;        // Estado do rele. true/ligado ou false/desligado
  28. bool rele2 = false;
  29. bool rele3 = false;
  30. bool rele4 = false;
  31.  
  32.  
  33. void setup()
  34. {
  35.   pinMode(rele1_pino, OUTPUT);      // Define pino como saida
  36.   pinMode(rele2_pino, OUTPUT);
  37.   pinMode(rele3_pino, OUTPUT);
  38.   pinMode(rele4_pino, OUTPUT);
  39.  
  40.   Debug.begin(115200);
  41.   Serial.begin(115200); // your esp's baud rate might be different
  42.  
  43.   //rtc
  44.   rtc.begin();
  45.   if (! rtc.begin()) {
  46.     Serial.println("Couldn't find RTC");
  47.     while (1);
  48.   }
  49.  
  50.   if ( rtc.isrunning()) {
  51.     Serial.println("RTC is NOT running!");
  52.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  53.   }
  54.   ///
  55.   sendData("AT+RST\r\n", 3000, DEBUG); // reseta o modulo
  56.   sendData("AT+CWMODE=1\r\n", 1000, DEBUG); // configura o modulo como station
  57.   sendData("AT+CWJAP=\"" + minha_SSID + "\",\"" + minha_senha + "\"\r\n", 10000, DEBUG); // loga na rede wifi
  58.   sendData("AT+CIPSTA=\"" + meu_IP + "\"\r\n", 1000, DEBUG); // Define um endereco de IP para o modulo
  59.   sendData("AT+CIFSR\r\n", 2000, DEBUG); // Informa endereco de IP
  60.   sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // Configura para aceitar mais de uma conexão por vez.
  61.   sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // Habilita servidor na porta 80. O modulo passa a responder nessa porta
  62.  
  63. }
  64.  
  65. void loop()
  66. {
  67.  
  68.   if (Serial.available()) // Se o ESP8266 estiver mandando caracteres para a AutomationBoard
  69.   {
  70.     if (Serial.find("+IPD,")) // Se encontrou "+IPD," o proximo caracter sera o ID da conexao
  71.     {
  72.       delay(1000);
  73.       int connectionId = Serial.read() - 48; // subtrai 48 porque a funcao read() retorna um caracter ASCII, subtraindo 48 teremos um int correspondente (primeiro decimal comeca em 48)
  74.       int muda_rele = 0; // Variavel que define qual rele sera alterado, de acordo com o link que o usuario clicou
  75.       if (Serial.find("rele")) {
  76.         muda_rele = Serial.read() - 48;
  77.       }
  78.       if (muda_rele == 1) {
  79.         digitalWrite(rele1_pino, !rele1);  // Se usuario clicou no 1o link, muda estado do rele1
  80.         rele1 = !rele1;
  81.       }
  82.       if (muda_rele == 2) {
  83.         digitalWrite(rele2_pino, !rele2);  // Se usuario clicou no 2o link, muda estado do rele2
  84.         rele2 = !rele2;
  85.       }
  86.       if (muda_rele == 3) {
  87.         digitalWrite(rele3_pino, !rele3);  // Se usuario clicou no 3o link, muda estado do rele3
  88.         rele3 = !rele3;
  89.       }
  90.       if (muda_rele == 4) {
  91.         digitalWrite(rele4_pino, !rele4);  // Se usuario clicou no 4o link, muda estado do rele4
  92.         rele4 = !rele4;
  93.       }
  94.      
  95.       //
  96.       // manipula data/hora de outra maneira
  97.       //
  98.      
  99.       DateTime now = rtc.now();
  100.       String horas = "";
  101.       String horas += now.hour();
  102.       String horas += ":";
  103.       String horas += now.minute();
  104.       String horas += ":";
  105.       String horas += now.second();
  106.      
  107.       //
  108.       //
  109.       //
  110.  
  111.       String webpage = "";
  112.       webpage += "<html><head></head><body><center>"; //"<html><head><meta http-equiv='refresh' content='10'></head><body><center>";  //trecho com meta tag trava  a pagina somado ao sprintf da hora
  113.  
  114.       webpage += "<img src=\"http://goo.gl/IoorRu\" width=\"250\">"; // parte que trava a pagina
  115.       webpage += "<br><br><h2>Web Server com ESP8266 e AutomationBoard</h2><br>"; // parte que trava a pagina
  116.      
  117.       webpage += "<h3>";
  118.       webpage += horas; // imprime a string hora na pagina
  119.       webpage += "</h3>";
  120.       //
  121.       webpage += "<a href=\"http://";
  122.       webpage += meu_IP;
  123.       webpage += "/rele1\"><h2>Rele 1 ";
  124.       if (rele1) {
  125.         webpage += "ligado</h2></a>";
  126.       } else {
  127.         webpage += "desligado</h2></a>";
  128.       }
  129.       webpage += "<a href=\"http://";
  130.       webpage += meu_IP;
  131.       webpage += "/rele2\"><h2>Rele 2 ";
  132.       if (rele2) {
  133.         webpage += "ligado</h2></a>";
  134.       } else {
  135.         webpage += "desligado</h2></a>";
  136.       }
  137.       webpage += "<a href=\"http://";
  138.       webpage += meu_IP;
  139.       webpage += "/rele3\"><h2>Rele 3 ";
  140.       if (rele3) {
  141.         webpage += "ligado</h2></a>";
  142.       } else {
  143.         webpage += "desligado</h2></a>";
  144.       }
  145.       webpage += "<a href=\"http://";
  146.       webpage += meu_IP;
  147.       webpage += "/rele4\"><h2>Rele 4 ";
  148.       if (rele4) {
  149.         webpage += "ligado</h2></a>";
  150.       } else {
  151.         webpage += "desligado</h2></a>";
  152.       }
  153.       webpage += "</center></body></html>";
  154.  
  155.  
  156.  
  157.       String cipSend = "AT+CIPSEND=";  // Variavel com o comando para enviar dados para o dispositivo que se comunica com o modulo
  158.       cipSend += connectionId;         // ID da conexao
  159.       cipSend += ",";                  // separacao
  160.       cipSend += webpage.length();     // tamanho do pacote de dados a ser enviado
  161.       cipSend += "\r\n";               // fim do comando
  162.  
  163.       sendData(cipSend, 1000, DEBUG);  // envia comando para o modulo
  164.       sendData(webpage, 3000, DEBUG);  // envia a pagina para responder ao acesso de algum dispositivo (ex: usuario)
  165.  
  166.       String closeCommand = "AT+CIPCLOSE=";   // Variavel com o comando para fechar a conexao
  167.       closeCommand += connectionId;           // ID da conexao
  168.       closeCommand += "\r\n";                 // fim do comando
  169.  
  170.       sendData(closeCommand, 1000, DEBUG);    // Envia comando para fechar a conexao
  171.     }
  172.   }
  173. }
  174.  
  175.  
  176. String sendData(String command, const int timeout, boolean debug)
  177. {
  178.   String response = "";  // variavel que armazena a resposta do modulo
  179.  
  180.   Serial.print(command); // envia o comando para o modulo
  181.  
  182.   long int tempo = millis();
  183.  
  184.   while ( (tempo + timeout) > millis())
  185.   {
  186.     while (Serial.available() > 0)
  187.     {
  188.       char c = Serial.read(); // le e armazena os caracteres da resposta do modulo
  189.       response += c;
  190.     }
  191.   }
  192.  
  193.   if (debug)    // Se debug estiver habilitado, imprime mensagens de resposta do modulo
  194.   {
  195.     Debug.print(response);
  196.   }
  197.  
  198.   return response;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement