Last Update: | 11/22/2017 12:34:56 AM |
---|---|
Thermometer 1 | Thermometer 2 |
78.2 | 62.6 |
Scale 1 | Scale 2 |
23.6 g | 18.1 g |
This script will create a table similar to the following, but will dynamically update the contents based on the latest readings from the various connected sensors.
This script consists of 3 different parts.
The first part is a HyperACCESS script to capture the incoming data and parse it out to a text file
$LANG = "VBScript"
filePath = "C:\path\to\file.txt"
while haGetConnectionStatus = 1 'loop as long as HyperACCESS is connected
datastr = haGetInput (0, -1, 10000, 10000) 'Capture incoming string to a variable
if datastr <> "" then
output = replace(replace(datastr,chr(10),""),chr(13),"") 'remove CR/LF from captured string
if output <> "" then
'code to parse out the desired values goes here
output = now & "," & output 'Append time stamp to front of values (optional)
'write to file
file = haFileOpen(filePath, "w")
haFileWrite file, output
haFileClose file
end if
end if
wend
haTerminate
The second part is an HTML5 web page that contains whatever other information you want, as well as a div element to contain the automatically updating data
<!DOCTYPE html>
<html>
<body>
<p>Other parts of page</p>
<div id="result"></div>
<script>
if(typeof(EventSource) !== "undefined")
{
var source = new EventSource("lastData.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
}
else
{
document.getElementById("result").innerHTML = "Browser does not support server-sent events";
}
</script>
</body>
</html>
The third part is a php script to format the captured data from the file into a table for the web page, and to send out the event stream data.
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$data = file_get_contents("file.txt");
list($time, $temp1, $temp2, $weight1, $weight2) = split(",", $data, 5);
//format the output table
$strTable = "<table><tr><th>Last Update:</th><td>$time</td></tr><tr><th>Thermometer 1</th><th>Thermometer 2</th></tr><tr><td>$temp1</td><td>$temp2</td></tr><tr><th>Scale 1</th><th>Scale 2</th></tr><tr><td>$weight1 g</td><td>$weight2 g</td></tr></table>";
echo "data: $strTable\n\n";
flush();
?>
Return to sample script index