Sunday, December 16, 2012

Integrating the TMP102 sensor with the datalogger, and I2C bus lengths

The TMP102 is an I2C sensor available on a breakout board from Sparkfun TMP102.  It's pretty cheap, under $10.  It will actually allow you to have multiple sensors on the same I2C bus just by jumpering an address pin.

I'm working on creating a software module to decode the output from the sensor.  So far it isn't straight forward, at least for an amateur.  If I understand correctly, the sensor reports the readings in a backwards fashion.  I'm not sure I've decoded the output correctly, but I'm close.  Once I get that ironed out, I'll post the code up.

I started playing with I2C bus lengths.  I heard that the practical length was less than a meter.  I've got around a 2-3 meter CAT-5 cable set up running the TMP102.  It's dangling outside the window, inside a tupperware container, and appears to be working just fine.  I'm using Sparkfun RJ-45 Jacks and breakout boards.  It adds a couple of bucks to the cost of the project, but it makes for great modularity.  It's also going to let me do some cable run tests.  Eventually I'll post those up as well.

I modified the jacks and breakout boards slightly.  In place of a simple header, I used Arduino 8-pin stackable headers.  That way, I can easily add a second sensor (or more) at the cable end, while still being able to easily breadboard it.  So far it's working great.

The goal with this setup is to have two sets of environmental sensors.  One on the Raspberry Pi itself, and the other just slightly remote from the Raspberry Pi.  This sensor set could be outside a window, inside a terrarium/fish tank, inside a science project (fermenter? soil analyzer? hot water heater?), all kinds of possibilities.

If I can make the software scale-able, anyone could add all kinds of sensors to the system and have it easily recordable.  The only drawback would be writing new sensor drivers for each set of sensors.  That's where I'm having a fair amount of difficulty now.

It looks like I was wrong when I figured out the size of the data files.  Right now, the datafiles appear to be much smaller, despite my cramming more information into them (Temp1, Temp2, Pressure, Motion Sensor, Time, Log Level).  I think I want to change the logging system even further so that it creates a better record of values.  Right now I'm saving a string for each sensor cycle with just the sensor values.  The software is written so that it assumes a certain value is in a certain order in the string.  That's great, as long as no one else is adding sensors to the system, and you only look a certain way.

I'm wondering if I can create a log with each sensor cycle recording a dictionary.  Then I could easily write code to examine which sensors were polled for each sensor cycle.  I could easily pull all the sensor values, or some of the sensor values.  I could have some sensors polled more often than others.

I need to grab a second Raspberry Pi, and start thinking about how to pull all the sensors nodes together, into one easy to read display.


Tom Cox said...

This doesn't belong on this blog, but I don't know how else to reach you. I've seen some of your posts elsewhere, and think you may know something that I am trying to discover. The smbus module that I have found does not seem to be compatible with Python 3. Is there a version of smbus that IS?

Tom Cox

Static said...

That's a good question. I seem to recall having a problem with SMBus when I didn't use Adafruit's Occidentalis distro. I played with it for a bit, but ultimately just installed her linux flavor because everything just seemed to work. They (Adafruit) call it a very preliminary beta, but so far it's been incredibly steady.

Tom Cox said...

I'm trying to use theTSL2561 Luminosity Sensor with a Rasp. Pi, as you did, and am using Occidentalis. But when I try to run your programs, they complain about missing smbus. I saw a suggestion that the smbus module was for Python version 2. Have not found one for version 3. You are writing it partly capitalized: SMBus, which must be different somehow.

Static said...

Terribly sorry that I didn't get back sooner. I don't think that I'm using a different version of smbus. I capitalized it out of habit, not because it is different.

I'm trying to think of what I did differently. Did you update your Raspberry Pi installation?

Did you update the smbus installation?
(sudo apt-cache search smbus)
(sudo apt-get install smbus)

Those are the only ones I can think of off the top of my head. Let me know if they work. I'll keep looking.

Tom Cox said...

sudo apt-cache search smbus produces one result: python-smbus.

sudo apt-get install smbus produces "Unable to locate package smbus."

sudo apt-get install python-smbus indicates that it is already present, probably from my previous fumbling.

Your code says "import smbus." IDLE 3 (Python 3) reports "No such module." IDLE (Python 2) finds smbus, but complains two lines later that it can't find and import the module Adafruit_I2C.

I think that is because I don't know where to put that module, and have put it in the wrong place, where Python can't find it.

Static said...

I'm at work right now, but I'm going to take a glance at this in the morning, and pack my laptop to take with me tomorrow. I feel like I ran into this problem, but I can't remember how I fixed it. I'm going to read over my notes and possibly ask for help on one of the boards.

Static said...

Tom, are you using a "sh-bang" at the top of your code?

I'm not 100% sure what this does, but I remember reading that it is important. I think it "points" Python to the correct directory.

Any chance you can post your code, and I can try it on my machine?

Tom Cox said...

The code (which is yours) is here:

Your posting is dated Jan 30, 2013 1:20 pm

The line "import smbus" causes Python 3 to complain, "No such module."

Static said...
Try this forum post, on this page.

That may be what I used. There was a period where I was pretty sleep deprived, and my notes aren't of much help.
Let me know. If it doesn't work, I'll keep looking and chasing things down.

Tom Cox said...

I find no post by you an the page at that URL.

Static said...

Oh, no. Not my post. The post by catmaker on Dec 8th.

I think the thread may be able to help. I looked at:
and was dismayed to see that you had already seen that thread.

Tom Cox said...

Thanks again. Yes, I had seen this. Right now, I'm trying to master RPi, Linux, and Python, and complete the device I'm working on. I had hoped to put off learning how to compile C progams, and studying the the TSL2561 datasheets, but it looks like there is no plug-and-play option, so I'll plunge in.