I developed an intranet website that provides an interface to display real-time (300ms-1000ms updates) data for devices. My original setup is built on Raspberry PI 3Bs and has been running well for years. I used Python to capture I/O data and write an XML object to a file
EDIT: I added a bit of code that demonstrates it in a loop providing 'real-time' updates. This bit updates at 500ms. There is some additional data and a much less frequent SQL write that isn't shown.
def writeXML( params, xmlFile ):
try:
file=open(xmlFile,"w")
outString = "\x1B[2J" #Alternative to OS.SYSTEM('CLEAR')
#outString = ""
file.write("<data>\n")
for p in params:
file.write("\t<"+p[0]+">"+str(p[1])+"</"+p[0]+">\n")
outString += p[0]+": "+str(p[1])+"\n"
file.write("</data>\n")
file.close()
print(outString)
except Exception as e:
log.err("realtime.writeXML error - ",e)
def getStates():
try:
global BMEs
for BME in BMEs:
data = bme280.sample(BME.bus, BME.addr, BME.cal_params)
BME.temp = round((data.temperature * 9/5)+32,2)
BME.baro = round((data.pressure/1013),2)
BME.humid = round(data.humidity,2)
xmlParams = ([bPre+"Temp",BME.temp],[bPre+"Baro",BME.baro],[bPre+"humid",BME.humid])
txt.writeXML(xmlParams, bmeValues)
except Exception as e:
log.err("bme.getStates error - ", e)
while True:
getStates()
time.sleep(0.5)
There are a few of these PIs collecting data connected on a network. My original web server is running on a PI and uses the following PHP code to read/convert the XML to a JSON object:
<?php
require_once('mapShared.php');
ini_set('display_errors', 'On');
error_reporting(E_ALL|E_STRICT);
$mntPath = $_POST['mntPath'];
$dirName = $_POST['dirName'];
$remotePath = $_POST['remotePath'];
$fileName = $_POST['fileName'];
$user = $_POST['user'];
$pw = $_POST['pw'];
$res = array();
$noError = true;
//Make the local dirName if it doesn't exist
if ($noError) { $noError = makeDir($mntPath, $dirName); }
//Mount the remotePath to dirName
if ($noError) { $noError = mountDrive($mntPath, $dirName, $remotePath, $user, $pw); }
if ($noError) {
$fName = $mntPath.'/'.$dirName.'/'.$fileName;
try {
if ( filesize( $fName ) > 0 ) {
$res=simplexml_load_file($fName, 'SimpleXMLElement', LIBXML_NOWARNING);
echo json_encode($res);
}
} catch (Exception $e) {
echo 'error';
}
} else {
echo 'error';
}
?>
I then use JSON.parse to convert the result into an object in JavaScript and assigned that to my real-time object (updating the data available for the page).
All of this works as expected and has for years, but I want to move the webserver to a Windows PC. I have an obsolete Win 7 machine that I dropped the javascript/PHP code into and I'm running Apache using Xampp. I modified the PHP to work with Windows mapped drives:
<?php
require_once('mapDrive.php');
ini_set('display_errors', 'On');
error_reporting(E_ALL|E_STRICT);
$remotePath = $_POST['remotePath'];
$fileName = $_POST['fileName'];
$user = $_POST['user'];
$pw = $_POST['pw'];
$res = array();
$noError = true;
$fName = $remotePath.'\\'.$fileName;
if (!file_exists($fName)) { $noError = mapDrive($remotePath, $user, $pw); }
if ($noError) {
try {
if ( filesize( $fName ) > 0 ) {
$res=simplexml_load_file($fName, 'SimpleXMLElement', LIBXML_NOWARNING);
echo json_encode($res);
}
} catch (Exception $e) {
echo 'error';
}
}
?>
The problem I'm observing is that the real-time data doesn't update as expected on the Windows web site. Instead of ~3Hz, I'm hardly seeing any updates in several minutes. I still have the PI server running and I can see the values updating.
I've tried:
- Looking at the XML files manually in Windows explorer and in Raspbian. The data in the file and the modified date is changing periodically (several minutes) instead of ~3Hz. The Windows website follows this, but the PI website updates every ~3Hz with accurate data.
- Creating a test file in VB to tick a counter and write that to an XML file every 300ms. That updates as expected on the Windows website.
- looking at the mount folder in Raspbian. None of the XML files show up with ls. Same is true if I look with Windows Explorer, the mounted folder where the XML files should be listed. I have database files in some of these same folders, these appear normal.
- Modifying one of the python scripts to also write to a separate file in a separate folder that isn't included in a Raspbian mount. The file appears, but again doesn't update.
- Several dozen other things that weren't helpful and probably not relevant.
If the PI website wasn't updating, all the evidence would lead me to think the Python script wasn't writing the file at the intended frequency. I suspect this has something to do with the way Python is writing the file, but I've run into a block trying to figure it out.
EDIT: I can't add full content of my code (logistically and it contains potentially sensitive data) here. I added what I believe is relevant to the problem. I'll add the JS bits that get something readable in console debug:
function getRemote_XML(remotePath, file, user, pw, fx) {
$.ajax({
url: '../php/getRemoteXML.php',
type: 'post',
datatype: 'json',
data: {'remotePath': remotePath,
'fileName': file,
'user': user,
'pw': pw},
success: fx,
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
}
function rXMLtoStatus(source) { //Reads a remote XML file and converts it to an object which is written to the console [normally appended to statusData]
let sr = source.remote;
getRemote_XML(sr.path, source.file, sr.user, sr.password, function(data) {
try{console.log(JSON.parse(data));}catch(e){}
setTimeout(function(){window[source.fn](source);}, source.interval);
});
}
I normally get what I need from reading questions and google search. This is my first time actually asking something. If there is a way I can improve this question, please let me know.