Home / Raspberry Pi / Home Automation / DS18B20 Temperature Sensor With Raspberry Pi in HTML Web Using PHP.

DS18B20 Temperature Sensor With Raspberry Pi in HTML Web Using PHP.

Temperature Logging Using Raspberry Pi .

 

So , Here we are gonna use DS18B20 Using PHP With Raspberry Pi and Show the temperature in a HTML Page Using AJAX and PHP. DS18B20 Using Raspberry Pi And PHP. DS18B20 Using PHP.

Lets start with Required Items.

  1.  DS18B20 Sensor
  2.  Raspberry Pi
  3.  10k oHm Resistor
  4.  jumper Wires

Wiring DS18B20 With Raspberry Pi

GND from SENSOR goes to GND on Pi.

RED wire to 3V

Yellow Wire to GPIO4

Wiring Raspberry Pi 1/2/3/ B With DS18B20 Temperature Sensor Wired.
Wiring Raspberry Pi 1/2/3/ B With DS18B20

 

 

 

 

 

 

 

image_1_l3l5lt

image_ejvfbjimage_az2yqg

 

 

 

 

 

 

Software Part

GPIO and Therm kernel modules

For Using DS18B20 Using PHP , Now we need to activate the kernel module for the GPIO pins on the Raspberry Pi and the thermometer DS18B20 Sensor module by executing the commands:

sudo modprobe w1-gpio
sudo modprobe w1-therm

We do not want to do that manually every time the Raspberry reboots, so we want to enable these modules on every boot. This can be done by editing the following lines to the file /etc/modules:

w1-gpio

w1-therm

Now to test this out ,  Type in

cd /sys/bus/w1/devices/

Now should see your device information, In the device drivers, your DS18B20 Sensor should be listed as a series of numbers and letters. In this case, the device is registered as 28-000005e2fdc3. You then need to access the sensor with the cd command, replacing our serial number with your own.

cd 28-000005e2fdc3

The DS18B20 sensor periodically writes to the w1_slave file, so we simply use the cat command to read it.

cat w1_slave

This yields the following two lines of text, with the output t= showing the temperature in degrees Celsius. A decimal point should be placed after the first two digits e.g. the temperature reading we’ve received is 30.125 degrees Celsius.


Now assuming that you have Apache Web Server And PHP Installed. Goto /etc/www/html/ folder which is the root folder of Apache Web Server. Now  we need to create a PHP File which get’s the temperature reading and stores it in a variable so that we could display that in a HTML Page.

In order to do that , create a file named temp.php

<?php
//File to read
$file = '/sys/devices/w1_bus_master1/10-000802292522/w1_slave';
//Read the file line by line
$lines = file($file);
//Get the temp from second line
$temp = explode('=', $lines[1]);
//Setup some nice formatting (i.e. 21,3)
$temp = number_format($temp[1] / 1000, 1, ',', '');
//And echo that temp
echo $temp . " °C";
?>

Now create another html page , Name it index.html And display the temperature to it using ajax.

Add this code in the footer of the page ,

 <script>
$(document).ready(function(){
setInterval(function(){
$("#screen").load('inc/temp.php')
}, 1000);
});
</script>

In this , #screen is the ID of div in which we want to display the temperature. Create a Div and Give its ID as screen.  And then it loads inc/temp.php file every 1000 milliseconds.


<html>
<body>
<div id = "screen"></div>
</body>
</html>
<script>
$(document).ready(function(){
setInterval(function(){
$("#screen").load('inc/temp.php')
}, 1000);
});
</script>

I have used bootstrap to make a beautiful panel for displaying temperature . You can add multiple icons and glyphicons as well to make it more attractive. 

This is a very simple HTML Page just for displaying temp.

So now you have your live temperature monitor ready within an hour. 

About Abdul Hannan Mustajab

Well hello there! Not wanting to keep myself obscure,here's something about me. WHO AM I? Im a young 17 year old,based in India. Currently pursuing education in the science - math stream , im an Computer Science aspirator. WHAT DO I LIKE? A simple statement says it all, computers & tech. WHAT DO I KNOW? I have a passion for programming and have learned the following programming languages so far - C,python,PHP & angular js. Also do I possess knowledge regarding web servers and linux and shell related stuff. WHAT ARE MY INTERESTS? Im interested in Raspberry pi & Arduino associated home automation and well that's what my website is about!

Check Also

Setup Instagram AutoLiker Bot On Raspberry Pi

  Do you have a instagram account and you wish to have thousands of followers …

Leave a Reply