Temperature Sensors and the Raspberry Pi

From KlavoWiki
Jump to navigationJump to search

Work in in Progress
I followed the document Temperature Sensing from Adafruit to configure the breadboard.

Modules

vi /etc/modules

add the following lines

w1-gpio
w1-therm

BASH Write to MySQL

I have this file as a CRON job that is ran every minute.

crontab -l

* * * * * /root/temp.sh >/dev/null
#! /bin/bash
#sudo modprobe w1-gpio
#sudo modprobe w1-therm


temp1=$(cat /sys/bus/w1/devices/28-0000055018bd/w1_slave | grep  -E -o ".{0,0}t=.{0,5}" | cut -c 3-)
temp2=$(cat /sys/bus/w1/devices/28-0000055099d2/w1_slave | grep  -E -o ".{0,0}t=.{0,5}" | cut -c 3-)


temp1=$(echo "scale=3; $temp1 / 1000" | bc -q)
temp2=$(echo "scale=3; $temp2 / 1000" | bc -q)

mysql temperature -e "insert into temp (no1, no2) values('${temp1}','${temp2}');"

PHP

Read Directly from Sensors

<?php
$sensors = array(
                    "/sys/bus/w1/devices/28-0000055018bd/w1_slave",
                    "/sys/bus/w1/devices/28-0000055099d2/w1_slave"
                );

$count = 0;

print "Direct read from sensors<br>";
print "-------------------------<br>";

while ($count <= 1) {

     // Open resource file for thermometer
     $thermometer = fopen($sensors[$count], 'r');

     // Get the contents of the resource
     $thermometerReadings = fread($thermometer, filesize($sensors[$count]));

     // Close resource file for thermometer
     fclose($thermometer);

     // We're only interested in the 2nd line, and the value after the t= on the 2nd line
     preg_match("/t=(.+)/", preg_split("/\n/", $thermometerReadings)[1], $matches);
     $temperature = $matches[1] / 1000;

     // Convert to Fahernheit
     $fahernheit = $temperature*9/5+32;

     // Output the temperature
     print "Sensor " . ($count+1) . " : $temperature&degC --- $fahernheit&degF<br>";

     $count++;
}
?>

Read from Database

This section is not directly related to the Raspberry Pi or the temperature sensors as this is more about querying data from a database.

<?php
print "Data from MySQL<br>";
print "--------------------<br>";


// Connect to MySQL Host,Username,Password,Database
$conn = new mysqli('localhost', 'root', '', 'temperature');
   // check connection
   if ($conn->connect_error)
     {
        trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
     }


// What is the SQL query (Retrieve the last 35 records)
$sqlquery = 'SELECT * FROM temp ORDER BY date DESC LIMIT 35';

// Query SQL and store
$info = $conn->query($sqlquery);
   //check for valid connection
   if($info === false)
       {
          trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
       } else
       {
         print "Last <b><font color=red>" . $info->num_rows . "</b><font color=black> results.<br>";

         while ($row = mysqli_fetch_array($info))
           {
//            printf ("%s (%s) (%s)<br>",$row[0],$row[1],$row[2]);
              print $row[0] . " <font color=blue>" . $row[1] . " <font color=green>" . $row[2] . '<font color=black><br>';
           }

        }

// Close Sql Connection
$info->close();
?>


MySQL Database

create database temperature;
use temperature;
create table temp (
  date TIMESTAMP,
  no1 FLOAT(5,3),
  no2 FLOAT(5,3),
  no3 FLOAT(5,3),
  no4 FLOAT(5,3),
  no5 FLOAT(5,3)
);