Project 4 - Temperature


Fritzing image
Fritzing

Resistor 1kohm
NTC Thermistor TDC 210 - nominal resistance at 25 Celsius: 1kohm

Arduino sketch
NOME FILE: Termistore_Database.pde
/*
- created 2011-10-31 by Massimo Pacilio
- Use to send data to Processing sketch "MySQL_INSERT"
- This sketch is in the public domain
*/

#include <math.h>

const unsigned int SENSOR_PIN = A0;
const unsigned int BAUD_RATE = 9600;

//equation to obtain the value of the temperature from a thermistor
//http://arduino.cc/playground/ComponentLib/Thermistor2
//http://en.wikipedia.org/wiki/Thermistor#Steinhart-Hart_equation
double Thermister (int RawADC){
 double Temp;
 Temp = log(((10240000/RawADC) - 10000));
 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
 Temp = Temp - 273.15;            // Convert Kelvin to Celsius
 return Temp;
}

void setup() {
 Serial.begin(BAUD_RATE);
 pinMode(SENSOR_PIN, INPUT);
}

void loop() {
  //assign to variable 'temp' the value of the temperature calculated by thermistor
  int temp = int(Thermister(analogRead(SENSOR_PIN)));
  Serial.println(temp);  // send the value to the serial port
   delay(30000);
}


Processing sketch
NOME FILE: MySQL_INSERT


import de.bezier.data.sql.*;
import processing.serial.*;

// created 2005-05-10 by fjenett
// updated fjenett 20080605
// modified 2011-10-31 by Massimo Pacilio
// this sketch needs the Arduino sketch "Termistore_Database"
// See MySQL database "test", table "temperature"

int temp = 0; 
Serial port;
String buffer = "";
PFont font;

MySQL dbconnection;


void setup() {
  size( 100, 100 );
  font = loadFont("ArialMT-48.vlw");  
  fill(255);  
  textFont(font, 32);
//println(Serial.list());
  String arduinoPort = Serial.list()[0];
  port = new Serial(this, arduinoPort, 9600);
}

void draw() {
  background(0);
  if (port.available() > 0) {
    int inByte = port.read();
    if (inByte != 10) {
      buffer = buffer + char(inByte); 
    }
    else {
      if (buffer.length() > 1) {
        buffer = buffer.substring(0,buffer.length() -1);
        temp = int(buffer);
        buffer = "";
        port.clear(); 
        
        String user     = "root";
        String pass     = "";
        String database = "test";
        // connect to database of server "localhost"
        dbconnection = new MySQL( this, "localhost", database, user, pass );
    
        if ( dbconnection.connect() ) {
        
        // now send data to database
        dbconnection.execute( "INSERT INTO temperature (valore) VALUES ('"+temp+"');" );
        println();
        println("Istruzioni eseguite!");
        }
      
    }
  }    
}
}


PHP code
FILENAME: temperature_test.php

<html>
  <body>
      <table border="1" cellspacing="2" cellpadding="2">
      <tr bgcolor="#FFFF00">
      <th><font face="Arial, Helvetica, sans-serif"> Data </font></th>
      <th><font face="Arial, Helvetica, sans-serif"> Temperatura </font></th>
      </tr>

<?php
// created 2011-10-28 by Massimo Pacilio
// this code is in public domain

 include "db_test_temperature.php";

 mysql_connect($host,$username,$password);
 @mysql_select_db($database) or die("Impossibile selezionare il database.");
 $sql = "SELECT * FROM `temperature`";
 $risultati=mysql_query($sql);
 $num=mysql_numrows($risultati);

 mysql_close();

 $i=0;
 while ($i < $num) {
     $data=mysql_result($risultati,$i,"data");
     $valore=mysql_result($risultati,$i,"valore");
   
     echo "<tr>";
     echo "<td align=center>$data</td>";
     echo "<td align=center><b>$valore</b></td>";
     echo "</tr>";
     $i++;
}

?>

      </table>
  </body>
</html>



PHP code
FILENAME: db_test_temperature.php

<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "test";

mysql_connect($host, $username, $password)or die (mysql_error());
?>


MySQL code
DATABASE: test - TABLE: temperature

CREATE TABLE temperature (
    id_temperature INT(10) NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id_temperature),
    data TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    valore INT(2) NULL
)

The result on the web


30 comments:

  1. Hello, good work I wonder if you could mount some images or videos on how you see the php process, ie as those readings are displayed on the website.

    Very very good job ...

    ReplyDelete
    Replies
    1. Good Work for this Project.
      In think, to make this example, it is necessery to have ethernet shield. Please is it possible to get one same work but without ethernet shield?? just by using arduino usb cable to acces to the mysql database?
      Thanks

      Delete
  2. Good idea.
    I post the picture of the result, as you suggest.

    Thank you.

    ReplyDelete
    Replies
    1. Good Work for this Project.
      In think, to make this example, it is necessery to have ethernet shield. Please is it possible to get one same work but without ethernet shield?? just by using arduino usb cable to acces to the mysql database?
      Thanks

      Delete
    2. of course. as you can see, in this experiment I do not use an ethernet shield. The data is transmittted via usb port. Each value is stored in mysql database using a processing sketch.

      Delete
  3. Hi,

    I try with two different TDC 210 and the value for +-25°c are (80 - 90)

    i need to dive by 3 to approach the right value, but why ?

    ReplyDelete
    Replies
    1. Try to check the resistor and the equation.

      Delete
    2. Hi,
      The same occurs to me, the schema is fine, also the measured resistance from the TDC, I get 870 ohms.

      Where's the bug?

      Thanks,

      Delete
    3. Well, I've found it, it wasn't balanced as a voltage divider must be.
      I've had to replace the fixed resistor by one near the nominal resistance of the TDC 210, in this case 1k.

      Thanks for the inspiration!

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. What if i want to connect it to the server? not to the localhost. is it possible?

    ReplyDelete
    Replies
    1. Change localhost by your IP server.

      Delete
  6. I try repeat your project, but in my Arduino IDE on the imports show errors. How install the library correctly? My code don´t compile.

    ReplyDelete
    Replies
    1. Have you solved this problem? Have you controlled the installation of mysql library for Processing?

      Delete
  7. Good works !! Finally I find this code but I don't try yet. Can I know what is processing sketch ? Maybe it's stupid question but I don't really know. Thank you :D

    ReplyDelete
    Replies
    1. it's very simple. processing sketch is a code written for Processing. Maybe you don't know how to use this kind of language. It's very similar to the arduino language. You have to download the latest version of Processing IDE and compile the code. Launch it to open comunication between arduino board and pc via serial port.

      Delete
    2. processing code must be after arduino scetch?
      Where i must set processing code? help please

      Delete
  8. What's your OS to do this project ? Thanks :)

    ReplyDelete
  9. hi! I tried it but it doesnt work for me....
    in processing I get this error:
    processing.app.SketchException: unexpected char: 'i'

    I create a .pde in processing called mysql_insert,
    I create a .pde in processing called termistore_database
    than in mysql_insert, in sketch > add file, I added termistore_database
    is it right?
    It's my first time with arduino and processing!!!
    thank you very much !

    ReplyDelete
    Replies
    1. Consider that the "termistore_database.pde" file is an Arduino sketch, not a Processing sketch.

      Delete
  10. i have this error in processing:
    SQL.connect(): Could not connect to the database ( jdbc:mysql://mysql.hostinger.it/u40xxxx_temp ).

    why? i tryed 2 or 3 free mysql hosting but i have the same error...

    ReplyDelete
  11. Hi, is there any way, how to make processing work with more values? I have DHT11, which have temperature and humidity. Thank you

    ReplyDelete
  12. I am very new to programming php & mysql. how can I import these files?

    import de.bezier.data.sql.*;
    import processing.serial.*;

    ReplyDelete
  13. hai, sorry for this question. i have a problem with your "processing sketch - NOME FILE: MySQL_INSERT". it's a Arduino program, MySql program or PHP program? because i try to compiling this sketch on the Arduino program, but i have an error.

    ReplyDelete
    Replies
    1. This is a Processing sketch. You have to download and install the Processing IDE in order to use this code.

      Delete
  14. Hi, loved your blog. Need help every file runs but I cannot connect to database instead it shows "Impossibile selezionare il database".Thanks

    ReplyDelete
    Replies
    1. Check the file that contains the instructions to connect to your database (db_test_temperature.php).

      Delete