Raspberry Pi Home Datalogger

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Raspberry Pi Home Datalogger

Post by static »

Here's the code that I'm going to use for the TMP102:

Code: Select all

#!/usr/bin/python

import time
from Adafruit_I2C import Adafruit_I2C

class TMP102 :
  i2c = None


  # Constructor
  def __init__(self, address=0x48, mode=1, debug=False):
    self.i2c = Adafruit_I2C(address)

    self.address = address
    self.debug = debug
    # Make sure the specified mode is in the appropriate range
    if ((mode < 0) | (mode > 3)):
      if (self.debug):
        print "Invalid Mode: Using STANDARD by default"
      self.mode = self.__BMP085_STANDARD
    else:
      self.mode = mode



  def readRawTemp(self):
    "Reads the raw (uncompensated) temperature from the sensor"
    raw = self.i2c.readU16(0x48)     #The TMP102 returns 12-bits, I think
    if (self.debug):
      print "DBG: Raw Temp: 0x%04X (%d)" % (raw & 0xFFFF, raw)
    return raw


  def readTemperature(self):
    "Gets the compensated temperature in degrees celcius"

    RawBytes = self.readRawTemp()  #get the temp from readRawTemp (above)

    temp =float(RawBytes) * (float(0.0625) / 16.0) # dividing by 16 accomplishes the bit-shift
    if (self.debug):
      print "DBG: Calibrated temperature = %f C" % temp
    
    
    return temp

You can see that it is still based off of the code for the BMP085.

I don't know if we have anyone that is really good at stylistic issues. I'm probably going to stick with one way and just consistently write that way, but:
Stylistically, should I create a 12-bit read entry in the I2C library, or stick with my current work which creates a new class to handle the behind the scenes math?
Is there a way that is more expected? My goal is to have some hardware and software that anyone can use easily. I know that I'm stuck too far into the weeds to see what is easy to use.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Raspberry Pi Home Datalogger

Post by adafruit_support_rick »

Static wrote:I think that's it!
Cool
Static wrote:Stylistically, should I create a 12-bit read entry in the I2C library, or stick with my current work which creates a new class to handle the behind the scenes math?
Is there a way that is more expected? My goal is to have some hardware and software that anyone can use easily.
Stylistically, when you create a class, you first assume that the user needs/wants to know absolutely nothing about what goes on inside the class. In this case, the user wants to know the temperature, period.

To be convenient, the class should offer the option of getting the temp in either F or C, and the method names should be clear about what they return. So, rather than "readTemperature", you want "readFahrenheit" and "readCelsius".

Of course, some users might want to know a little bit more, and that's where you add methods like readRawTemp. I'm afraid I don't know much about python, but I presume that it provides various integer data types. In your method, you don't explicitly type 'raw', but I assume that self.i2c.readU16 provides an implicit type declaration of uint16. Your class method should explicitly propagate that type. I don't know the syntax, but you want to include an explicit type definition of uint16 in def readRawTemp(self)

That goes for "readFahrenheit" and "readCelsius" as well. You always want to explicitly type your returns.

You've seen an example of why you need to know things like this. When you did your four-bit right-shift of the raw 12-bit reading, you were losing the sign information. if raw was typed as a signed integer, then python would have done an arithmetic shift, as tldr pointed out, and the sign would have been preserved.

But python implicitly typed raw as an unsigned integer, based on the return from readU16. The right shift then was done as a logical (bitwise) shift, and so the sign bit was not preserved. When you converted it to a float, it was as a positive unsigned integer instead of a (possibly negative) signed integer.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Raspberry Pi Home Datalogger

Post by static »

To be convenient, the class should offer the option of getting the temp in either F or C, and the method names should be clear about what they return. So, rather than "readTemperature", you want "readFahrenheit" and "readCelsius".
This is one of my next steps. I could probably be doing it now (Shouldn't take more than a few minutes), but instead I'm going to document a bit.

I changed the exterior sensor housing. I was getting a massive greenhouse effect from sealing the sensor in a tupperware. Today while cleaning the kitchen, I had a bit of an epiphany.

I took an empty lemon juice bottle Image, that one, and cleaned it out. Then I cut the bottom off of it. Finally I threaded some CAT-5 cable through the opening up top and created a CAT-5 cable passing through the lid. I kept the distance shorter than I had used originally, so the current cable is only 1.5 meters. I then affixed my RJ-45 breakout board/jack/sensor up in the neck of the bottle, but let it dangle freely. I then sealed the top with some electrical tape. I took the resulting construct and dangled it out the window. Now the sensor is protected by a skirt of plastic. The plastic is open at the bottom which should allow for an isothermic environment.

I also played around a little more with matplotlib. I got some feedback from a friend that the graphs looked pretty, but they didn't actually tell you anything (label your work!). So I put a legend, some axis information and a graph title one there.

I've had the polling program running for a little under a week now (over 9100 records). The sensor log software is still churning away pretty rapidly. With the last couple of re-writes to the code I don't expect that to change, except for the limited memory of the Pi itself. The sensor log file is now between 460kb and 3.7MB. I'm guessing that the lower number is the actual bit size of the file, while the upper number has to do with block allocation (how much room the file takes up in memory). Educated guess.
Important to note: The file is larger than the memory size of the Pi (250MB, I believe). Nothing is crashing, so far.

Thank you two for the help!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Raspberry Pi Home Datalogger

Post by adafruit_support_rick »

Static wrote:Image
Hey, I've worked on a few projects like this! :lol:

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Raspberry Pi Home Datalogger

Post by static »

I'm still getting a fair amount of greenhouse effect. I'm not completely sure what my next step is. I'm reading an approximate 10c temperature increase.

In other news, I found another Raspberry Pi for sale. I just ordered that. I'll be working on a second node soon. I think this one will be a front door monitor. I'd like to figure out how to incorporate a webcam with it in addition to the environmental sensors.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Raspberry Pi Home Datalogger

Post by adafruit_support_rick »

Static wrote:I'm still getting a fair amount of greenhouse effect.
That would because your enclosure is closed at the top, so heat will remain trapped inside of it, rather than convecting out the bottom. You need some sort of double-cover, like the rain-fly on a tent, that will let air convect out the top of your lemon bottle, but stop rain from coming in.

What about simply sealing the TMP102 in heat shrink, or potting it in silicone or something like that? Use a rubber balloon or ziploc bag or some other such thing for the BMP085.

User avatar
tastewar
 
Posts: 408
Joined: Thu Mar 17, 2011 10:16 am

Re: Raspberry Pi Home Datalogger

Post by tastewar »

Ideally, you want a "Stevenson Screen" type enclosure:

http://en.wikipedia.org/wiki/Stevenson_screen

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Raspberry Pi Home Datalogger

Post by static »

I just took a break from work. I thought I was going to be able to report something really cool. Instead, I'm having massive spikes in the temperature of the TMP102. This is the thermometer that I put outside in the new enclosure. I'll try to get a copy of the graph, but it looks like the minute the temperature dropped below 0c, it spiked. It's currently reporting the temperature at 254.375c. It's about -1c to 0c right now.
The code in the posts above is the current code. That looked good, but I think I'm still missing something.

That's the exact enclosure I need to build. I want to include a light sensor as well (it's coming, I just sent it to the wrong address), but I can make a subassembly with a clear window for that. Thanks for posting it. I was mulling it over and trying to get there, but not getting there.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Raspberry Pi Home Datalogger

Post by static »

Sun's up, temp is 4c here, and the datalogger is reading correctly, now. I'm missing something in the datasheet. I'm going to pour over it as soon as I get a chance.
Right now is not my chance. ER killed me last night. I need a nap. I'm going to pull the datasheet over to my phone and glance at it when I can.

User avatar
tastewar
 
Posts: 408
Joined: Thu Mar 17, 2011 10:16 am

Re: Raspberry Pi Home Datalogger

Post by tastewar »

Almost sounds like a signed/unsigned issue...

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Raspberry Pi Home Datalogger

Post by adafruit_support_rick »

It is a signed/unsigned issue. It's happening on the conversion from uint16 to float. You're going to have to explicitly cast your raw reading to a signed integer before you convert it to float.

Probably want to do it here:

Code: Select all

    RawBytes = int16(self.readRawTemp())  #get the temp from readRawTemp (above), and cast to a signed integer.
I don't know what the python type name for signed 16-bit integer is. Maybe 'short'? Maybe 'int16'? Gonna have to learn python one of these days ...

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Raspberry Pi Home Datalogger

Post by static »

Went to bed, had a dream about this (including the function I needed to run from the Adafruit I2C module), came to say this, found verification/validation.
Step 4, profit?
Dunno.

I swapped out reading an unsigned bit for a signed bit.
That's the nice thing about Python. It's fairly English in it legibility. If the code as commented at all, it tends to be pretty easy to figure out.

Driverblock and Tastewar, thanks for posting up. I might not have hit this tonight.

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Raspberry Pi Home Datalogger

Post by static »

Great night at work, but I was able to check the website a few times while waiting for results.

It works! A little after midnight we dropped below zero. It looks like last night got to -2.75c or so

Thanks for the help gang. I gotta rack out. Two more nights. Hope everyone drives safe and stays healthy!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Raspberry Pi Home Datalogger

Post by adafruit_support_rick »

Great! Don't freeze. :wink:

User avatar
static
 
Posts: 188
Joined: Thu Dec 23, 2010 6:21 pm

Re: Raspberry Pi Home Datalogger

Post by static »

Merry Christmas to all!

My family heard me whining about Raspberry Pi's in August. They bought me one for Christmas. It's one of the new Model B's with 512MB of memory.

So far, it seems a little peppier. Nothing I can put my finger on, yet (Though this webpage is loading faster).
I do need to go in and re-write the code that polls the I2C bus. I think I'm going to try to be a real programmer and use the Python exceptions to check either I2C bus.
Now that I write it, I realize that may not be a good idea. If there are two buses, then someone could use both.

I need to wait until next week's paycheck in order to buy some more I2C sensors. I think I have enough of my "Bus Remotes" to outfit the second node. I should probably also look at posting all of the code up, somewhere.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”