Thursday 18 July 2013

Python Arduino DAQ on the Mac

Kevin Hughes created a basic DAQ system in python to read Arduino analog voltages and display them on screen. He told me it should be platform independent, so I decided to try it on my mac. Steps to getting there [including mistakes where I slipped up]:

Get the arduino sketch running. This was simple, although I dropped the serial data rate to 57600 because that works with all the other stuff in my ecosystem.

[This was probably a huge mistake, as it looks like many libraries only work with python 2.7. Restored machine state and started again. Install python 3.3.2 for the mac, because that's the latest. After the install I still got python 2.7.2 from a terminal command line, even after a restart, but 'python3' gets 3.3.2.]

Serial Communications

Rely on the installed python 2.7.2 on the mac and install pySerial using Adafruit tutorial.

tar -xzf pyserial-2.6.tar.gz
cd pyserial-2.6
sudo python setup.py install

I confirmed pyserial was functioning by creating serialTst.py

import time
import serial

ser = serial.Serial('/dev/tty.usbmodemfa13131', 57600)

while True:
    message = ser.readline()
    print(message)
    time.sleep(0.5)

and it echoed output being written by the Arduino with Serial.println.

matplotlib

[The packaged matplotlib doesn't seem to work with the Apple release of Python. (matplotlib-1.2.1-py2.7-python.org-macosx10.6.dmg) from http://matplotlib.org/downloads.html gives an error saying "matplotlib 1.2.1 can't be installed on this disk. matplotlib requires System Python 2.7 to install.]

Following http://penandpants.com/2012/02/24/install-python/ (or maybe better http://penandpants.com/install-python/ and some of http://www.tapir.caltech.edu/~dtsang/python.html) provided alternatives after installing the xcode command line tools:

ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
brew doctor

brew install pkg-config
brew install freetype
brew install libpng

sudo easy_install pip
sudo pip install matplotlib

The matplotlib install produced a huge number of warnings, but reported successful installation at the end.

wxPython

Install wxPython (wxPython2.8-osx-unicode-py2.7) from http://wxpython.org/download.php. To get the install to work you need to allow installs from anywhere in system settings. see http://trac.wxwidgets.org/ticket/14523 and http://www.toolfarm.com/blog/entry/installing_osx_10.8_mountain_lion_please_read

Continuing

After all that installing, serialTst.py still works. Got the Arduino running Kevin's ArduinoDAQ on a fresh port tty.usbmodemfa13141. Changed the port in daq.py  and ran, but it fails on loading wxPython 2.8 with no 64 bit environment. wxPython 2.9 claims to be OK with both 32 and 64 bit, so I installed it and then python daq.py runs!

It generates repeating warnings if the Arduino environment is still running, but quitting it and rerunning python daq.py works great.