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.
- DS18B20 Sensor
- Raspberry Pi
- 10k oHm Resistor
- jumper Wires
Wiring DS18B20 With Raspberry Pi
GND from SENSOR goes to GND on Pi.
RED wire to 3V
Yellow Wire to GPIO4
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:
Now to test this out , Type in
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.
The DS18B20 sensor periodically writes to the w1_slave file, so we simply use the cat command to read it.
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
//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);
//Setup some nice formatting (i.e. 21,3)
$temp = number_format($temp / 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 ,
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.
<div id = "screen"></div>
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.