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
    self.name = dbName
    self.connection = MySQLdb.connect(
                            host='localhost',
                            user=self.user,
                            passwd=self.password,
                            db=self.name )

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

  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())
      else:
        self.connection.commit()
    else:
      return None

def ReadTemperature():
  try:
    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):
    serialPort.write("s")
    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
  serialPort.write("r")
  line = serialPort.readline()
  average2 = float(line) * 0.5

  serialPort.close()
  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/readTemperature.py
  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