How i can get real time value from webserver?

I made temperature sensor webserver with esp8266.I want to display temperature value from webserver that changes in real time in label and value to put later make a chart. This is my webserver code:

const char html_page[] PROGMEM = R"rawSrting(
<!DOCTYPE html>
<html>
  <style>
    body {font-family: sans-serif;}
    h1 {text-align: center; font-size: 30px;}
    p {text-align: center; color: #4CAF50; font-size: 40px;}
  </style>

<body>
  <h1>DS18B20 Temperature Monitoring With Nodemcu</h1><br>
  <p id="TempValue"><span id="TempValue">0</span></p><br>

  <script>
    setInterval(function() {
      var xhttp = new XMLHttpRequest();
     xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("TempValue").innerHTML = this.responseText;
        }
      };
     xhttp.open("GET", "readTemp", true);
      xhttp.send();
    },50);
  </script>
</body>
</html>
)rawSrting";

I tried this:


window.AppInventor.setWebViewString(document.getElementsById("TempValue")[0].innerText)

But it don't change the label text at all. I also unsuccsefully tried with reload url on timer.

How i can do this?

Why not set the webviewstring in your html ?

...
document.getElementById("TempValue").innerHTML = this.responseText;
if(AppInventor) {
window.AppInventor.setWebViewString(this.responseText);
}
...

then in your blocks just use the WebViewStringChanged block, and add values to a list for processing. No need for the PageLoaded event.

Are you sure you want to capture the data 20 times a second ?

Your solution works. Now when app is opened, it wont get a reading when temperature is stable. I want to display a first value when page loads. Do i need to use Page Loaded block with RunJavaScipt like before? But this:

window.AppInventor.setWebViewString(document.getElementsById("TempValue")[0].innerText)

didn't worked before.

Set the webviewstring to a value outside of the expected range of values to be returned on startup. Then when the first actual value comes in it will be registered.

Do much the same (if) whenever you require a reset.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.