Friday, October 12, 2007

Debian Etch + Temperature Logging

Time to store the data. Being able to sense the temperature it's just the first step, storing and plotting the resulting data is the power, muhahaha, muhahaha

Storage backend

I will use MySql database, it's easy to use and to install

Choose a python module to access MySql database

I will use MySQL-Python because it supports python dbapi, that guarantees a common interface to access any database from python, so if in the future I change my mind and decided to use PostgreSQL database, the code to access it will be de

Steps to install it in debian

  1. apt-get install python-mysqldb

Python code

Class to access mysql database

import MySQLdb
import serial
import time

class mysqlDB:

  def __init__(self, dbName, user, password):
    """ Constructor """
    self.cursor = None
    self.user = user
    self.password = password = dbName
    self.connection = MySQLdb.connect(

  def __del__(self):
    """ Destructor """
    if self.cursor != None:
    if self.connection != None:

  def Query(self, query, isSelect = True):
    if self.connection != None:
      self.cursor = self.connection.cursor()
      result = self.cursor.execute(query)

      if isSelect:
        return (self.cursor, self.cursor.fetchall())
      return None

def ReadTemperature():
    serialPort = serial.Serial('/dev/ttyUSB0' , 115200, timeout=5)
    #print serialPort.portstr
  except serial.SerialException:
    return (-1,-1)

  # Read LM35
  sum = 0
  numSamples = 50
  for i in range(numSamples):
    line = serialPort.readline()
    #print line
    temperature = (5.0 * float(line) * 100.0)/1024.0;
    sum += temperature

  average1 = sum/numSamples
  #print "average:" + str(average)

  # Read DS1621
  line = serialPort.readline()
  average2 = float(line) * 0.5

  return (average1, average2)

if __name__ == "__main__":
  (lm35Temp, ds1620Temp) = ReadTemperature()
  timestamp = time.time()
  db = mysqlDB("database", "user", "passwd")
  query = "insert into temperatureLog (timestamp, temperature, sensorId) values (%d,%f,%d);"
             % (timestamp, int(lm35Temp), 1)
  db.Query(query, False)

  query = "insert into temperatureLog (timestamp, temperature, sensorId) values (%d,%f,%d);"
             % (timestamp, ds1620Temp, 2)
  db.Query(query, False)

cron configuration

I will start storing one measurement per hour and then check if that's enough. To do so I will ask for help to the cron.

  1. Create a readTemperature bash script invoking the python script

  2. #!/bin/bash
    python /home/user/crondScripts/
  3. chmod 755 readTemperature

  4. cp readTemperature /etc/cron.hourly/

cron configuration (2)

I finally decided to store one measurement each 15min, to do so, I cheated a bit, I'm sure this is not the best way to do it, so fell free to give me feedback

  1. Check that readTemperature is the only one script in /etc/cron.hourly

  2. Edit /etc/crontab and modify the hourly configuration

  3. Set hourly configuration to
    */15 *  * * *   root    cd / && run-parts --report /etc/cron.hourly

No comments:

Post a Comment

About Me

Popular Posts

Designed By Seo Blogger Templates Published.. Blogger Templates