Author Options:

temperature logging with raspberry pi Answered

new here, and to Pi/Python

looking to log data from a few different sensors from Atlas Scientific (RTD & PH).  I've gotten really stuck so fare in getting these going and was wondering if anyone here can help me out.  so far I've used this code: 

import smbus
import time
import datetime

#SMBus(0) - Raspberry Pi Model A
#SMBus(1) - Raspberry Pi Model B

bus = smbus.SMBus(1)

#I2C address of sensor
address = 0x66

def temperature():
    rvalue0 = bus.read_word_data(address,0)
    rvalue1 = (rvalue0 & 0xff00) >> 8
    rvalue2 = rvalue0 & 0x00ff
    rvalue = (((rvalue2 * 256) + rvalue1) >> 4 ) *.0625
    #print rvalue1, rvalue2
    return rvalue

print("Temperature Data Logger\n")

while True:

    #Open Log File
    now = datetime.datetime.now()
    timestamp = now.strftime("%Y/%m/%d %H:%M")
    outvalue = temperature()
    outstring = str(timestamp)+"  "+str(outvalue)+" C "+str(outvalue*1.8+32)+" F"+"\n"
    print outstring

    #log temperature every 10 seconds

I've got the two sensor boards connected to the raspberry pi 3 mounted via a Whitebox Tentacle T3.   The boards are in i2c mode and i can see them using sudo i2cdetect -y 1 (they show up address 63 and 66)  but when i run this program above I just get a red error light when it reads the temperature.   The log file reports a measurement of 2C and 35.2F which is not correct but the red light on the board tells me that the Pi is trying to connect with it.   Anyone able to help?


The forums are retiring in 2021 and are now closed for new topics and comments.