Reputation:
I have a problem. There are some microcontrollers which are working as webservers and output information about temperature and humidity (sensors are connected to this MC) as JSON data. I've written a php parsing script which just connects to this webpages (each MC has 4 sensors and there are 8 MC altogether, so there are 8 pages with 4 sensors on each one) and display all 32 sensors from 8 MC as 1 table. But the problem is, I don't understand how to write "if error - show "sensor is not avaliable".
This is a example of web page of 1 MC.
[{ "Sensor":"T1.1","temperature":22.90,"humidity":14.30},{ "Sensor":"T1.2","temperature":23.60,"humidity":14.70},{ "Sensor":"T1.3","temperature":22.40,"humidity":16.90},{ "Sensor":"T1.4","temperature":23.50,"humidity":17.10}]
And this is a PHP script of output.
$url = 'http://192.168.40.61/';
$data = file_get_contents($url);
$characters = json_decode($data);
$url2 = 'http://192.168.40.62/';
$data2 = file_get_contents($url2);
$characters2 = json_decode($data2);
$url3 = 'http://192.168.40.63/';
$data3 = file_get_contents($url3);
$characters3 = json_decode($data3);
$url4 = 'http://192.168.40.64/';
$data4 = file_get_contents($url4);
$characters4 = json_decode($data4);
$url7 = 'http://192.168.40.65/';
$data7 = file_get_contents($url7);
$characters7 = json_decode($data7);
?>
<!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
foreach ($characters as $character) {
echo '<tr>';
echo '<td align="center">' . $character->Sensor . '</td></center>';
echo '<td align="center">' . $character->temperature . '</td>';
echo '<td align="center">' . $character->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 numver</th>
<th>Temp С ° </th>
<th>Hum %</th>
</tr>
<?php
foreach ($characters2 as $character2) {
echo '<tr>';
echo '<td align="center">' . $character2->Sensor . '</td></center>';
echo '<td align="center">' . $character2->temperature . '</td>';
echo '<td align="center">' . $character2->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
foreach ($characters3 as $character3) {
echo '<tr>';
echo '<td align="center">' . $character3->Sensor . '</td></center>';
echo '<td align="center">' . $character3->temperature . '</td>';
echo '<td align="center">' . $character3->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
foreach ($characters4 as $character4) {
echo '<tr>';
echo '<td align="center">' . $character4->Sensor . '</td></center>';
echo '<td align="center">' . $character4->temperature . '</td>';
echo '<td align="center">' . $character4->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
foreach ($characters7 as $character7) {
echo '<tr>';
echo '<td align="center">' . $character7->Sensor . '</td></center>';
echo '<td align="center">' . $character7->temperature . '</td>';
echo '<td align="center">' . $character7->humidity . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
</body>
</html>
And when one of these MC is not actually avaliable, there is just an empty page, with no error indicating. How can I write it for each table ? thank you in advance.
Upvotes: 0
Views: 54
Reputation: 1361
Hope this would be helpful:
Better to put $characters into an array for easier output later, such as
$urls = array("http://192.168.40.61/", "http://192.168.40.62/", "http://192.168.40.62/", "http://192.168.40.63/", "http://192.168.40.64/", "http://192.168.40.65/" );
foreach($urls as $key => $url) {
$data = file_get_contents($url);
$characters[$key] = json_decode($data);
}
Check if the data is available before output it: available: output the table; not available: give message.
foreach($characters as $key => $character_this) {
if(count($character_this) < 1) {
echo 'Data for No. ' .$key. ' is not available';
} else {
$rows = show_table($character_this);
};}
function show_table($character_this){
foreach ($character_this as $character) {
$rows .= '<tr>';
$rows .= '<td align="center">' . $character->Sensor . '</td></center>';
$rows .= '<td align="center">' . $character->temperature . '</td>';
$rows .= '<td align="center">' . $character->humidity . '</td>';
$rows .= '</tr>';
}
return $rows;}
I do not know why I cannot put the last }
on a separate line.
Upvotes: 1
Reputation: 2632
Try suppressing errors (with @) on file_get_contents()
each time you use it. If the result === false
, you can go and check what kind of error it is:
$mc = '[THE-IP-ADDR]';
$data = @file_get_contents($mc);
if ($data === false) {
$headers = get_headers($mc);
$responseCode = substr($headers[0], 9, 3);
// Handle errors based on response code
if ($responseCode == '404') {
//do something, page is missing
}
// Etc.
} else {
// Do default json rendering here.
}
You can even write a function to get the error since you'll use this multiple times:
function get_mc_error($mc) {
$headers = get_headers($mc);
$responseCode = substr($headers[0], 9, 3);
// Handle errors based on response code
if ($responseCode == '404') {
return 'You\'re page is missing.
// or return json_encode(array('error'=>true,'errorMsg'=>'Missing page'));
}
// Etc.
}
And then do:
if($data === false) {
$errorMsg[] = get_mc_error($mc);
// $data = get_mc_error($mc); JSON solution
}
Upvotes: 0