Created
May 8, 2020 18:32
-
-
Save YhanChristian/fe2fe8a474ccc9c5443f738e30931a89 to your computer and use it in GitHub Desktop.
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
/* WebServer com NodeMCU e AJAX | |
Utilizado: NodeMCU, sensor de tempratura LM35 | |
Autor: Yhan Christian Souza Silva - Data: 21/07/2018 | |
*/ | |
// -- Bibliotecas Auxiliares -- | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
// -- Definições de Hardware -- | |
#define tempSensor A0 | |
// -- Definições de rede -- | |
const char* ssid = "xxxx"; | |
const char* password = "xxxx"; | |
// -- Escutar porta 80 -- | |
ESP8266WebServer server(80); | |
// -- Variáveis e constantes -- | |
String webSite, javaScript, XML; | |
// -- Escopo funções auxiliares -- | |
void connectToWiFi(); | |
void buildWebSite(); | |
void buildJavaScript(); | |
void buildXML(); | |
float getTemp(); | |
void handleWebSite(); | |
void handleXML(); | |
// -- Setup -- | |
void setup() { | |
Serial.begin(115200); | |
connectToWiFi(); | |
delay(1000); | |
} | |
// -- Loop -- | |
void loop() { | |
server.handleClient(); | |
} | |
// -- Funções Auxiliares -- | |
void connectToWiFi() { | |
Serial.println("Conectando a rede: "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
WiFi.mode(WIFI_STA); | |
Serial.println(""); | |
Serial.println("Conectado!"); | |
Serial.print("IP: "); | |
Serial.println(WiFi.localIP()); | |
server.on("/", handleWebSite); | |
server.on("/xml", handleXML); | |
server.begin(); | |
} | |
void buildWebSite() { | |
buildJavaScript(); | |
webSite = "<!DOCTYPE HTML>\n"; | |
webSite += javaScript; | |
webSite += "<BODY onload='process()'>\n"; | |
webSite += "<BR> WebServer dinâmico com Ajax<BR>\n"; | |
webSite += "Temperatura = <A id='runtime'></A> \n"; | |
webSite += "</BODY>\n"; | |
webSite += "</HTML>\n"; | |
} | |
void buildJavaScript() { | |
javaScript += "<SCRIPT>\n"; | |
javaScript += "var xmlHttp = createXmlHttpObject();\n"; | |
// function createXmlHttpObject() | |
javaScript += "function createXmlHttpObject() {\n"; | |
javaScript += "if(window.XMLHttpRequest) {\n"; | |
javaScript += " xmlHttp = new XMLHttpRequest();\n"; | |
javaScript += "} else {\n"; | |
javaScript += " xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');\n"; | |
javaScript += "}\n"; | |
javaScript += "return xmlHttp;\n"; | |
javaScript += "}\n"; | |
// function process() | |
javaScript += "function process() {\n"; | |
javaScript += " if(xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {\n"; | |
javaScript += " xmlHttp.open('PUT','xml',true);\n"; | |
javaScript += " xmlHttp.onreadystatechange = handleServerResponse;\n"; | |
javaScript += " xmlHttp.send(null);\n"; | |
javaScript += " }\n"; | |
javaScript += " setTimeout('process()',1000);\n"; | |
javaScript += "}\n"; | |
// function handleServerResponse() | |
javaScript += "function handleServerResponse(){\n"; | |
javaScript += " if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {\n"; | |
javaScript += " xmlResponse=xmlHttp.responseXML;\n"; | |
javaScript += " xmldoc = xmlResponse.getElementsByTagName('response');\n"; | |
javaScript += " message = xmldoc[0].firstChild.nodeValue;\n"; | |
javaScript += " document.getElementById('runtime').innerHTML=message;\n"; | |
javaScript += " }\n"; | |
javaScript += "}\n"; | |
javaScript += "</SCRIPT>\n"; | |
} | |
void buildXML() { | |
XML = "<?xml version='1.0'?>"; | |
XML += "<response>"; | |
XML += getTemp(); | |
XML += "</response>"; | |
} | |
float getTemp() { | |
float readTemp = (analogRead(tempSensor) * 330.0f) / 1023.0f; | |
return readTemp; | |
} | |
void handleWebSite() { | |
buildWebSite(); | |
server.send(200, "text/html", webSite); | |
} | |
void handleXML() { | |
buildXML(); | |
server.send(200, "text/xml", XML); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment