Reputation: 2220
I'm in the process of creating a lapcounter for a slot car racetrack.
Whenever a car passes a sensor, it sends some data through a microcontroller to the computer which writes the data to a file. Now I would like to detect whenever that file has changed, meaning that a car has passed the sensor, and store that data onto a database (mysql), then present it on the screen in real time. The filecheck would have to be done continuously, and as rapidly as possible..
Also a timer that counts in hundreds of a seconds would be nice to present.. But that's another thing, and has nothing to do with this question...
There may be better ways of doing this, but I'm pretty sure it could be possible with some ajax, php and mysql on a local server that is directly connected to the microcontroller.
Correct me if I'm wrong, but Facebook, Stackoverflow, and instant messages/chats uses some similar future I would assume..
Upvotes: 0
Views: 84
Reputation: 4621
The Logic for file Change can be on php end 'check_the_file_change.php'
The below is the smaple JS code, which will send the request every 1 second to a php file
'check_the_file_change.php'
$(document).ready(function(){
function checkFileChange(){
setTimeout(function () {
$.ajax({
url :'check_the_file_change.php',
type : 'POST',
data :{
user_secret : 'XXXX'
},
success : function(respsone){
if(response == "true"){
//file changed
}else{
//file not chnaged
}
},
complete : function(){
checkFileChange(); //net file check after 1 sec from the completion of the previous request
}
});
},1000);
}
}
});
ON PHP end
header("Access-Control-Allow-Origin: *"); // use this in case the ajax call is coming from other domain
$filename = 'somefile.txt';
if (file_exists($filename)) {
if( time() - filemtime($filename) > 60){
echo "false";
}else{ //file is modified 1 minute ago
echo "true";
}
}
Upvotes: 1