In this part of the series we will be looking at creating a PHP web page that will show the data logged in the database in JSON format. This format is easily readable by not only computers but also humans, so it is an excellent way to share data. So lets get on with making the changes we need.

Acknowledgement

The original guide that this post is taken from can be located below.

wingoodharry.wordpress.com/2015/05/18/raspberry-pi-temperature-sensor-web-server-part-3-scheduling-temperature-readings-and-php-script/

Create a PHP web page

First we need to create a file in our web server storage. This is a special area that provides access via a webpage. This is stored in /var/www/html so type:

cd /var/www/html/

Now create PHP file that will display the data in JSON format:

sudo nano temperaturejson.php

The code to pasted into the file is

 PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
$stmt = $pdo->query('SELECT * FROM tempLog ORDER BY datetime DESC');
$tempValue = array();
$i=0;
while ($row = $stmt->fetch())
{
    $dateAndTemps = array();
    $datetime = $row['datetime'];
    $temp = $row['temperature'];
    $dateAndTemps["Date"] = $datetime;
    $dateAndTemps["Temp"] = $temp;
    $tempValue[$i]=$dateAndTemps;
    $i++;
}
echo '{"records":'.json_encode($tempValue).'}';
?>

Now to view your data open a web browser and type the following address but also changing the numbers to match your Pi's address.

[192.168.0.10/temperatu...](http://192.168.0.10/temperaturejson.php)

Note I had to restart my Pi to get this working. You can use the following from SSH

sudo shutdown -r now

If all works you should see something similar to:

JSON Data
JSON Data

Now you are ready for Part 4, where we will make our iOS app to display the Temperature data.