From c11b5bf2bf327155a3e32bdc4f3feba2126fc120 Mon Sep 17 00:00:00 2001 From: Mickael Date: Mon, 14 Sep 2020 12:43:26 +0200 Subject: [PATCH] add debug mode --- GreenPonik_Thermistor10k/Thermistor10k.py | 39 ++++++++++++++--------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/GreenPonik_Thermistor10k/Thermistor10k.py b/GreenPonik_Thermistor10k/Thermistor10k.py index e6ead48..962fb94 100644 --- a/GreenPonik_Thermistor10k/Thermistor10k.py +++ b/GreenPonik_Thermistor10k/Thermistor10k.py @@ -25,6 +25,7 @@ class Thermistor10k: def __init__(self, bus=DEFAULT_BUS, addr=DEFAULT_ADDR): self._bus = bus self._addr = addr + self._debug @property def bus(self): @@ -34,6 +35,14 @@ def bus(self): def address(self): return self._addr + @property + def debug(self): + return self._debug + + @debug.setter + def debug(self, d): + self._debug = d + def read_temp(self): """ @brief Read thermistor 10k temperature on raspberry pi i2c bus @@ -47,15 +56,14 @@ def read_temp(self): ref_current = 0.0001 """ The ADS1015 and ADS1115 both have the same gain options. - # - # GAIN RANGE (V) - # ---- --------- - # 2/3 +/- 6.144 - # 1 +/- 4.096 - # 2 +/- 2.048 - # 4 +/- 1.024 - # 8 +/- 0.512 - # 16 +/- 0.256 + GAIN RANGE (V) + ---- --------- + 2/3 +/- 6.144 + 1 +/- 4.096 + 2 +/- 2.048 + 4 +/- 1.024 + 8 +/- 0.512 + 16 +/- 0.256 """ gains = (2 / 3, 1, 2, 4, 8, 16) # Create the ADS object @@ -67,16 +75,12 @@ def read_temp(self): address=self._addr, ) adc2 = AnalogIn(ads, ADS.P2) - print("adc2 analog: %.3f" % adc2.value) voltage = adc2.value * (device_vcc / 65535) - print("voltage: %.3f" % voltage) # Using Ohm's Law to calculate resistance of thermistor resistance = voltage / ref_current - print("resistance: %.3f" % resistance) # Log of the ratio of thermistor resistance # and resistance at 25 deg. C ln = math.log(resistance / thermistor_25) - print("ln: %.3f" % ln) # Using the Steinhart-Hart Thermistor # Equation to calculate temp in K kelvin = ( @@ -84,9 +88,14 @@ def read_temp(self): + (0.0000021400943 * ln ** 2) + (-0.000000072405219 * ln ** 3) ) - print("kelvin: %.3f" % kelvin) temp = kelvin - 273.15 # Converting Kelvin to Celcius - print("temperature: %.3f" % (temp)) + if self._debug: + print("adc2 analog: %.3f" % adc2.value) + print("voltage: %.3f" % voltage) + print("resistance: %.3f" % resistance) + print("ln: %.3f" % ln) + print("kelvin: %.3f" % kelvin) + print("Thermistor 10k temperature: %.3f" % (temp)) return temp except Exception as e: print("cannot read water temperature, An exception occurred: {}".format(e))