修改了楼主的代码,晶晨正常显示。
home.php修改如下
<div id="footer">
<div class="footer-contents">
<div class="links">
<div class="line">
<!-- 新增温度显示元素 -->
<span class="temperature" id="temperatureDisplay">正在获取温度...</span>
<!-- 保持原有的链接和版权信息 -->
<span class="footer-link-separator"></span>
<a href="https://bbs.histb.com" target="_blank">海纳思交流论坛</a>
<span class="copyright">Copyright © <?php echo date('Y', time()); ?></span>
</div>
</div>
</div>
<script>
// 页面加载完成后立即调用一次,其后每三秒更新
setInterval(updateTemperature, 3000);
updateTemperature();
function updateTemperature() {
fetch('updateTemperature.php') // 替换为您的PHP脚本路径
.then(response => response.json())
.then(data => {
// 修改温度显示格式
document.getElementById('temperatureDisplay').textContent =
'设备温度:' + data.temperature + '°C';
})
.catch(error => {
console.error('Error fetching temperature:', error);
// 修改错误提示文本
document.getElementById('temperatureDisplay').textContent = '温度:获取失败';
});
}
</script>
</div>
updateTemperature.php修改如下
<?php
// 定义温度文件路径 - 替换为您在步骤1中找到的实际路径
$tempFile = '/sys/class/thermal/thermal_zone0/temp';
// 检查文件是否存在和可读
if (!file_exists($tempFile)) {
die(json_encode(['temperature' => 'N/A'])); // 修改为JSON格式错误响应
}
if (!is_readable($tempFile)) {
die(json_encode(['temperature' => 'N/A'])); // 修改为JSON格式错误响应
}
// 读取温度数据(单位为毫摄氏度)
$tempMilliC = file_get_contents($tempFile);
if ($tempMilliC === false) {
die(json_encode(['temperature' => 'N/A'])); // 修改为JSON格式错误响应
}
// 转换为摄氏度并输出
$tempC = intval($tempMilliC) / 1000;
echo json_encode(['temperature' => number_format($tempC, 1)]); // 修改为JSON格式输出
// 可选:添加日志记录或API输出
// file_put_contents('temp_log.txt', date('Y-m-d H:i:s') . " - $tempC °C\n", FILE_APPEND);
?>