/*
AT Firmware simulation example
Use the serial monitor to interface with the AT firmware. For instance, to scan for WiFi:
AT+CWMODE=1
AT+CWLAP
Connect to WiFi:
AT+CWJAP="Wokwi-GUEST",
Show the current IP address:
AT+CIFSR
Show firmware version
AT+GMR
To show list list of available commands:
AT+CMD?
AT
AT+CWMODE=1
AT+CWLAP
AT+CWJAP="Wokwi-GUEST",
AT+CIFSR
AT+CIPMUX=1
AT+CIPSERVER=1,80
*/
//Programa: Comandos AT com ESP8266
//Guia do Usuario do ESP8266
#include <SoftwareSerial.h>
#include <DHT.h>
#include<ESP8266WiFi.h>
//RX pino 2, TX pino 3
/*
* Adaptador Arduino
* RX -> 3(TX)
* TX -> 2(RX)
*/
SoftwareSerial esp8266(2, 3);
#define DEBUG true
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
esp8266.begin(115200); // baud rate padrão do ESP8266
dht.begin();
Serial.println("** Verificando conexao **");
Serial.println();
delay(1000);
// Reinicializar parametros ...
sendData("AT+RST\r\n", 2000, DEBUG); // rst
delay(1000);
// Ativar mode de operação para estação ...
Serial.println("Alterando para mode de estação");
sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
delay(1000);
Serial.println("Lista de redes");
sendData("AT+CWLAP\r\n", 2000, DEBUG);
delay(1000);
// Conectar a rede wireless ...
Serial.println("Conectar na rede");
sendData("AT+CWJAP=\"Wokwi-GUEST\",\r\n", 2000, DEBUG);
delay(3000);
// Mostrar o endereco IP ...
Serial.println("Mostrar endereço IP");
sendData("AT+CIFSR\r\n", 1000, DEBUG);
Serial.println();
Serial.println("** Final **");
}
void loop() {
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Umidade: ");
Serial.print(h);
Serial.print("% - Temperatura: ");
Serial.print(t);
Serial.print("C");
Serial.println();
}
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;
}