Skip to content

Commit

Permalink
Timeout display functions for the range display when no signal or net…
Browse files Browse the repository at this point in the history
…work.
  • Loading branch information
easytarget committed Nov 5, 2019
1 parent c1fbf4a commit 31ee3b6
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const char MAIN_page[] PROGMEM = R"=====(
<h3 style="text-align: center;">ESP32/VL53L0X Demo</h3>
<hr>
<div id="signal" class="signal">Comms Timeout</div>
<div><span id="RANGEValue" style="float: right; font-size: 200%;font-weight: bold;">?</span>
<div><span id="RANGEValue" style="float: right; font-size: 200%;font-weight: bold;">connecting..</span>
<span style="float: left; font-size: 160%;font-weight: bold;">Range:</span></div><hr>
<div><span id="MODEValue"style="float: right; font-weight: bold;">unknown</span>&nbsp;
<span style="float: left;font-weight: bold;">Mode:</span></div>
Expand Down Expand Up @@ -207,19 +207,23 @@ const char MAIN_page[] PROGMEM = R"=====(
// Loop to get, process and display value readings
var plotline = 0;
var plotscale = 20;
var timeout = 0;

function getValues() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = JSON.parse(this.responseText);
document.getElementById("signal").innerHTML = "&nbsp;";
document.getElementById("signal").innerHTML = "&nbsp;"; //some content is needed to prevent element collapsing
if (response.RangeStatus < 0) {
timeout = 0; // dont timeout while disabled
document.getElementById("RANGEValue").innerHTML = "<span style=\"font-size: 80%;\">Disabled</span>";
document.getElementById("RANGEValue").style.color = "#555";
} else {
timeout++; // always increment the timeout, it will only reset when we have a valid range
switch (response.RangeStatus) {
case 0:
timeout = 0; // valid range result, reset timeout counter
document.getElementById("RANGEValue").innerHTML = response.Distance;
document.getElementById("RANGEValue").innerHTML += "<span style=\"font-size: 66%;\">mm</span>";
document.getElementById("RANGEValue").style.color = "#555";
Expand Down Expand Up @@ -260,6 +264,8 @@ const char MAIN_page[] PROGMEM = R"=====(
document.getElementById("signal").innerHTML = "Unknown Error";
document.getElementById("RANGEValue").style.color = "#8e0b0b;";
}
// if timeout reached, clear the range value.
if (timeout > 10) document.getElementById("RANGEValue").innerHTML = "n/a";
}
// If an angle field is present, display it.
if (response.hasOwnProperty('Angle')) {
Expand Down Expand Up @@ -301,6 +307,9 @@ const char MAIN_page[] PROGMEM = R"=====(
xhttp.timeout = 300; // time in milliseconds
xhttp.ontimeout = function () {
document.getElementById("signal").innerHTML = "Network Timeout";
timeout++; // increment timeout counter
// if timeout reached, clear the range value
if (timeout > 10) document.getElementById("RANGEValue").innerHTML = "n/a";
};
}

Expand Down

0 comments on commit 31ee3b6

Please sign in to comment.