Friday, 14 June 2013

Arduino temperature logger to MySQL

As a first time blogger I decided to create a tutorial which is essentially my experiences of creating an Arduino sketch. The project is to log temperature recordings from the Arduino Ethernet directly to a local mysql database.

Components

  • Arduino Ethernet
  • Thermometer (TMP36)
  • FTDI cable (or chip with usb cable)
  • 5 wires

Step 1

Download the arduino software from here.

Step 2

Download FTDI driver from here. Choose the valid driver for your computer under the sub-heading 'Currently Supported VCP Drivers'.

Step 3

Follow the steps to create the Arduino example circ-10. A couple good guide can be found on the The Arduino Experience blog and oomalout. We will be using this code to calculate the temperature. When you upload the sketch you can go to Arduino -> Tools -> Serial Monitor to view the temperature output.

Step 4

Once you get that sketch working we can start to look at mysql. A great library can be found at Chuck's Blog. It takes a bit of effort locating the files, they can be found here. You will also need the 'sha1' library. I can't remember exactly where I downloaded it from but I think this is one is the same Arduino-oath-token. Put sha1.cpp and sha1.h in a folder called sha1 in your Arduino Library folder.

Step 5 - Preparing database

I'm going to assume you already got MySQL installed and running. Here is the SQL statement to create the table. The main thing to understand here is that 'created_at' is a timestamp defaulted to current_timestamp. This adds the time automatically when a record is inserted, so you don't have to code for it.

CREATE TABLE `temperature` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `temperature` float DEFAULT NULL,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

Step 6

So hopefully everything is properly installed. Here is the code. 
Note. I have know prior knowledge of C programming and so I found building the insert string really difficult. It is most likely horrible code, if there is a better way to do this please post.

Replace the X's in the mac address with the characters on the back of your arduino. Replace the server_addr with the ip of the computer that has your mysql database.


#include "SPI.h"
#include "Ethernet.h"
#include "sha1.h"
#include "mysql.h"

/* Setup for Ethernet Library */
byte mac_addr[] = { 0xX, 0xX, 0xX, 0xX, 0xX, 0xX };
IPAddress server_addr(192, 168, 1, 1);

/* Setup for the Connector/Arduino */
Connector my_conn; // The Connector/Arduino reference

int temperaturePin = 0;
char user[] = "arduino";
char password[] = "arduino";

void setup() {
  Ethernet.begin(mac_addr);
  Serial.begin(14400);
  delay(1000);
  Serial.println("Connecting...");
  if (my_conn.mysql_connect(server_addr, 3306, user, password))
  {
      Serial.println("Query Success!");
  }
  else
    Serial.println("Connection failed."); 
}

void loop() {
  addTempEvent();
}

//taken straight from the adruino circ-10 example
float getVoltage(int pin){
return (analogRead(pin) * .004882814);
}

void addTempEvent()
{
  //taken straight from the adruino circ-10 example
  float temperature = getVoltage(temperaturePin);
  //temperature = (((temperature - 0.5) * 100)*1.8) + 32;  //Fahrenheit
  temperature = (temperature - 0.5) * 100; 
  
  //I'm sure there is a cleaner way to build this string
  char insert_sql[110] = "INSERT INTO tempMonitor.temperature (id,temperature) VALUES (ID,";
  dtostrf(temperature,1,2, &insert_sql[64]);
  char* array3 = ")";
  strcat(insert_sql,array3);
  
  //Serial.println(insert_sql);
  delay(5000);
  my_conn.cmd_query(insert_sql);
  delay(120000); 
}

I'm not sure if the temperature was very accurate. I may buy a better one or some of the numbers may need tweaked. However I consider the project a success as it does what I set out to do.

Any questions or improvements are more than welcome.





6 comments:

  1. i try it. i follow your steps but it didn't work..
    i dont know what is may error.. the compiler seems uploading the codes successfully.. can u help me

    ReplyDelete
  2. and can u give me some pictures or videos to make it easy to understand.. thanks

    ReplyDelete
  3. @Senando Benig, sorry my steps did not work for you. I had a similar issue initially also. My problem was that the router was blocking the request. I had to log on to my router and set the Arduino Ethernet's IP to static and allow access to it.

    Do you know if your Arduino Board ethernet is working?
    Here is a helpful video I found http://www.youtube.com/watch?v=nYzEbEOMGuo

    Also, have you set up a mysql user/password that has access to the database?

    I had some issues trying to get the insert query right. You could
    try some troubleshooting by commenting out the Mysql parts and uncomment Serial.println(insert_sql); and check that the insert statement looks valid in tool monitor.

    I may add video or pictures in the future.

    ReplyDelete
  4. Great blog. You write to MySQL every 2 minutes. Did you experience any dropped connections? How do you suggest to deal with that?

    ReplyDelete
  5. @Larry, I ran the app for a week and didn't have any problems... I don't think.

    I wouldn't know where to start to offer any suggestions, I frequently use the brute force method and a shit load of googling, because I'm really just learning this stuff myself.

    The main reason for blog was to gather all my googling notes in one place so that I wouldn't have to do it again :)

    It's a pretty lame answer I know, you could try Chuck's blog, I have a link in 'Step 4'. I think he's the person that could answer your questions

    ReplyDelete
  6. hey man, thanks for putting this out there. it worked great. it helps me a LOT. thanks again.

    ReplyDelete