/***************** LIBRARIES ***********************/ #include /***************************************************/ /**************** CONSTANTES **********************/ #define SSID "xxxxxxxxxxxxxx" //nome da rede de WIFI a ligar #define PASS "xxxxxxxxxxxxxxxxxxxxx" //password da rede WIFI #define pinLED 13 //pino ligação led #define debug true SoftwareSerial ESP8266(9, 8); //(TX 9), (RX 8) do ESP8266 /***************************************************/ /**************** VARIAVEIS **********************/ /***************************************************/ void setup() { Serial.begin(9600); //velocidade de comunicacao por porta serie com o PC por USB ESP8266.begin(9600); //velocidade de comunicacao por porta serie com o ESP8266 pinMode(pinLED, OUTPUT); //define o pinLED como saída digital connectWiFi(); //ligacao à rede WIFI digitalWrite(pinLED, HIGH); delay(1000); digitalWrite(pinLED, LOW); Serial.println("Pronto para receber comandos"); } void loop() { if (ESP8266.available()) //verifica se o ESP está a enviar uma mensagem { if (ESP8266.find("+IPD,")) { delay(1000); unsigned int connectionId = ESP8266.read() - 48; // subtract 48 because the read() function returns // the ASCII decimal value and 0 (the first decimal number) starts at 48 ESP8266.find("comando="); // advance cursor to "comando=" int comando = (ESP8266.read() - 48) * 10; comando += (ESP8266.read() - 48); Serial.print("recebido: "); Serial.println(String(comando)); String content = "Comando " + String(comando) + " executado com sucesso."; sendHTTPResponse(connectionId, content); switch(comando){ case 10: digitalWrite(pinLED,HIGH); break; case 11: digitalWrite(pinLED,LOW); break; } } } } /* * Nome: sendHTTPResponse * Descrição: Função utilizada para responder ao envio do comando pelo browser * Params: connectionId, content * Returns: TRUE se consegui conectar corretamente e FALSE se não conseguir conectar-se à rede WUIFI */ void sendHTTPResponse(unsigned int connectionId, String content) { // build HTTP response String httpHeader; // HTTP Header httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n"; httpHeader += "Content-Length: "; httpHeader += content.length(); httpHeader += "\r\n"; httpHeader += "Connection: close\r\n\r\n"; httpHeader += content; httpHeader += " "; String cipSend = "AT+CIPSEND="; cipSend += connectionId; cipSend += ","; cipSend += httpHeader.length(); //cipSend +="\r\n"; ComandoAT(cipSend, 1000); ComandoAT(httpHeader, 1000); } /* * Nome: conectarWIFI * Descrição: Função utilizada para ligacao ao WIFI * Params: - * Returns: TRUE se consegui conectar corretamente e FALSE se não conseguir conectar-se à rede WUIFI */ boolean connectWiFi() { ESP8266.println("AT"); delay(1000); if (ESP8266.find("OK")) { ComandoAT("AT+RST", 2000); //reset ESP ComandoAT("AT+CWMODE=1", 1000); //configura acesspoint String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\""; ComandoAT(cmd, 8000); ComandoAT("AT+CIFSR", 1000); ComandoAT("AT+CIPMUX=1", 1000); ComandoAT("AT+CIPSERVER=1,80", 8000); Serial.println("LIGACAO CONCLUIDA COM SUCESSO."); return true; } else { ComandoAT("AT+CWMODE=1", 1000); //configura acesspoint ComandoAT("AT+RST", 2000); //reset ESP Serial.println("\n!!!!! ESP DESLIGADO !!!!!"); delay(3000); connectWiFi(); return false; } } /* * Name: ComandoAT * Description: Função para enviar comandos AT para o ESP8266. * Params: comando - comando AT para enviar; timeout - tempo de espera de uma resposta do ESP; debug - Envia para a porta serie USB?(true = sim, false = não) * Returns: devolve a resposta do ESP */ String ComandoAT(String comando, const int timeout) { String resposta = ""; //variavel para armazenar a resposta do ESP ESP8266.println(comando); //Envia os dados para o ESP para executar o comando long int time = millis(); //variaval para armazenar os millis atuais while ( (time + timeout) > millis()) { while (ESP8266.available()) //utilizado para armazenar o resultado enviado pelo ESP { char c = ESP8266.read(); resposta += c; } } if (debug) //se estiver em modo debug imprime para a porta serie do USB { Serial.println(resposta); } return resposta; }