0

I have a Raspberry PI with a button attached to it. I run a Python script on it to detect when the button is pressed. When the button is pressed, it increment a number in a text file. I need to display this number on a web page (Apache) and to launch a sound.

For the moment, my python script change the index.html with the value I need and I am using <meta http-equiv="refresh" content="1"> to resfresh the page.

The problem is that I need to know when the number is changing to launch the sound.

Weedoze
  • 13,683
  • 1
  • 33
  • 63

1 Answers1

0

Too little information to say something concrete, but here's how I'd get around to it given the main following limitation that the page is not dynamic (no ajax).

General outline:

Put the value from the text file in a identifiable field like

<span id="myNumber">#the number from file goes here </span>

then on load with java script read the value of the field: (planin JS) var myNumberValue = document.getElementById('myNumber').innerHTML; then create a cookie to store the last value on clinets machine: document.cookie = "lastNumber="+myNumberValue;

To sum it all up: on loading the webpage launch a script that will :

  1. check for a cookie
  2. read a value from the coookie (http://www.w3schools.com/js/js_cookies.asp)
  3. read a value from the field
  4. if a cookie exists compare the values and if the number changed play a sound like here: Playing audio with Javascript?
  5. either way store the value from the field to the cookie for next website update.

[Edit] Full working solution using local storage or cookies:

<html>
<head>
<META HTTP-EQUIV="refresh" CONTENT="5">
<script type="text/JavaScript">
function myFunction()
{
var numberDisplayed  = parseInt(document.getElementById('myNumber').innerHTML);
var numberInCookie = numberDisplayed;
//lets assume that's the only data in the cookie
var tmpData = readNumber();
if(tmpData!="NaN" && tmpData!= "undefined") 
    {
        numberInCookie = tmpData;
    }
if(numberDisplayed!=numberInCookie)
{
    alert("changed to from "+ numberInCookie+" to " + numberDisplayed);
}
saveNumber(numberDisplayed);
}
function readNumber()
{
    if (typeof(Storage) !== "undefined") {
        return localStorage.getItem("lastNumber")
    } else {
        var cookieData = document.cookie;
        //lets assume that's the only data in the cookie
        var tmpData = parseInt(cookieData.split("=")[1]);
        return tmpData
    }
}
function saveNumber(number)
{
    if (typeof(Storage) !== "undefined") {
        localStorage.setItem("lastNumber", number);
    } else {
        document.cookie = "lastNumber="+number;
    }
}
</script>
</head>
<body  onload="myFunction()">
<span id="myNumber">2</span>
</body>
</html>

[Edit 2] as the author of the question hinted he doesn't actually want to use the site refresh here's another option: lest start a loop that will load a test file from the server as an ajax request. then the data is loaded, parsed, store it loacally as before. and set timer to trigger the refresh again.

One importatnt thing the files need to be available on the same domain / server (see HTTP access control (CORS) for more information)

<html>
<head>
<script type="text/JavaScript">
var externalNumber = 0;
var timer;
function myFunction()
{
// start the readouts:
LoadExternalData();
}

function LoadExternalData()
{
var client = new XMLHttpRequest();
client.open("GET", "http:/myserver/readme.txt");
client.onreadystatechange = function() {    
  externalNumber = parseInt(responseText);
  //store a local copy
  var NewestNumber = externalNumber;
  var tmpData = readNumber();
  if(tmpData!="NaN" && tmpData!= "undefined") 
        {
            numberInCookie = tmpData;
        }
    if(NewestNumber!=numberInCookie)
    {
        alert("changed to from current  "+ numberInCookie+" to " + NewestNumber);
    }
   saveNumber(NewestNumber);
   timer = setTimeout(LoadExternalData, 1000);
}
client.send();
}


function readNumber()
{
    if (typeof(Storage) !== "undefined") {
        return localStorage.getItem("lastNumber")
    } else {
        var cookieData = document.cookie;
        //lets assume that is the only data in the cookie
        var tmpData = parseInt(cookieData.split("=")[1]);
        return tmpData
    }
}
function saveNumber(number)
{
    if (typeof(Storage) !== "undefined") {
        localStorage.setItem("lastNumber", number);
    } else {
        document.cookie = "lastNumber="+number;
    }
}
</script>
</head>
<body  onload="myFunction()">
<span id="myNumber">2</span>
</body>
</html>
Community
  • 1
  • 1
azrael.pl
  • 85
  • 1
  • 8