PHP解析单片机JSON温湿度数据时如何添加不可用错误提示
解决单片机Web服务器不可用时的错误提示问题
我明白你的痛点——现在只要有一台MC连不上,整个页面就直接空白,完全不知道哪里出问题对吧?咱们可以通过封装数据获取逻辑+错误判断来解决,既让代码更简洁,又能在MC不可用时友好显示提示。
第一步:封装数据获取函数
把重复的file_get_contents和json_decode逻辑抽成一个函数,统一处理错误:
function getMcData($url) { // 抑制file_get_contents的报错(也可以用curl更稳定,后面会提) $data = @file_get_contents($url); if (!$data) { // 获取失败,返回标记 return false; } $decoded = json_decode($data); // 检查json解析是否成功,并且是数组格式 if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) { return false; } return $decoded; }
这个函数会帮你:
- 尝试获取指定MC的JSON数据
- 如果获取失败(比如MC离线)或者JSON解析出错,返回
false - 成功的话返回解析后的传感器数据数组
第二步:用函数替换重复的代码
原来的重复代码可以简化成这样:
$mc1Data = getMcData('http://192.168.40.61/'); $mc2Data = getMcData('http://192.168.40.62/'); $mc3Data = getMcData('http://192.168.40.63/'); $mc4Data = getMcData('http://192.168.40.64/'); $mc7Data = getMcData('http://192.168.40.65/');
是不是清爽多了?而且所有错误处理都在函数里统一完成。
第三步:修改表格部分的逻辑
每个表格里,先判断对应MC的数据是否有效,如果无效就显示提示,否则正常遍历数据。比如MC1的表格可以改成这样:
<div class="m"> <table width="30%" cellspacing="0" border="1"> <tbody> <tr> <td colspan="3"> <center><b>MC №1</b></center> </td> </tr> <th>Sensor number</th> <th>Temp С ° </th> <th>Hum %</th> </tr> <?php if ($mc1Data === false) { // MC不可用,显示提示行,跨3列 echo '<tr><td colspan="3" align="center"><b>sensor is not available</b></td></tr>'; } else { // 正常遍历数据 foreach ($mc1Data as $sensor) { echo '<tr>'; echo '<td align="center">' . $sensor->Sensor . '</td>'; echo '<td align="center">' . $sensor->temperature . '</td>'; echo '<td align="center">' . $sensor->humidity . '</td>'; echo '</tr>'; } } ?> </tbody> </table> </div>
其他MC的表格都按照这个逻辑修改就行,注意把$mc1Data换成对应的变量(比如$mc2Data对应MC2)。
额外优化建议(可选)
如果file_get_contents经常因为超时或者网络问题报错,你可以改用curl来实现更稳定的请求,比如把函数改成这样:
function getMcData($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 设置5秒超时 $data = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // 检查请求是否成功(HTTP状态码200),且数据不为空 if ($httpCode !== 200 || !$data) { return false; } $decoded = json_decode($data); if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) { return false; } return $decoded; }
curl能更好地控制超时时间,还能检查HTTP状态码,比file_get_contents更可靠。
完整修改后的代码
把上面的逻辑整合起来,完整代码如下:
<?php function getMcData($url) { $data = @file_get_contents($url); if (!$data) { return false; } $decoded = json_decode($data); if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) { return false; } return $decoded; } // 获取各MC数据 $mc1Data = getMcData('http://192.168.40.61/'); $mc2Data = getMcData('http://192.168.40.62/'); $mc3Data = getMcData('http://192.168.40.63/'); $mc4Data = getMcData('http://192.168.40.64/'); $mc7Data = getMcData('http://192.168.40.65/'); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Monitoring</title> <style type="text/css"> body { background-color: white; color: black; } header { background: white url(img/header.png) repeat-x; } table { border-collapse: collapse; border: 5px double #000; border-color: black; } th { background: gray; text-align: center; color: RGB(255, 241, 213); } td, th { border: 1px solid #800; padding: 8px; border-color: black; } .m { float:left; margin-left: 15px; } </style> <body> <header> <div class="header-bg"> <center><img src="/img/header.png" alt="EEEEE"></center> </div> </header> <div class="m"> <table width="30%" cellspacing="0" border="1"> <tbody> <tr> <td colspan="3"> <center><b>MC №1</b></center> </td> </tr> <th>Sensor number</th> <th>Temp С ° </th> <th>Hum %</th> </tr> <?php if ($mc1Data === false) { echo '<tr><td colspan="3" align="center"><b>sensor is not available</b></td></tr>'; } else { foreach ($mc1Data as $sensor) { echo '<tr>'; echo '<td align="center">' . $sensor->Sensor . '</td>'; echo '<td align="center">' . $sensor->temperature . '</td>'; echo '<td align="center">' . $sensor->humidity . '</td>'; echo '</tr>'; } } ?> </tbody> </table> </div> <div class="m"> <table width="30%" cellspacing="0" border="1"> <tbody> <tr> <td colspan="3"> <center><b>MC №2</b></center> </td> </tr> <th>Sensor number</th> <th>Temp С ° </th> <th>Hum %</th> </tr> <?php if ($mc2Data === false) { echo '<tr><td colspan="3" align="center"><b>sensor is not available</b></td></tr>'; } else { foreach ($mc2Data as $sensor) { echo '<tr>'; echo '<td align="center">' . $sensor->Sensor . '</td>'; echo '<td align="center">' . $sensor->temperature . '</td>'; echo '<td align="center">' . $sensor->humidity . '</td>'; echo '</tr>'; } } ?> </tbody> </table> </div> <div class="m"> <table width="30%" cellspacing="0" border="1"> <tbody> <tr> <td colspan="3"> <center><b>MC №3</b></center> </td> </tr> <th>Sensor number</th> <th>Temp С ° </th> <th>Hum %</th> </tr> <?php if ($mc3Data === false) { echo '<tr><td colspan="3" align="center"><b>sensor is not available</b></td></tr>'; } else { foreach ($mc3Data as $sensor) { echo '<tr>'; echo '<td align="center">' . $sensor->Sensor . '</td>'; echo '<td align="center">' . $sensor->temperature . '</td>'; echo '<td align="center">' . $sensor->humidity . '</td>'; echo '</tr>'; } } ?> </tbody> </table> </div> <div class="m"> <table width="30%" cellspacing="0" border="1"> <tbody> <tr> <td colspan="3"> <center><b>MC №4</b></center> </td> </tr> <th>Sensor Number</th> <th>Temp С ° </th> <th>Hum %</th> </tr> <?php if ($mc4Data === false) { echo '<tr><td colspan="3" align="center"><b>sensor is not available</b></td></tr>'; } else { foreach ($mc4Data as $sensor) { echo '<tr>'; echo '<td align="center">' . $sensor->Sensor . '</td>'; echo '<td align="center">' . $sensor->temperature . '</td>'; echo '<td align="center">' . $sensor->humidity . '</td>'; echo '</tr>'; } } ?> </tbody> </table> </div> <div class="m"> <table width="30%" cellspacing="0" border="1"> <tbody> <tr> <td colspan="3"> <center><b>MC №7</b></center> </td> </tr> <th>Sensor number</th> <th>Temp С ° </th> <th>Hum %</th> </tr> <?php if ($mc7Data === false) { echo '<tr><td colspan="3" align="center"><b>sensor is not available</b></td></tr>'; } else { foreach ($mc7Data as $sensor) { echo '<tr>'; echo '<td align="center">' . $sensor->Sensor . '</td>'; echo '<td align="center">' . $sensor->temperature . '</td>'; echo '<td align="center">' . $sensor->humidity . '</td>'; echo '</tr>'; } } ?> </tbody> </table> </div> </body> </html>
这样修改后,哪怕某台MC离线或者出问题,对应的表格会显示sensor is not available,其他正常的MC数据依然能正常展示,不会再出现整个页面空白的情况啦!
内容的提问来源于stack exchange,提问作者user9250691
相关产品推荐
相关产品推荐

