Monday, September 17, 2007

Arduino + Temperature + Linux



With the temperature sensor ready to be tested, I need them to be on all the day and a PC storing the temperature values. My laptop is too lazy, always hibernating as soon as I leave it alone. But what about my Linux server? He is always on, with its low consumption VIA processor and 5 free USB connections. That's my target.

Install FTDI Driver for Debian Etch

I've a Debian Etch installed so I started to look for drivers for the FTDI, the USB to serial chip that comes with the Arduino board. To do so I typed:

apt-cache search ftdi

and I received this as answer
ftdi-eeprom - Tool for reading/erasing/flashing FTDI USB chip eeproms
libftdi-dev - Development files for libftdi
libftdi0 - Library to control and program the FTDI USB controller

so I decided to install the libftdi0 typing:

apt-get install libftdi0

After the installation I checked that all was ok just plugin the Arduino board, and I received the following info from the OS:
usb 2-1: new full speed USB device using uhci_hcd and address 2
usb 2-1: configuration #1 chosen from 1 choice
usbcore: registered new driver usbserial
drivers/usb/serial/usb-serial.c: USB Serial support registered for generic
usbcore: registered new driver usbserial_generic
drivers/usb/serial/usb-serial.c: USB Serial Driver core
drivers/usb/serial/usb-serial.c: USB Serial support registered for FTDI USB Serial Device
ftdi_sio 2-1:1.0: FTDI USB Serial Device converter detected
drivers/usb/serial/ftdi_sio.c: Detected FT232BM
usb 2-1: FTDI USB Serial Device converter now attached to ttyUSB0
usbcore: registered new driver ftdi_sio
drivers/usb/serial/ftdi_sio.c: v1.4.3:USB FTDI Serial Converters Driver

This confirmed the USB driver had been properly installed.

Accessing USB serial port with python

First I searched to find the pySerial package for linux, I typed:

apt-cache search python serial

and I received this as answer
gpsd - GPS (Global Positioning System) daemon
libsyck0-dev - YAML parser kit -- development files
libyaml-ruby - YAML for Ruby
php4-syck - YAML parser kit -- PHP4 bindings
python-fibranet - cooperative threading and event driven framework
python-serial - Module encapsulating access for the serial port
python-simplejson - Simple, fast, extensible JSON encoder/decoder for Python
python-syck - YAML parser kit -- python bindings (default package)
python-twisted-news - An NNTP protocol implementation with client and server
python-vobject - parse iCalendar and VCards in python

So I installed the python-serial package by typing:

apt-get install python-serial

To test that every thing was properly installed I coded this python example:

import serial

serialPort = serial.Serial('/dev/ttyUSB0' , 115200, timeout=5)
print serialPort.portstr

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

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

serialPort.close()


Configure modpython to access .py files through Apache

  • Install modPython for debian
  • apt-get install libapache2-mod-python
  • Set python module as available:
  • cd /etc/apache2/mods-enabled/
    sudo ln -s ../mods-available/mod_python.load mod_python.load
  • Add python handler to the web configuration
  • cd /etc/apache2/sites-available/
    sudo gedit default
    
    <Directory /var/www/>
           Options Indexes FollowSymLinks MultiViews
           AllowOverride AuthConfig
           Order allow,deny
           allow from all
    
            AddHandler mod_python .py
            PythonHandler mod_python.publisher
            PythonDebug On
    </Directory>
  • Restart apache
  • /etc/init.d/apache2 restart
  • Create test file index.py and move web home directory
  • import serial
    
    def ReadTemperature():
      serialPort = serial.Serial('/dev/ttyUSB0' , 115200, timeout=5)
      print serialPort.portstr
    
      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
    
      average = sum/numSamples
      #print "average:" + str(average)
    
      serialPort.close()
      return average
    
    def index():
      msg = """
      <html>
      <script language="javascript">
      setTimeout("window.location = 'http://boundingbox.homelinux.net/python/index.py'",5000)
      </script>
      <title>ModPython</title>
      <body>
      <p>Temperature: %f</p>
      </body>
      </html>
      """ % (ReadTemperature())
      return msg
And it worked.

2 comments:

  1. Wow, quite interesting Alberto. Keep up the good work!...

    ReplyDelete
  2. When I run this in apache I get "SerialException: could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0'" even though I can run it ok in a shell environment. Do you know what I am doing wrong?

    Thanks

    ReplyDelete