/*
AT
AT+CWMODE=1
AT+CWLAP
AT+CWJAP="Wokwi-GUEST",
AT+CIFSR
AT+CIPMUX=1
AT+CIPSERVER=1,80
*/
#include <SoftwareSerial.h>
#define DEBUG true
//RX pino 2, TX pino 3
/*
* Adaptador Arduino
* RX -> 3(TX)
* TX -> 2(RX)
*/
SoftwareSerial esp8266(2, 3);
void setup() {
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
esp8266.begin(115200);
Serial.println("** Verificando conexao **");
Serial.println();
delay(1000);
// Reinicializar parametros ...
sendData("AT\r\n", 2000, DEBUG); // rst
delay(1000);
// Ativar mode de operação para estação ...
sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
delay(1000);
// Conectar a rede wireless ...
sendData("AT+CWJAP=\"Wokwi-GUEST\",\r\n", 2000, DEBUG);
delay(3000);
// Mostrar o endereco IP ...
sendData("AT+CIFSR\r\n", 1000, DEBUG);
delay(1000);
// Configurar para multiplas conexoes
sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);
// Iniciar o web server na porta 80
sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG);
}
void loop() {
// Verifica se o ESP8266 esta enviando dados
Serial.println("Aguardando solicitacao ...");
if (esp8266.available())
{
if (esp8266.find("+IPD,"))
{
delay(300);
int connectionId = esp8266.read() - 48;
String webpage = "<head><meta http-equiv=""refresh"" content=""3"">";
webpage += "</head><h1><u>ESP8266 - Web Server</u></h1><h2>Porta";
webpage += "Digital 8: ";
int a = digitalRead(8);
webpage += a;
webpage += "<h2>Porta Digital 9: ";
int b = digitalRead(9);
webpage += b;
webpage += "</h2>";
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += webpage.length();
cipSend += "\r\n";
sendData(cipSend, 1000, DEBUG);
sendData(webpage, 1000, DEBUG);
String closeCommand = "AT+CIPCLOSE=";
closeCommand += connectionId; // append connection id
closeCommand += "\r\n";
sendData(closeCommand, 3000, DEBUG);
}
}
}
String sendData(String command, const int timeout, boolean debug)
{
// Envio dos comandos AT para o modulo
String response = "";
esp8266.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response += c;
}
}
if (debug)
{
Serial.println(response);
}
return response;
}