Thanks Again. Completely understand. I googled and came across the same plus a few others in Python a few days ago and threw it into our monitoring system. But the maths seems to be subtlety different and I am thinking it is to do with trying to normalise the result or take out some rounding issues.The results I am getting come in an obvious pattern and I am now trying to establish if the A/C in teh server room is really fluctuating like this or if the Pi is heating up as the cpu cycles or if the maths are not correct. I am taking 5 samples 1 second apart every 30 minutes and getting the average and then plotting it. So I am not sure why I get this cyclic pattern. Of course the A/C could be going up and down but since it is a closed room I am suspicious that it is something else. Beyond all of that I am always keen to understand the logic and in this case I am half there.
Here is the Python script that I garnished from the web and then cleaned it up and put my own logic around the sampling as well as customising the output for the PRTG monitoring API.
#!/usr/bin python
import time
import numpy as np
import smbus
****************************************************************************
INITIALISATION
****************************************************************************
OK=0
WARN=1
ERR=2
BUS_ID = 1
REGISTER_ID = 0x48
TIMEX = 100
TEMP_MIN = 10
TEMP_MAX = 40
TICK = 15
ADJUSTMENT = 8.5
MAX_SAMPLES = 5
DELAY = 1
bus = smbus.SMBus(BUS_ID)
i = 0
result = 0
sample = 0
****************************************************************************
MAINLINE
Probably should get the average of several readings to flatten out
what appears to be spikes in temperature on regular cpu cycles
****************************************************************************
for i in range(0, MAX_SAMPLES):
data = bus.read_i2c_block_data(REGISTER_ID, 0)
dataMSB = data[0]
dataLSB = data[1]
sample = (((dataMSB << 8) | dataLSB) >>7) * 0.5
# Check for negative temperature
if sample > 125:
sample =(((((dataMSB << 8) | dataLSB) >>7) * 0.5) -256)
# Adjust result to cater for estimated direct rack and pi heat
sample = sample - ADJUSTMENT
result = result + sample
time.sleep(DELAY)
Calculate Average
result = result / MAX_SAMPLES
Send Result to STDOUT
print “%s:%s:Current Temperature” % (OK,result)
Cheers and thanks again