Advertisement
noam76

ESP send A0 to my web page

Dec 10th, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -------------- Script I found ------------------------
  2. <script>
  3.    function obtenirVariables()
  4.    {
  5.       var uniqueURL = "reqEtatVariables" + "&aleatoire=" + Math.trunc(Math.random() * 1000000);
  6.       var request = new XMLHttpRequest(); // http://www.toutjavascript.com/reference/ref-xmlhttprequest.php
  7.       // la fonction à appeler lors d'un changement d'avancement de la requête AJAX
  8.       request.onreadystatechange = function()
  9.       {
  10.          if (this.readyState == 4) {
  11.             // Indicateur de l'avancement de l'appel AJAX == 4 => Données complètement accessibles
  12.             if (this.status == 200) {
  13.                // Code retour du serveur après l'appel AJAX == 200 => OK, tout s'est bien passé
  14.                if (this.responseXML != null) {
  15.                   // si on a bien obtenu une réponse non nulle
  16.                   // alors on va extraire du XML les éléments qui nous intéressent
  17.                   /*document.getElementById("boutonID").innerHTML =
  18.                   this.responseXML.getElementsByTagName('bouton')[0].childNodes[0].nodeValue;
  19.                  
  20.                   document.getElementById("digital1ID").innerHTML =
  21.                   this.responseXML.getElementsByTagName('digital1')[0].childNodes[0].nodeValue;*/
  22.                  
  23.                   document.getElementById("analog1ID").innerHTML =
  24.                   this.responseXML.getElementsByTagName('sendsensor1')[0].childNodes[0].nodeValue;
  25.                   document.getElementById("analog2ID").innerHTML =
  26.                   this.responseXML.getElementsByTagName('sendsensor2')[0].childNodes[0].nodeValue;
  27.                   document.getElementById("analog3ID").innerHTML =
  28.                   this.responseXML.getElementsByTagName('sendsensor3')[0].childNodes[0].nodeValue;
  29.                   document.getElementById("analog4ID").innerHTML =
  30.                   this.responseXML.getElementsByTagName('sendsensor4')[0].childNodes[0].nodeValue;
  31.                }
  32.             }
  33.          }
  34.       }
  35.       request.open("GET", uniqueURL , true); // ici on envoie la requête GET sur l'URL /reqEtatVariables
  36.       request.send(null);
  37.       setTimeout("obtenirVariables()", 1000); // on rappelle obtenirVariables() dans 1s
  38.    }
  39.    </script>
  40.  
  41. <p>la pin D4 vaut <span id="digital1ID">...</span></p> -->
  42.    <p>Plant Soil Humidity Sensor1 <span id="analog1ID">...</span></p>
  43.    <p>Plant Soil Humidity Sensor2 <span id="analog2ID">...</span></p>
  44.    <p>Plant Soil Humidity Sensor3 <span id="analog3ID">...</span></p>
  45.    <p>Plant Soil Humidity Sensor4 <span id="analog4ID">...</span></p>
  46.  
  47. ----------------------------------  ESP8266 CODE -----------------------------
  48. #include <ESP8266WebServer.h>
  49. #include <FS.h>
  50. const char* ssid = "****";
  51. const char* password = "****";
  52. const String DISPLAY_DATA_HTML = "/html/display_data.html";
  53.  
  54. ESP8266WebServer server(80);
  55. #define Sensor1 A0
  56.  
  57. **** function to display the page ****
  58. void DisplayData()
  59. {
  60.  String form = "";
  61.  File f = SPIFFS.open(DISPLAY_DATA_HTML, "r");
  62.  if (!f){
  63.   Serial.println("Can't open update html file");
  64.   server.send(404, "text/html", "File not found");    
  65.   }
  66.   else{
  67.    char buf[1024];
  68.    int siz = f.size();
  69.    while(siz > 0) {
  70.     size_t len = std::min((int)(sizeof(buf) - 1), siz);
  71.     f.read((uint8_t *)buf, len);
  72.     buf[len] = 0;
  73.     form += buf;
  74.     siz -= sizeof(buf) - 1;
  75.    }
  76.    f.close();
  77.    server.send(200, "text/html", form);
  78.   }
  79. }
  80.  
  81. void setup(void){
  82.  Serial.begin(115200);
  83.  
  84.  if (!SPIFFS.begin()) {
  85.   Serial.println("Failed to mount file system");
  86.   return;
  87.   }
  88.  
  89.  WiFi.begin(ssid, password);
  90.  Serial.println("");
  91.  // Wait for connection
  92.  while (WiFi.status() != WL_CONNECTED) {
  93.   delay(500);
  94.   Serial.print(".");
  95.   }
  96.  Serial.println("");
  97.  Serial.print("Connected to ");
  98.  Serial.println(ssid);
  99.  Serial.print("IP address: ");
  100.  Serial.println(WiFi.localIP());
  101.  
  102.  if (MDNS.begin("esp8266")) {
  103.   Serial.println("MDNS responder started");
  104.   }
  105.  
  106.  server.on("/display_data",DisplayData);
  107.  server.on("/reqEtatVariables", [&](){
  108.  server.send(200, "text/XML", String((int)Web_Soil_Sensor1)); // send to someones browser when asked
  109.   });
  110.  
  111. server.serveStatic("/css", SPIFFS, "/html/css");
  112. server.serveStatic("/display_data.html", SPIFFS, "/html/display_data.html");
  113.  
  114. server.begin();
  115.   Serial.println("HTTP server started");
  116.   if (!MDNS.begin(host)) {
  117.     Serial.println("Error setting up MDNS responder!");
  118.     while(1){
  119.       delay(1000);
  120.     }
  121.   }
  122.   Serial.println("mDNS responder started");
  123.   // Add service to MDNS-SD
  124.   MDNS.addService("http", "tcp", 80);
  125. }
  126.  
  127. void loop(void){
  128.  server.handleClient();
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement