This website uses 'cookies' to give you the best, most relevant experience. Using this website means you're happy with this. read more Accept & Close
Start Games
Do It Yourself
DIY & Home Improvement

Raspberry PI with DS18b20 web server PHP - Remote temperature measurement

Raspberry PI with DS18b20 web server PHP - Remote temperature measurement
Raspberry PI with DS18b20 web server PHP - Remote temperature measurement
How to make remote temperature measurement using Raspberry PI, DS18B20, PHP and PostgreSQL. This projection will show you how to easily make a temperature logger using the above technology. The entire temperature logger will be made using the PostgreSQL database. I will also show the example of a working online temperature logger on my website.

Step 1: Components

Step 2: Requirements

In this project we will need installed: Install the apache2 package by typing the following command in to the Terminal:
sudo apt-get update sudo apt-get install apache2
Then we install PHP.
sudo apt-get install php5 libapache2-mod-php5
Install the PostgreSQL.
sudo apt-get install php5-pgsql

Step 3: Connection of the temperature sensor

The DS18B20 sensors can be connected in parallel. The resistor 4.7k is used as a pullup for the data-line, and is required to keep the data transfer stable. All sensors should share the same pins, and you only need one 4.7K resistor for all of them.
Diagram of connection of temperature sensor DS18B20.
Diagram of connection of temperature sensor DS18B20.
Diagram of connection of temperature sensor DS18B20.
Diagram of connection of temperature sensor DS18B20.
NOTE: In newer versions of the system, add the following entry to /boot/config.txt and restart the Raspberry PI.
dtoverlay=w1-gpio,gpiopin=4
With this command we can check the attached devices for one wire.
cat /sys/bus/w1/devices/w1_bus_master1/w1_master_slaves
To read the temperature from sensor, type the following command, replace device_id with your device identifier.
cat /sys/bus/w1/devices/[device_id]/w1_slave

Step 4: Database creation

Now create a simple database containing only two tables.

Below the table in which we will store the connected devices.
Field Description
name Friendly name
dev_id Device identifier
CREATE TABLE w1_devices ( w1_devices_id serial NOT NULL, name character varying(128) NOT NULL, -- Friendly name dev_id character varying(64) NOT NULL, -- Device identifier CONSTRAINT w1_devices_pkey PRIMARY KEY (w1_devices_id), CONSTRAINT w1_devices_dev_id_key UNIQUE (dev_id) )
Table containing temperature measurements.
Field Description
date_insert Measurement time
w1_devices_id Row identifier from table "w1_devices"
temperature The value of the measured temperature
date_check Time of the last measurement.
CREATE TABLE temperature ( temperature_id serial NOT NULL, date_insert timestamp with time zone NOT NULL DEFAULT now(), w1_devices_id integer NOT NULL, temperature numeric(10,2), date_check timestamp with time zone, CONSTRAINT temperature_pkey PRIMARY KEY (temperature_id), CONSTRAINT temperature_w1_devices_id_fkey FOREIGN KEY (w1_devices_id) REFERENCES w1_devices (w1_devices_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION )

Step 5: Script in PHP

Below is a script that will be executed every minute, via cron. This script measures the temperature of the sensors (DS18B20),
then compares the value of the measurement to the previous value. If the difference is greater than 0.1 degrees Celsius,
it inserts a new record into the table "temperature". If the difference is less than 0.1 degree celsius. This does the update of
the last measurement by changing the value of "date_check". I put this file in /var/www/html/

Before using these scripts, you need to configure your connection to the database server. I use an external
database on my server, but you can also use a local database on the Raspberry PI.
$dbname=''; $host=''; $dbuser=''; $dbpass='';
File temp.php
<?php error_reporting(E_ALL); ini_set('display_errors', 1); $dbname=''; $host=''; $dbuser=''; $dbpass=''; try { $db = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ echo "ERROR: " . $e->getMessage(); } //---------------------------------- $str = file_get_contents('/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves'); $dev_ds18b20 = preg_split("/\\r\\n|\\r|\\n/", $str); foreach( $dev_ds18b20 as $val ){ if( $val!='' ){ //echo "DS18B20: $val<br>\r\n"; $temp_path = "/sys/bus/w1/devices/$val/w1_slave"; $str = file_get_contents($temp_path); if( preg_match('|t\=([0-9]+)|mi', $str, $m) ){ $temp = $m[1] / 1000; //echo "temp=$temp<br>\r\n"; SaveMeasurement( $db, $val, $temp); } } } //---------------------------------- function SaveMeasurement($db, $dev_id, $temp){ try { $sql = $db->query ("SELECT * FROM w1_devices WHERE dev_id='$dev_id'"); $result = $sql->fetch( PDO::FETCH_ASSOC ); if( $result===false ){ die('You must first define the device: '); }else{ $sql = $db->query ("SELECT * FROM temperature WHERE w1_devices_id='$result[w1_devices_id]' ORDER BY date_insert DESC LIMIT 1"); $result = $sql->fetch( PDO::FETCH_ASSOC ); $ins = true; if( $result!==false && abs($result['temperature'] - $temp) < 0.1 ){ echo abs($result['temperature'] - $temp); $ins = false; } if( $ins ){ $sql = "INSERT INTO temperature(w1_devices_id, temperature) VALUES(:w1_devices_id, :temperature)"; $statement = $db->prepare($sql); $statement->bindValue(':w1_devices_id', $result['w1_devices_id']); $statement->bindValue(':temperature', $temp); $statement->execute(); $statement->closeCursor(); }else{ $sql = "UPDATE temperature SET date_check=now() WHERE temperature_id=:temperature_id"; $statement = $db->prepare($sql); $statement->bindValue(':temperature_id', $result['temperature_id']); $statement->execute(); $statement->closeCursor(); } } }catch(PDOException $e){ echo "ERROR: " . $e->getMessage(); } } ?>
Now we will create a shell script.
cd /var/www/html/ nano cron_temp.sh chmod +x cron_temp.sh
File cron_temp.sh
php -f /var/www/html/temp.php
Then we add this file to corn.
crontab -e
We add this line and save.
* * * * * /var/www/html/cron_temp.sh
Saving the results of temperature measurement every minute should already work. Now display the results from the table "temperature".
For this purpose we write another two scripts.
File ds18b20.php
<?php error_reporting(E_ALL); ini_set('display_errors', 1); echo "time: " . time() . "\r\n<br>"; $str = file_get_contents('/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves'); $dev_ds18b20 = preg_split("/\\r\\n|\\r|\\n/", $str); foreach( $dev_ds18b20 as $val ){ if( $val!='' ){ echo "DS18B20: $val<br>\r\n"; $temp_path = "/sys/bus/w1/devices/$val/w1_slave"; $str = file_get_contents($temp_path); if( preg_match('|t\=([0-9]+)|mi', $str, $m) ){ $temp = $m[1] / 1000; echo "temp=$temp<br>\r\n"; } } } $dbname=''; $host=''; $dbuser=''; $dbpass=''; try { $db = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ echo "ERROR: " . $e->getMessage(); } ?><div style="background-color: #a0a0a0; vertical-align: bottom;"><?php $sql = " SELECT * FROM ( SELECT * FROM temperature ORDER BY date_insert DESC LIMIT 35 ) AS T ORDER BY date_insert "; foreach ($db->query($sql) as $row) { ?><div style="display:inline-block; margin-right: 1px; background-color: #66b3ff; vertical-align: bottom; width: 50px; height: <?php echo 2 * $row['temperature']; ?>px;"><?php echo $row['temperature']; ?></div><?php } ?></div>
To automatically refresh the results every second, we use Jquery and AJAX.
File index.php
<?php error_reporting(E_ALL); ini_set('display_errors', 1); $dbname=''; $host=''; $dbuser=''; $dbpass=''; try { $db = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ echo "ERROR: " . $e->getMessage(); } ?><html> <head> <title><?php echo exec('hostname'); ?></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> function check(){ $.get( "ds18b20.php", function( data ) { $( "#result" ).html( data ); }); } </script> <script type="text/javascript"> <!-- setInterval( "check()" ,1000); //--> </script> </head> <body> <div id="result"></div> </body> </html>
Download source code: all-files.zip

Step 6: The end

Below the table with temperature measurements. The sensor is mounted in my living room and the temperature update is one minute.
date_insert temperature date_check
2017-09-19 16:52:16 27.56 ℃
2017-09-19 16:51:17 27.69 ℃
2017-09-19 16:49:16 27.50 ℃ 2017-09-19 16:50:17
2017-09-19 16:48:18 27.25 ℃
2017-09-19 12:06:33 24.63 ℃ 2017-09-19 12:19:33
2017-09-19 12:03:33 24.75 ℃ 2017-09-19 12:05:32
2017-09-19 11:58:33 24.63 ℃ 2017-09-19 12:02:33
2017-09-19 11:55:33 24.75 ℃ 2017-09-19 11:57:33
2017-09-19 11:45:33 24.63 ℃ 2017-09-19 11:54:33
2017-09-19 11:35:34 24.75 ℃ 2017-09-19 11:44:33
2017-09-19 11:30:33 24.63 ℃ 2017-09-19 11:34:32
2017-09-19 11:24:33 24.75 ℃ 2017-09-19 11:29:32
2017-09-19 11:21:33 24.63 ℃ 2017-09-19 11:23:33
2017-09-19 11:17:33 24.31 ℃ 2017-09-19 11:20:33
2017-09-19 11:16:33 24.44 ℃
2017-09-19 11:05:33 24.75 ℃ 2017-09-19 11:15:32
2017-09-19 10:52:33 24.88 ℃ 2017-09-19 11:04:33
2017-09-19 10:50:33 25.00 ℃ 2017-09-19 10:51:33
2017-09-19 10:45:33 24.88 ℃ 2017-09-19 10:49:33
2017-09-19 10:42:33 25.00 ℃ 2017-09-19 10:44:33
2017-09-19 10:30:33 24.88 ℃ 2017-09-19 10:41:33
2017-09-19 10:28:33 24.75 ℃ 2017-09-19 10:29:33
2017-09-19 10:22:33 24.63 ℃ 2017-09-19 10:27:33
2017-09-19 10:20:33 24.75 ℃ 2017-09-19 10:21:33
2017-09-19 10:11:33 25.00 ℃ 2017-09-19 10:19:33
2017-09-19 09:55:33 24.88 ℃ 2017-09-19 10:10:33
2017-09-19 09:31:32 24.75 ℃ 2017-09-19 09:54:33
2017-09-19 09:24:33 24.63 ℃ 2017-09-19 09:30:33
2017-09-19 09:23:33 24.50 ℃
2017-09-19 09:07:33 24.63 ℃ 2017-09-19 09:22:32
2017-09-19 08:50:33 24.50 ℃ 2017-09-19 09:06:33
2017-09-19 08:42:33 24.38 ℃ 2017-09-19 08:49:33
2017-09-19 08:30:33 24.25 ℃ 2017-09-19 08:41:32
2017-09-19 08:23:33 24.13 ℃ 2017-09-19 08:29:33
2017-09-19 08:14:33 24.00 ℃ 2017-09-19 08:22:33
2017-09-19 08:10:33 23.88 ℃ 2017-09-19 08:13:33
2017-09-19 08:07:33 23.75 ℃ 2017-09-19 08:09:33
2017-09-19 08:03:33 23.56 ℃ 2017-09-19 08:06:33
2017-09-19 08:02:33 23.44 ℃
2017-09-19 08:00:33 23.25 ℃ 2017-09-19 08:01:33
2017-09-19 07:58:33 23.13 ℃ 2017-09-19 07:59:33
2017-09-19 07:56:33 23.00 ℃ 2017-09-19 07:57:33
2017-09-19 07:55:33 22.88 ℃
2017-09-19 07:54:33 22.75 ℃
2017-09-19 07:43:33 22.63 ℃ 2017-09-19 07:53:33
2017-09-19 07:28:33 22.75 ℃ 2017-09-19 07:42:33
2017-09-19 07:19:33 22.88 ℃ 2017-09-19 07:27:33
2017-09-19 07:12:33 23.00 ℃ 2017-09-19 07:18:33
2017-09-19 07:09:34 23.13 ℃ 2017-09-19 07:11:33
2017-09-19 05:45:33 23.00 ℃ 2017-09-19 07:08:33
2017-09-19 05:42:34 22.88 ℃ 2017-09-19 05:44:33
2017-09-19 04:24:33 23.00 ℃ 2017-09-19 05:41:34
2017-09-19 04:11:34 23.13 ℃ 2017-09-19 04:23:33
2017-09-19 04:03:33 23.00 ℃ 2017-09-19 04:10:33
2017-09-19 02:23:33 23.13 ℃ 2017-09-19 04:02:33
2017-09-19 02:12:33 23.25 ℃ 2017-09-19 02:22:34
2017-09-19 02:10:33 23.13 ℃ 2017-09-19 02:11:33
2017-09-19 01:58:34 23.31 ℃ 2017-09-19 02:09:33
2017-09-19 01:57:33 23.19 ℃
2017-09-19 01:40:33 23.31 ℃ 2017-09-19 01:56:33
2017-09-19 01:38:34 23.19 ℃ 2017-09-19 01:39:33
2017-09-19 00:57:33 23.31 ℃ 2017-09-19 01:37:33
2017-09-19 00:48:34 23.19 ℃ 2017-09-19 00:56:33
2017-09-19 00:47:33 23.31 ℃
2017-09-19 00:43:33 23.19 ℃ 2017-09-19 00:46:33
2017-09-19 00:41:33 23.31 ℃ 2017-09-19 00:42:33
2017-09-19 00:28:33 23.19 ℃ 2017-09-19 00:40:34
2017-09-19 00:22:33 23.38 ℃ 2017-09-19 00:27:34
2017-09-18 23:30:33 23.25 ℃ 2017-09-19 00:21:33
2017-09-18 23:10:34 23.38 ℃ 2017-09-18 23:29:33
2017-09-18 23:09:33 23.25 ℃
2017-09-18 23:05:34 23.44 ℃ 2017-09-18 23:08:33
2017-09-18 23:01:34 23.31 ℃ 2017-09-18 23:04:34
2017-09-18 22:44:34 23.44 ℃ 2017-09-18 23:00:33
2017-09-18 22:10:33 23.56 ℃ 2017-09-18 22:43:34
2017-09-18 22:09:34 23.69 ℃
2017-09-18 21:39:33 23.50 ℃ 2017-09-18 22:08:34
2017-09-18 21:37:33 23.38 ℃ 2017-09-18 21:38:33
2017-09-18 21:31:34 23.50 ℃ 2017-09-18 21:36:34
2017-09-18 21:25:33 23.38 ℃ 2017-09-18 21:30:33
2017-09-18 21:21:34 23.50 ℃ 2017-09-18 21:24:33
2017-09-18 21:00:34 23.38 ℃ 2017-09-18 21:20:34
2017-09-18 20:59:34 23.50 ℃
2017-09-18 20:55:34 23.38 ℃ 2017-09-18 20:58:34
2017-09-18 20:49:33 23.50 ℃ 2017-09-18 20:54:34
2017-09-18 20:43:34 23.38 ℃ 2017-09-18 20:48:33
2017-09-18 20:40:33 23.50 ℃ 2017-09-18 20:42:34
2017-09-18 20:37:34 23.38 ℃ 2017-09-18 20:39:34
2017-09-18 20:35:33 23.50 ℃ 2017-09-18 20:36:34
2017-09-18 20:33:34 23.38 ℃ 2017-09-18 20:34:33
2017-09-18 20:25:33 23.50 ℃ 2017-09-18 20:32:34
2017-09-18 20:10:34 23.38 ℃ 2017-09-18 20:24:33
2017-09-18 19:56:34 23.25 ℃ 2017-09-18 20:09:34
2017-09-18 19:52:34 23.13 ℃ 2017-09-18 19:55:34
2017-09-18 19:49:34 23.00 ℃ 2017-09-18 19:51:34
2017-09-18 19:48:34 22.88 ℃
2017-09-18 19:47:33 22.75 ℃
2017-09-18 19:46:33 22.63 ℃
2017-09-18 19:45:33 22.75 ℃
2017-09-18 19:43:33 22.88 ℃ 2017-09-18 19:44:33