diff --git a/ds18b20.py b/ds18b20.py index 5c4e33b..f80cf39 100755 --- a/ds18b20.py +++ b/ds18b20.py @@ -41,8 +41,17 @@ def read_probe(): probe = open("/sys/bus/w1/devices/%s/w1_slave" % DS18B20_ID) - temp = float(probe.readlines()[-1].split()[-1].split("=")[-1])/1000 # Parse probe output - globals.CURRENT_TEMP = int(round((temp * 9/5) +32)) # Convert C-F and round into an integer + temp = float(probe.readlines()[-1].split()[-1].split("=")[-1])/1000 # Parse probe output + temp = int(round((temp * 9/5) +32)) # Convert C-F and round into an integer + + # 1-wire interfaces (like the DS18B20 uses) can occasionally + # return wildly wrong results. For this reason, we throw away + # values that have changed more than 10 degrees since the last + # reading, since that's almost certainly noise. + + if abs(temp - globals.CURRENT_TEMP) <= 10: + globals.CURRENT_TEMP = temp + probe.close()