Last active
June 9, 2020 15:02
-
-
Save jacksonn455/ebfed74fbace976f8c2060ad2c44b30d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
const char* ssid = "****************"; | |
const char* password = "**************"; | |
ESP8266WebServer server(80); //objeto webserver na porta 80 | |
const int led = 2; | |
void handleRoot() { // requisita pagina raiz | |
digitalWrite(led, 1); // led pisca | |
String textoHTML; | |
textoHTML = "Ola!! Aqui é o <b>ESP8266</b> falando!<br> "; | |
textoHTML += "Porta A0: "; | |
textoHTML += analogRead(A0); | |
server.send(200, "text/html", textoHTML); | |
digitalWrite(led, 0); | |
} | |
void handleNotFound(){ // caso a pagina nao existe | |
digitalWrite(led, 1); | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET)?"GET":"POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for (uint8_t i=0; i<server.args(); i++){ | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
digitalWrite(led, 0); | |
} | |
void setup(void){ | |
pinMode(led, OUTPUT); | |
digitalWrite(led, 0); //led desligado | |
Serial.begin(115200); // serve para testar a conexao do monitor serial com velocidade 115200 | |
WiFi.mode(WIFI_STA); // modo de conexao | |
WiFi.begin(ssid, password); // passa login e senha | |
Serial.println(""); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); // se ficar o ponto, tem algum erro | |
} | |
Serial.println(""); // se parar o ponto ele conectou | |
Serial.print("Conectado em: "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
if (MDNS.begin("esp8266")) { | |
Serial.println("MDNS responder started"); | |
} | |
server.on("/", handleRoot); | |
server.on("/inline", [](){ // deixa o servidor ligado | |
server.send(200, "text/plain", "this works as well"); | |
}); | |
server.onNotFound(handleNotFound); | |
server.begin(); // server ativo | |
Serial.println("Servidor iniciado"); | |
} | |
void loop(void){ | |
server.handleClient(); //sempre ouvindo conexao | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment