Follow @superdavey on

Raspberry Pi Temp Sensor Part 3

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.


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

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

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();
while ($row = $stmt->fetch())
    $dateAndTemps = array();
    $datetime = $row['datetime'];
    $temp = $row['temperature'];
    $dateAndTemps["Date"] = $datetime;
    $dateAndTemps["Temp"] = $temp;
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.


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:


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