Hay muchos ejemplos en internet. Búscalo en goolge con algo como ESP8266 LED webserver o ESP8266 Blynk . Allí, en su mayoría, muestran cómo construir esto y el acceso a su NodeMcu a través de, por ejemplo. Google Chrome. Si quieres hacerlo con tu propia aplicación de Android es un poco más complicado. Búsquelo
aquí o aquí .
Para enviar datos desde el servidor al cliente, debe considerar usar javascript. Aquí hay un código de ejemplo de mi control Relais ESP8266. Tomé el código de aquí y lo modifiqué según mis preferencias.
principal :
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
boolean LEDstate = false;
#include "functions.h"
int LED = 2;
int OUT = 5;
IPAddress ip(10, 0, 0, 99);
IPAddress gateway(10, 0, 0, 1);
IPAddress subnet(255, 255, 255, 0);
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(115200); delay(100);
Serial.print("... Attempting to log into router... \r\n");
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) delay(500);
WiFi.mode(WIFI_STA);
Serial.println("\n\nBOOTING ESP8266 ...");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.println("Station IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleWebsite);
server.on("/setESPval", handleESPval);
server.on("/xml", handleXML);
server.begin();
}
void loop() {
server.handleClient();
buildXML();
delay(100);
if (LEDstate) {
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
}
functions.h
ESP8266WebServer server(80);
String webSite;
String javaScript;
String XML;
void buildJavascript() {
javaScript = "<SCRIPT>\n";
javaScript += "xmlHttp=createXmlHttpObject();\n";
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";
javaScript += "function process(){\n";
javaScript += " if(xmlHttp.readyState==0||xmlHttp.readyState==4){\n";
javaScript += " xmlHttp.onreadystatechange=function(){\n";
javaScript += " if(xmlHttp.readyState==4&&xmlHttp.status==200){\n";
javaScript += " xmlDoc=xmlHttp.responseXML;\n";
javaScript += " console.log(xmlDoc.getElementsByTagName('mt')[0]);\n";
javaScript += " xmlmsg=xmlDoc.getElementsByTagName('mt')[0].firstChild.nodeValue;\n";
javaScript += " document.getElementById('runtime').innerHTML=xmlmsg;\n";
javaScript += " xmlmsg=xmlDoc.getElementsByTagName('state')[0].firstChild.nodeValue;\n";
javaScript += " document.getElementById('ledstate').innerHTML=xmlmsg;\n";
javaScript += " }\n";
javaScript += " }\n";
javaScript += " xmlHttp.open('PUT','xml',true);\n";
javaScript += " xmlHttp.send(null);\n";
javaScript += " }\n";
javaScript += " setTimeout('process()',1000);\n";
javaScript += "}\n";
javaScript += " function sendRequest(nr) {\n";
//javaScript += "alert(nr);";
javaScript += "if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {\n";
javaScript += " xmlDoc=xmlHttp.responseXML;\n";
javaScript += " xmlmsg=xmlDoc.getElementsByTagName('mt')[0].firstChild.nodeValue;\n";
javaScript += " document.getElementById('runtime').innerHTML=xmlmsg;\n";
javaScript += " xmlHttp.open('PUT', 'setESPval?fnc=' + nr, true);\n";
javaScript += " xmlHttp.send(null);\n";
javaScript += "}\n}";
javaScript += "</SCRIPT>\n";
Serial.println("JavaScript built");
}
void buildWebsite() {
buildJavascript();
webSite = "<!DOCTYPE HTML>\n";
webSite += "<META name='viewport' content='width=device-width, initial-scale=1'>\n";
webSite += javaScript;
webSite += "<BODY onload='process()'>\n";
webSite += "Runtime = <A ID='runtime'></A>\n";
webSite += "LEDstate = <A ID='ledstate'></A>\n";
webSite += "<br><button onclick='sendRequest(0)'>Funktion 1</button>\n";
webSite += "<button onclick='sendRequest(1)'>Funktion 2</button>\n";
webSite += "</HTML>\n";
// webSite += "<!DOCTYPE html><html><head> <title>JavaScript View</title> <script type="text/javascript"> function showToast(){ var message = document.getElementById("message").value; var lengthLong = document.getElementById("length").checked; app.makeToast(message, lengthLong); return false; } /* Call the 'showToast' method when the form gets submitted (by pressing button or return key on keyboard). */ window.onload = function(){ var form = document.getElementById("form"); form.onsubmit = showToast; } </script></head><body><form id="form"> Message: <input id="message" name="message" type="text"/><br /> Long: <input id="length" name="length" type="checkbox" /><br /> <input type="submit" value="Make Toast" /></form></body></html>"
Serial.println("HTML built");
}
String millis2time() {
String Time = "";
unsigned long ss;
byte mm, hh;
ss = millis() / 1000;
hh = ss / 3600;
mm = (ss - hh * 3600) / 60;
ss = (ss - hh * 3600) - mm * 60;
if (hh < 10)Time += "0";
Time += (String)hh + ":";
if (mm < 10)Time += "0";
Time += (String)mm + ":";
if (ss < 10)Time += "0";
Time += (String)ss;
return Time;
}
int i = 0;
void buildXML() {
XML = "<?xml version='1.0'?>";
XML += "<xml>";
XML += "<mt>";
XML += millis2time();
XML += "</mt>";
XML += "<state>";
XML += LEDstate;
XML += "</state>";
XML += "</xml>";
//Serial.println(XML);
}
void handleWebsite() {
buildWebsite();
server.send(200, "text/html", webSite);
}
void handleXML() {
buildXML();
server.send(200, "text/xml", XML);
Serial.println("Website handeled");
}
void handleESPval() {
//Serial.println("HANDLE");
if (server.arg("fnc").toInt() == 0) {
Serial.println("BTN1 pressed");
delay(100);
LEDstate = false;
}
else if (server.arg("fnc").toInt() == 1) {
Serial.println("BTN2 pressed");
delay(100);
LEDstate = true;
}
Serial.println(LEDstate);
server.send(200, "text/xml", XML);
}