Created
July 11, 2015 20:58
-
-
Save diegorv/53627b0575d556c70b0f to your computer and use it in GitHub Desktop.
arduino esp8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Programa: Web Server com modulo ESP8266 | |
// Alteracoes e adaptacoes: FILIPEFLOP | |
#include "ESP8266.h" | |
#include "SoftwareSerial.h" | |
// Cria uma serial nas portas 2 (RX) e 3 (TX) | |
SoftwareSerial esp8266(2 , 3); | |
// Define que o modulo ira utilizar a serial minhaSerial | |
ESP8266 wifi(esp8266); | |
#define DEBUG true | |
void setup() | |
{ | |
Serial.begin(9600); | |
esp8266.begin(19200); | |
sendData("AT+RST\r\n", 2000, DEBUG); // rst | |
// Conecta a rede wireless | |
sendData("AT+CWJAP=\"drv wifi\",\"eita9999\"\r\n", 2000, DEBUG); | |
delay(3000); | |
sendData("AT+CWMODE=1\r\n", 1000, DEBUG); | |
// Mostra o endereco IP | |
sendData("AT+CIFSR\r\n", 1000, DEBUG); | |
// Configura para multiplas conexoes | |
sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); | |
// Inicia o web server na porta 80 | |
sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); | |
} | |
void loop() | |
{ | |
// Verifica se o ESP8266 esta enviando dados | |
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.print(response); | |
} | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment