Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [code]
- // Esp arduino webserver
- #include <SoftwareSerial.h>
- #include "RTClib.h"
- #include <Wire.h>
- //rtc
- RTC_DS1307 rtc;
- char hora[30];
- //rele
- #define DEBUG false // Se "true" o codigo printa a resposta do modulo a cada operacao
- #define rele1_pino 5 // Pino conectado ao rele 1
- #define rele2_pino 6
- #define rele3_pino 7
- #define rele4_pino 8
- ////########## MUDE AQUI PARA OS PARAMETROS DA SUA REDE WIFI #################
- //String meu_IP = "192.168.0.142"; // Escolha um IP compativel com sua rede local
- //String minha_SSID = "xxxxxxx";// SSID da sua rede wifi
- //String minha_senha ="xxxxx"; // senha da sua rede wifi
- ////##########################################################################
- SoftwareSerial Debug(10, 11); // Cria serial em 10 e 11 para debug. Permite leitura das respostas do modulo
- bool rele1 = false; // Estado do rele. true/ligado ou false/desligado
- bool rele2 = false;
- bool rele3 = false;
- bool rele4 = false;
- void setup()
- {
- pinMode(rele1_pino, OUTPUT); // Define pino como saida
- pinMode(rele2_pino, OUTPUT);
- pinMode(rele3_pino, OUTPUT);
- pinMode(rele4_pino, OUTPUT);
- Debug.begin(115200);
- Serial.begin(115200); // your esp's baud rate might be different
- //rtc
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- while (1);
- }
- if ( rtc.isrunning()) {
- Serial.println("RTC is NOT running!");
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- }
- ///
- sendData("AT+RST\r\n", 3000, DEBUG); // reseta o modulo
- sendData("AT+CWMODE=1\r\n", 1000, DEBUG); // configura o modulo como station
- sendData("AT+CWJAP=\"" + minha_SSID + "\",\"" + minha_senha + "\"\r\n", 10000, DEBUG); // loga na rede wifi
- sendData("AT+CIPSTA=\"" + meu_IP + "\"\r\n", 1000, DEBUG); // Define um endereco de IP para o modulo
- sendData("AT+CIFSR\r\n", 2000, DEBUG); // Informa endereco de IP
- sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // Configura para aceitar mais de uma conexão por vez.
- sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // Habilita servidor na porta 80. O modulo passa a responder nessa porta
- }
- void loop()
- {
- //
- DateTime now = rtc.now();
- sprintf( hora, "%02d:%02d:%02d", now.hour(), now.minute(), now.second() ); // converte char hora em string 01:01:01
- //
- if (Serial.available()) // Se o ESP8266 estiver mandando caracteres para a AutomationBoard
- {
- if (Serial.find("+IPD,")) // Se encontrou "+IPD," o proximo caracter sera o ID da conexao
- {
- delay(1000);
- 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)
- int muda_rele = 0; // Variavel que define qual rele sera alterado, de acordo com o link que o usuario clicou
- if (Serial.find("rele")) {
- muda_rele = Serial.read() - 48;
- }
- if (muda_rele == 1) {
- digitalWrite(rele1_pino, !rele1); // Se usuario clicou no 1o link, muda estado do rele1
- rele1 = !rele1;
- }
- if (muda_rele == 2) {
- digitalWrite(rele2_pino, !rele2); // Se usuario clicou no 2o link, muda estado do rele2
- rele2 = !rele2;
- }
- if (muda_rele == 3) {
- digitalWrite(rele3_pino, !rele3); // Se usuario clicou no 3o link, muda estado do rele3
- rele3 = !rele3;
- }
- if (muda_rele == 4) {
- digitalWrite(rele4_pino, !rele4); // Se usuario clicou no 4o link, muda estado do rele4
- rele4 = !rele4;
- }
- String webpage = "";
- webpage += "<html><head></head><body><center>"; //
- webpage += "<img src='http://goo.gl/IoorRu' width='250' />";
- // webpage += "<br>ESP8266 - Web Server<br />";
- // webpage += "<h3>";
- // webpage += String(hora);// imprime a string hora na pagina
- // webpage += "</h3>";
- webpage += "<a href=\"http://";
- webpage += meu_IP;
- webpage += "/rele1\"><h3>Rele 1 ";
- if (rele1) {
- webpage += "ligado</h3></a>";
- } else {
- webpage += "desligado</h3></a>";
- }
- webpage += "<a href=\"http://";
- webpage += meu_IP;
- webpage += "/rele2\"><h3>Rele 2 ";
- if (rele2) {
- webpage += "ligado</h3></a>";
- } else {
- webpage += "desligado</h3></a>";
- }
- webpage += "<a href=\"http://";
- webpage += meu_IP;
- webpage += "/rele3\"><h3>Rele 3 ";
- if (rele3) {
- webpage += "ligado</h3></a>";
- } else {
- webpage += "desligado</h3></a>";
- }
- webpage += "<a href=\"http://";
- webpage += meu_IP;
- webpage += "/rele4\"><h3>Rele 4 ";
- if (rele4) {
- webpage += "ligado</h3></a>";
- } else {
- webpage += "desligado</h3></a>";
- }
- webpage += "</center></body></html>";
- String cipSend = "AT+CIPSEND="; // Variavel com o comando para enviar dados para o dispositivo que se comunica com o modulo
- cipSend += connectionId; // ID da conexao
- cipSend += ","; // separacao
- cipSend += webpage.length(); // tamanho do pacote de dados a ser enviado
- cipSend += "\r\n"; // fim do comando
- sendData(cipSend, 1000, DEBUG); // envia comando para o modulo
- sendData(webpage, 3000, DEBUG); // envia a pagina para responder ao acesso de algum dispositivo (ex: usuario)
- String closeCommand = "AT+CIPCLOSE="; // Variavel com o comando para fechar a conexao
- closeCommand += connectionId; // ID da conexao
- closeCommand += "\r\n"; // fim do comando
- sendData(closeCommand, 1000, DEBUG); // Envia comando para fechar a conexao
- }
- }
- }
- String sendData(String command, const int timeout, boolean debug)
- {
- String response = ""; // variavel que armazena a resposta do modulo
- Serial.print(command); // envia o comando para o modulo
- long int tempo = millis();
- while ( (tempo + timeout) > millis())
- {
- while (Serial.available() > 0)
- {
- char c = Serial.read(); // le e armazena os caracteres da resposta do modulo
- response += c;
- }
- }
- if (debug) // Se debug estiver habilitado, imprime mensagens de resposta do modulo
- {
- Debug.print(response);
- }
- return response;
- }
- [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement