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.
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.
NOTE: In newer versions of the system, add the following entry to /boot/config.txt and restart the Raspberry PI.
With this command we can check the attached devices for one wire.
To read the temperature from sensor, type the following command, replace device_id with your device identifier.
Step 4: Database creation
Now create a simple database containing only two tables.
Below the table in which we will store the connected devices.
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.
Row identifier from table "w1_devices"
The value of the measured temperature
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,
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.