Today, I setup my raspberry pi as a temperature sensor. I mainly use a LM35 temperature sensor and an IC “ADC0804″ as an analog to digital converter. I also setup a database to record down the temperature data and then display result on web browser with Chat format. The connection of the whole electronics circuit is as below diagram. temp0

Program script:

1) The coding the a python program to take temperature data is as the program below:

#!/usr/bin/env python
# author: Powen Ko    Program Name:
import time, RPi.GPIO as GPIO
import urllib

def fetch_thing(url, params, method):
params = urllib.urlencode(params)
if method==’POST':
f = urllib.urlopen(url, params)
f = urllib.urlopen(url+’?’+params)
return (, f.code)

GPIO.setup(7, GPIO.IN)
GPIO.setup(11, GPIO.IN)
GPIO.setup(12, GPIO.IN)
GPIO.setup(13, GPIO.IN)
GPIO.setup(15, GPIO.IN)
GPIO.setup(16, GPIO.IN)
GPIO.setup(18, GPIO.IN)
GPIO.setup(22, GPIO.IN)
while True:
     a0 = GPIO.input(7)
     a1 = GPIO.input(11)
     a2 = GPIO.input(12)
     a3 = GPIO.input(13)
     a4 = GPIO.input(15)
     a5 = GPIO.input(16)
     a6 = GPIO.input(18)
     a7 = GPIO.input(22)
     print a7,a6,a5,a4,a3,a2,a1,a0,”[“,total,”]”,”[C=”,temp,”]”
     content, response_code = fetch_thing(
                    {‘id': 1, ‘temp': temp},

2) Then, I save the temperature data into the database via a web php program as below:

if (mysqli_connect_errno()) {
echo “Failed to connect to MySQL: ” . mysqli_connect_error();

$now= date(‘Ymdhms’);
$id = $_GET[‘id’];
$temp = $_GET[‘temp’];
mysqli_query($con,”INSERT INTO temp (datatime,temp,userid)
VALUES ($now,$temp,$id)”);

echo “ get it”.”, date time=”.$now.”, temp=”.$temp.”, id=”.$id;

Finally, I view data by using a php web browser program as below and also screen dump result as below:

<!doctype html>
                <title>Bar Chart</title>
                <script src=”Chart.js-master/Chart.js”></script>
                <div style=”width: 50%”>
                        <canvas id=”canvas” height=”450″ width=”800″></canvas>
if (mysqli_connect_errno()) {
  echo “Failed to connect to MySQL: ” . mysqli_connect_error();

$result = mysqli_query($con,”SELECT * FROM temp”);

echo “<table border=’1′>
<th>Date Time</th>
<th>user ID </>


while($row = mysqli_fetch_array($result))
  echo “<tr>”;
  echo “<td>” . $row[‘datatime’] . “</td>”;
  echo “<td>” . $row[‘temp’] . “</td>”;
  echo “<td>” . $row[‘userid’] . “</td>”;
  echo “</tr>”;
  $Lables=$Lables.'”‘. $row[‘datatime’].'”,';
  $temps=$temps.'”‘. $row[‘temp’].'”,';

echo “</table>”;

var barChartData = {
                labels : [<?php echo  $Lables;  ?>],
                datasets : [
                                fillColor : “rgba(20,20,20,0.5)”,
                                strokeColor : “rgba(220,220,220,0.8)”,
                                highlightFill: “rgba(220,220,220,0.75)”,
               highlightStroke: “rgba(220,220,220,1)”,
                                data : [<?php echo  $temps;  ?>
        window.onload = function(){
                var ctx = document.getElementById(“canvas”).getContext(“2d”);
                window.myBar = new Chart(ctx).Bar(barChartData, {
                        responsive : true


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>