Hi everyone,
I am not a programmer just know some arduino and html code
I am using ESP8266 Nodemcu, I upload in the SPIFFS my html files, png and css.
after multiples search on google I coming to your help.
I use the <ESP8266WebServer.h> librarie and search the correct way to display the analog data reading from A0 on my web page.
I found some Xlm code but it's note working.
I asking for your help to use the correct code.
I post just the appropriate code for the moment.
The JS I found
<script>
function obtenirVariables()
{
var uniqueURL = "reqEtatVariables" + "&aleatoire=" + Math.trunc(Math.random() * 1000000);
var request = new XMLHttpRequest(); // http://www.toutjavascript.com/reference/ref-xmlhttprequest.php
// la fonction à appeler lors d'un changement d'avancement de la requête AJAX
request.onreadystatechange = function()
{
if (this.readyState == 4) {
// Indicateur de l'avancement de l'appel AJAX == 4 => Données complètement accessibles
if (this.status == 200) {
// Code retour du serveur après l'appel AJAX == 200 => OK, tout s'est bien passé
if (this.responseXML != null) {
// si on a bien obtenu une réponse non nulle
// alors on va extraire du XML les éléments qui nous intéressent
/*document.getElementById("boutonID").innerHTML =
this.responseXML.getElementsByTagName('bouton')[0].childNodes[0].nodeValue;
document.getElementById("digital1ID").innerHTML =
this.responseXML.getElementsByTagName('digital1')[0].childNodes[0].nodeValue;*/
document.getElementById("analog1ID").innerHTML =
this.responseXML.getElementsByTagName('sendsensor1')[0].childNodes[0].nodeValue;
document.getElementById("analog2ID").innerHTML =
this.responseXML.getElementsByTagName('sendsensor2')[0].childNodes[0].nodeValue;
document.getElementById("analog3ID").innerHTML =
this.responseXML.getElementsByTagName('sendsensor3')[0].childNodes[0].nodeValue;
document.getElementById("analog4ID").innerHTML =
this.responseXML.getElementsByTagName('sendsensor4')[0].childNodes[0].nodeValue;
}
}
}
}
request.open("GET", uniqueURL , true); // ici on envoie la requête GET sur l'URL /reqEtatVariables
request.send(null);
setTimeout("obtenirVariables()", 1000); // on rappelle obtenirVariables() dans 1s
}
</script>
the HTML code I try
<p>la pin D4 vaut <span id="digital1ID">...</span></p> -->
<p>Plant Soil Humidity Sensor1 <span id="analog1ID">...</span></p>
<p>Plant Soil Humidity Sensor2 <span id="analog2ID">...</span></p>
<p>Plant Soil Humidity Sensor3 <span id="analog3ID">...</span></p>
<p>Plant Soil Humidity Sensor4 <span id="analog4ID">...</span></p>
The esp code I have,
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>
const char* ssid = "****";
const char* password = "****";
const String DISPLAY_DATA_HTML = "/html/display_data.html";
ESP8266WebServer server(80);
#define Sensor1 A0
the function to display my html page
void DisplayData()
{
String form = "";
File f = SPIFFS.open(DISPLAY_DATA_HTML, "r");
if (!f){
Serial.println("Can't open update html file");
server.send(404, "text/html", "File not found");
}
else{
char buf[1024];
int siz = f.size();
while(siz > 0) {
size_t len = std::min((int)(sizeof(buf) - 1), siz);
f.read((uint8_t *)buf, len);
buf[len] = 0;
form += buf;
siz -= sizeof(buf) - 1;
}
f.close();
server.send(200, "text/html", form);
}
}
my setup code
void setup(void){
Serial.begin(115200);
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/display_data",DisplayData);
server.on("/reqEtatVariables", [&](){
server.send(200, "text/XML", String((int)Web_Soil_Sensor1)); // send to someones browser when asked
});
server.serveStatic("/css", SPIFFS, "/html/css");
server.serveStatic("/display_data.html", SPIFFS, "/html/display_data.html");
server.begin();
Serial.println("HTTP server started");
if (!MDNS.begin(host)) {
Serial.println("Error setting up MDNS responder!");
while(1){
delay(1000);
}
}
Serial.println("mDNS responder started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
finaly the loop code
void loop(void){
server.handleClient();
}
How can help me to adjust my code ?