i have two tmp102 sensors wired in parallel on I2C bus on 0x48 and 0x49 addresses. When i read the values and convert them to Celsius sometimes it seems to show the correct values sometimes not. I guess that i`m doing something wrong here. This is my code:
Code: Select all
public class TempSensor {
public static void main(String[] args) throws Exception {
I2CBus i2cBus = I2CFactory.getInstance(I2CBus.BUS_1);
for (int i=0;i<1000;i++){
I2CDevice insideSensor = i2cBus.getDevice(0x48);
I2CDevice outsideSensor = i2cBus.getDevice(0x49);
byte[] bufferInside = new byte[2];
byte[] bufferOutside = new byte[2];
int amountInside = insideSensor.read(bufferInside,0,2);
int amountOutside = outsideSensor.read(bufferOutside,0,2);
Integer msbIn = bufferInside[0]<<4;
Integer lsbIn = bufferInside[1]>>4;
Integer tIn = msbIn | lsbIn;
System.out.println("IN : "+tIn*0.0625);
Integer msbOut = bufferOutside[0]<<4;
Integer lsbOut = bufferOutside[1]>>4;
Integer tOut = msbOut | lsbOut;
System.out.println("Out : "+tOut*0.0625);
Thread.sleep(10000);
tIn = null;
tOut = null;
insideSensor = null;
outsideSensor = null;
}
}
}
java -classpath .:classes:/opt/pi4j/lib/'*' TempSensor
Jan 26, 2013 10:40:28 PM com.pi4j.util.NativeLibraryLoader load
INFO: Library [pi4j] loaded successfully using embedded resource file: [jar:file:/opt/pi4j/lib/pi4j-core.jar!/lib/hard-float/libpi4j.so]
IN : 24.4375
Out : 21.4375
IN : 24.375
Out : 21.4375
IN : 24.375
Out : 21.375
IN : -0.125
Out : 21.3125
IN : 26.3125
Out : 21.375
IN : 27.1875
Out : 21.375
IN : -0.1875
Out : 21.4375
IN : 28.3125
Out : 21.4375
So as you can see the value differs, without moving the sensors.
Any idea ?