From 10ce8a5d8753d6afe9309b1b0bea9c8df9ff699a Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 20:57:17 +0000 Subject: [PATCH 01/11] Initial Thermistor setup --- pitmaster/tools/sensor.py | 53 +++++++++++++++++++++++++++++++++++++++ pitmaster/tools/temps.py | 20 ++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/pitmaster/tools/sensor.py b/pitmaster/tools/sensor.py index 5bc74c2..df6eef3 100644 --- a/pitmaster/tools/sensor.py +++ b/pitmaster/tools/sensor.py @@ -13,9 +13,24 @@ # limitations under the License. import os import time +import RPi.GPIO as GPIO from pitmaster.exceptions import * +# For the thermistor: +# change these as desired - they're the pins connected from the +# SPI port on the ADC to the Cobbler +SPICLK = 18 +SPIMISO = 23 +SPIMOSI = 24 +SPICS = 25 + +# set up the SPI interface pins +GPIO.setup(SPIMOSI, GPIO.OUT) +GPIO.setup(SPIMISO, GPIO.IN) +GPIO.setup(SPICLK, GPIO.OUT) +GPIO.setup(SPICS, GPIO.OUT) + def _temp_raw(sensor=None): with open(sensor, "r") as file_reader: @@ -62,6 +77,44 @@ def find_temp_sensors(): } ] + +def read_thermistor(adcnum=None, offset=none, clockpin, mosipin, misopin, cspin): + if ((adcnum > 7) or (adcnum < 0)): + return -1 + + GPIO.output(cspin, True) + GPIO.output(clockpin, False) # start clock low + GPIO.output(cspin, False) # bring CS low + + commandout = adcnum + commandout |= 0x18 # start bit + single-ended bit + commandout <<= 3 # we only need to send 5 bits here + for i in range(5): + if (commandout & 0x80): + GPIO.output(mosipin, True) + else: + GPIO.output(mosipin, False) + commandout <<= 1 + GPIO.output(clockpin, True) + GPIO.output(clockpin, False) + + adcout = 0 + # read in one empty bit, one null bit and 10 ADC bits + for i in range(12): + GPIO.output(clockpin, True) + GPIO.output(clockpin, False) + adcout <<= 1 + if (GPIO.input(misopin)): + adcout |= 0x1 + + GPIO.output(cspin, True) + + adcout >>= 1 # first bit is 'null' so drop it + + return temps.resistance_to_temp(temps.convert_to_resistance(adcout)) + + + if __name__ == "__main__": for i in range(15): test = read_temp(sensor="/sys/bus/w1/devices/3b-0000001921e8/w1_slave") diff --git a/pitmaster/tools/temps.py b/pitmaster/tools/temps.py index 33a9a66..4e1ea65 100644 --- a/pitmaster/tools/temps.py +++ b/pitmaster/tools/temps.py @@ -12,10 +12,28 @@ # See the License for the specific language governing permissions and # limitations under the License. from numbers import Number +import math from pitmaster.exceptions import * +def convert_to_resistance(reading=None): + r1 = (1023.0 / reading) - 1.0 + resistance = resistor_size / r1 + return resistance + + +def resistance_to_temp(resistance=None): + if resistance is None: + raise MissingPropertyException("resistance can not be None!") + t = sth_coef_a + t += sth_coef_b * (math.log(resistance)) + t += sth_coef_c * math.pow((math.log(resistance)), 3) + t = 1 / t + t -= 273.15 + return t + + def from_c_to_f(temp=None): """ Converts a given temp from Celsius to fahrenheit @@ -29,4 +47,4 @@ def from_c_to_f(temp=None): raise InvalidPropertyException( "temp must be a valid number. Found: {}".format(type(temp)) ) - return (temp * 9/5) + 32.0 + return (temp * 9 / 5) + 32.0 From df5194904c007058a08591cdd804be5a253c5b6c Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 21:34:05 +0000 Subject: [PATCH 02/11] removed datadir --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8ce7ab9..edeb66a 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,4 @@ docs/_build/ target/ .idea/ +pitmaster/csv/ From 45d9a2d856779ea26cf08e49b81409ecaf5a7e97 Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 21:34:25 +0000 Subject: [PATCH 03/11] updates for thermistor, and local dir --- pitmaster/executable.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/pitmaster/executable.py b/pitmaster/executable.py index 2c36aac..86af2e7 100644 --- a/pitmaster/executable.py +++ b/pitmaster/executable.py @@ -55,6 +55,20 @@ def _setup_arg_parser(): action='store_true', help="Enable the LCD display or not. It is disabled by default." ) + parser.add_argument( + "-l", + "--localdisplay", + action='store_true', + help="Enable console output or not. It is disabled by default." + ) + parser.add_argument( + "-p", + "--probetype", + required=True, + action='store', + help="Configure which type of probes to use." + "This is either thermistor or thermocouple" + ) return parser @@ -69,7 +83,12 @@ def execute(): cook_name = provided_args.cookname output_file = provided_args.output use_lcd = provided_args.tft - sensors = sensor.find_temp_sensors() + probe_type = provided_args.probetype + local_display = provided_args.localdisplay + if str(probe_type).lower == "thermistor": + sensors = range(0, 2) + else: + sensors = sensor.find_temp_sensors() data_obj = DBObject(filename=output_file) if use_lcd: display = LocalDisplay() @@ -77,7 +96,11 @@ def execute(): try: while True: for sen in sensors: - temp_c = sensor.read_temp(sen["location"]) + if probe_type == "thermistor": + temp_c = sensor.read_thermistor(adcnum=sen) + else: + temp_c = sensor.read_temp(sen["location"]) + temp_f = temps.from_c_to_f(temp=temp_c) info = { "date": time.time(), @@ -87,6 +110,8 @@ def execute(): "cook_name": cook_name } data_obj.save(info=info) + if local_display: + print info if use_lcd: display.check_events() display.set_display_msg("{}: {}f".format( From 9cac80fcc6e72d173b974939d44a63e3a7e2c952 Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 21:34:52 +0000 Subject: [PATCH 04/11] beginning thermistor work --- pitmaster/tools/sensor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pitmaster/tools/sensor.py b/pitmaster/tools/sensor.py index df6eef3..8cb2aee 100644 --- a/pitmaster/tools/sensor.py +++ b/pitmaster/tools/sensor.py @@ -25,6 +25,9 @@ SPIMOSI = 24 SPICS = 25 +GPIO.setmode(GPIO.BCM) +DEBUG = 1 + # set up the SPI interface pins GPIO.setup(SPIMOSI, GPIO.OUT) GPIO.setup(SPIMISO, GPIO.IN) @@ -78,7 +81,7 @@ def find_temp_sensors(): ] -def read_thermistor(adcnum=None, offset=none, clockpin, mosipin, misopin, cspin): +def read_thermistor(clockpin=SPICLK, mosipin=SPIMOSI, misopin=SPIMISO, cspin=SPICS, adcnum=None, offset=None): if ((adcnum > 7) or (adcnum < 0)): return -1 From 9bb0db89ace2fb222a40b7a9fabd055b8f7e886b Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 21:56:39 +0000 Subject: [PATCH 05/11] moved clumsy ifs into better places --- pitmaster/tools/sensor.py | 63 +++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/pitmaster/tools/sensor.py b/pitmaster/tools/sensor.py index 8cb2aee..dcedf09 100644 --- a/pitmaster/tools/sensor.py +++ b/pitmaster/tools/sensor.py @@ -41,7 +41,7 @@ def _temp_raw(sensor=None): return lines -def read_temp(sensor=None, offset=None): +def read_temp(sensor=None, offset=None, probe_type=None): """ Reads the temp sensor and returns its temp in C. @@ -51,34 +51,59 @@ def read_temp(sensor=None, offset=None): """ if sensor is None: raise MissingPropertyException("sensor must not be None!") + if probe_type is None: + raise MissingPropertyException("must specify probe type!") if offset is None: offset = 0 - if not os.path.isfile(sensor): - raise SensorNotFoundException("Unable to locate: {}".format(sensor)) - lines = _temp_raw(sensor) - while lines[0].strip()[-3] != 'Y': - time.sleep(0.2) - lines = _temp_raw() - temp_output = lines[1].find('t=') - if temp_output != -1: - temp_string = lines[1].strip()[temp_output + 2:] - temp_c = (float(temp_string) / 1000.0) + offset + if probe_type != "thermistor": + if not os.path.isfile(sensor): + raise SensorNotFoundException("Unable to locate: {}".format(sensor)) + lines = _temp_raw(sensor) + while lines[0].strip()[-3] != 'Y': + time.sleep(0.2) + lines = _temp_raw() + temp_output = lines[1].find('t=') + if temp_output != -1: + temp_string = lines[1].strip()[temp_output + 2:] + temp_c = (float(temp_string) / 1000.0) + offset + return temp_c + else: + temp_c = read_thermistor(cspin=sensor) return temp_c -def find_temp_sensors(): +def find_temp_sensors(probe_type=None): """ Looks on system for temp sensors and returns all that it finds in a list :return list: List containing all sensors. """ - # Hard coded for now while we work out the best way to handel this. - return [ - { - "name": "Probe 1", - "location": "/sys/bus/w1/devices/3b-0000001921e8/w1_slave" - } - ] + if probe_type is None: + raise MissingPropertyException("probe type must be set!") + + if probe_type == "thermistor": + return [ + { + "name": "Probe 1", + "location": "0" + }, + { + "name": "Probe 2", + "location": "1" + }, + { + "name": "Probe 3", + "location": "2" + } + ] + else: + # Hard coded for now while we work out the best way to handel this. + return [ + { + "name": "Probe 1", + "location": "/sys/bus/w1/devices/3b-0000001921e8/w1_slave" + } + ] def read_thermistor(clockpin=SPICLK, mosipin=SPIMOSI, misopin=SPIMISO, cspin=SPICS, adcnum=None, offset=None): From 58af466635b335a250584b87bde076d8828bd556 Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 21:57:02 +0000 Subject: [PATCH 06/11] we can read the things now --- pitmaster/executable.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/pitmaster/executable.py b/pitmaster/executable.py index 86af2e7..ccc7448 100644 --- a/pitmaster/executable.py +++ b/pitmaster/executable.py @@ -85,10 +85,7 @@ def execute(): use_lcd = provided_args.tft probe_type = provided_args.probetype local_display = provided_args.localdisplay - if str(probe_type).lower == "thermistor": - sensors = range(0, 2) - else: - sensors = sensor.find_temp_sensors() + sensors = sensor.find_temp_sensors(probe_type) data_obj = DBObject(filename=output_file) if use_lcd: display = LocalDisplay() @@ -96,11 +93,7 @@ def execute(): try: while True: for sen in sensors: - if probe_type == "thermistor": - temp_c = sensor.read_thermistor(adcnum=sen) - else: - temp_c = sensor.read_temp(sen["location"]) - + temp_c = sensor.read_temp(sen["location"], probe_type=probe_type) temp_f = temps.from_c_to_f(temp=temp_c) info = { "date": time.time(), From d3913e6c3b8e99cdf167b850b0ed316bd21e5005 Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 22:04:38 +0000 Subject: [PATCH 07/11] clean up resistance to temp conversions --- pitmaster/tools/sensor.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pitmaster/tools/sensor.py b/pitmaster/tools/sensor.py index dcedf09..0bde89c 100644 --- a/pitmaster/tools/sensor.py +++ b/pitmaster/tools/sensor.py @@ -34,6 +34,11 @@ GPIO.setup(SPICLK, GPIO.OUT) GPIO.setup(SPICS, GPIO.OUT) +resistor_size = 10000 +sth_coef_a = 0.000436925136556 +sth_coef_b = 0.000230203788274 +sth_coef_c = 0.000000060486575 + def _temp_raw(sensor=None): with open(sensor, "r") as file_reader: @@ -138,9 +143,9 @@ def read_thermistor(clockpin=SPICLK, mosipin=SPIMOSI, misopin=SPIMISO, cspin=SPI GPIO.output(cspin, True) adcout >>= 1 # first bit is 'null' so drop it - - return temps.resistance_to_temp(temps.convert_to_resistance(adcout)) - + resistance = temps.convert_to_resistance(adcout) + temp_c = temps.resistance_to_temp(resistance) + return temp_c if __name__ == "__main__": From f31850db66826a1556c21f7c740f059c8b8e3e9c Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 22:09:21 +0000 Subject: [PATCH 08/11] moved the math bits to the right plae --- pitmaster/tools/sensor.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pitmaster/tools/sensor.py b/pitmaster/tools/sensor.py index 0bde89c..c476a39 100644 --- a/pitmaster/tools/sensor.py +++ b/pitmaster/tools/sensor.py @@ -34,11 +34,6 @@ GPIO.setup(SPICLK, GPIO.OUT) GPIO.setup(SPICS, GPIO.OUT) -resistor_size = 10000 -sth_coef_a = 0.000436925136556 -sth_coef_b = 0.000230203788274 -sth_coef_c = 0.000000060486575 - def _temp_raw(sensor=None): with open(sensor, "r") as file_reader: From 4634ef41aa69eabfd4717886e086a33799f12387 Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 22:09:35 +0000 Subject: [PATCH 09/11] moved some maths --- pitmaster/tools/temps.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pitmaster/tools/temps.py b/pitmaster/tools/temps.py index 4e1ea65..631b377 100644 --- a/pitmaster/tools/temps.py +++ b/pitmaster/tools/temps.py @@ -18,12 +18,17 @@ def convert_to_resistance(reading=None): + resistor_size = 10000 r1 = (1023.0 / reading) - 1.0 resistance = resistor_size / r1 return resistance def resistance_to_temp(resistance=None): + sth_coef_a = 0.000436925136556 + sth_coef_b = 0.000230203788274 + sth_coef_c = 0.000000060486575 + if resistance is None: raise MissingPropertyException("resistance can not be None!") t = sth_coef_a From a9c67d69ee67bdfd5332b23b705c4ff16af54e6e Mon Sep 17 00:00:00 2001 From: bunchc Date: Sat, 6 Feb 2016 22:28:47 +0000 Subject: [PATCH 10/11] potato --- pitmaster/tools/sensor.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/pitmaster/tools/sensor.py b/pitmaster/tools/sensor.py index c476a39..a2a1aa7 100644 --- a/pitmaster/tools/sensor.py +++ b/pitmaster/tools/sensor.py @@ -16,25 +16,11 @@ import RPi.GPIO as GPIO from pitmaster.exceptions import * - -# For the thermistor: -# change these as desired - they're the pins connected from the -# SPI port on the ADC to the Cobbler -SPICLK = 18 -SPIMISO = 23 -SPIMOSI = 24 -SPICS = 25 +from pitmaster.tools import temps GPIO.setmode(GPIO.BCM) DEBUG = 1 -# set up the SPI interface pins -GPIO.setup(SPIMOSI, GPIO.OUT) -GPIO.setup(SPIMISO, GPIO.IN) -GPIO.setup(SPICLK, GPIO.OUT) -GPIO.setup(SPICS, GPIO.OUT) - - def _temp_raw(sensor=None): with open(sensor, "r") as file_reader: lines = file_reader.readlines() @@ -68,7 +54,7 @@ def read_temp(sensor=None, offset=None, probe_type=None): temp_c = (float(temp_string) / 1000.0) + offset return temp_c else: - temp_c = read_thermistor(cspin=sensor) + temp_c = read_thermistor(adcnum=sensor) return temp_c @@ -106,7 +92,18 @@ def find_temp_sensors(probe_type=None): ] -def read_thermistor(clockpin=SPICLK, mosipin=SPIMOSI, misopin=SPIMISO, cspin=SPICS, adcnum=None, offset=None): +def read_thermistor(adcnum=None, offset=None): + clockpin = 18 + mosipin = 24 + misopin = 23 + cspin = 25 + + # set up the SPI interface pins + GPIO.setup(mosipin, GPIO.OUT) + GPIO.setup(misopin, GPIO.IN) + GPIO.setup(clockpin, GPIO.OUT) + GPIO.setup(cspin, GPIO.OUT) + if ((adcnum > 7) or (adcnum < 0)): return -1 From 2b8c841007eccc37afb94109fb801d866ca6f284 Mon Sep 17 00:00:00 2001 From: bunchc Date: Thu, 18 Feb 2016 04:18:35 +0000 Subject: [PATCH 11/11] fixed thermistor readings --- pitmaster/tools/sensor.py | 123 ++++++++++++++++++++++---------------- pitmaster/tools/temps.py | 22 ------- 2 files changed, 73 insertions(+), 72 deletions(-) diff --git a/pitmaster/tools/sensor.py b/pitmaster/tools/sensor.py index a2a1aa7..5da8f0d 100644 --- a/pitmaster/tools/sensor.py +++ b/pitmaster/tools/sensor.py @@ -13,13 +13,24 @@ # limitations under the License. import os import time +import math import RPi.GPIO as GPIO from pitmaster.exceptions import * -from pitmaster.tools import temps + +SPICLK = 18 +SPIMOSI = 24 +SPIMISO = 23 +SPICS = 25 GPIO.setmode(GPIO.BCM) DEBUG = 1 +# set up the SPI interface pins +GPIO.setup(SPIMOSI, GPIO.OUT) +GPIO.setup(SPIMISO, GPIO.IN) +GPIO.setup(SPICLK, GPIO.OUT) +GPIO.setup(SPICS, GPIO.OUT) + def _temp_raw(sensor=None): with open(sensor, "r") as file_reader: @@ -27,6 +38,66 @@ def _temp_raw(sensor=None): return lines +def _read_thermistor(sensor=None): + adcnum = int(sensor) + clockpin = SPICLK + mosipin = SPIMOSI + misopin = SPIMISO + cspin = SPICS + + if ((adcnum > 7) or (adcnum < 0)): + raise SensorNotFoundException("Invalid Thermisor Location: {}".format(sensor)) + + GPIO.output(cspin, True) + GPIO.output(clockpin, False) # start clock low + GPIO.output(cspin, False) # bring CS low + + commandout = adcnum + commandout |= 0x18 # start bit + single-ended bit + commandout <<= 3 # we only need to send 5 bits here + + for i in range(5): + if (commandout & 0x80): + GPIO.output(mosipin, True) + else: + GPIO.output(mosipin, False) + commandout <<= 1 + GPIO.output(clockpin, True) + GPIO.output(clockpin, False) + + adcout = 0 + # read in one empty bit, one null bit and 10 ADC bits + for i in range(12): + GPIO.output(clockpin, True) + GPIO.output(clockpin, False) + adcout <<= 1 + if (GPIO.input(misopin)): + adcout |= 0x1 + + GPIO.output(cspin, True) + + adcout >>= 1 # first bit is 'null' so drop it + resistor_size = 10000 + r1 = (1023.0 / adcout) - 1.0 + if r1 > 0: + resistance = resistor_size / r1 + else: + return -1 + + sth_coef_a = 0.000436925136556 + sth_coef_b = 0.000230203788274 + sth_coef_c = 0.000000060486575 + + if resistance is None: + raise MissingPropertyException("resistance can not be None!") + t = sth_coef_a + t += sth_coef_b * (math.log(resistance)) + t += sth_coef_c * math.pow((math.log(resistance)), 3) + t = 1 / t + t -= 273.15 + return t + + def read_temp(sensor=None, offset=None, probe_type=None): """ Reads the temp sensor and returns its temp in C. @@ -54,7 +125,7 @@ def read_temp(sensor=None, offset=None, probe_type=None): temp_c = (float(temp_string) / 1000.0) + offset return temp_c else: - temp_c = read_thermistor(adcnum=sensor) + temp_c = _read_thermistor(sensor) + offset return temp_c @@ -92,54 +163,6 @@ def find_temp_sensors(probe_type=None): ] -def read_thermistor(adcnum=None, offset=None): - clockpin = 18 - mosipin = 24 - misopin = 23 - cspin = 25 - - # set up the SPI interface pins - GPIO.setup(mosipin, GPIO.OUT) - GPIO.setup(misopin, GPIO.IN) - GPIO.setup(clockpin, GPIO.OUT) - GPIO.setup(cspin, GPIO.OUT) - - if ((adcnum > 7) or (adcnum < 0)): - return -1 - - GPIO.output(cspin, True) - GPIO.output(clockpin, False) # start clock low - GPIO.output(cspin, False) # bring CS low - - commandout = adcnum - commandout |= 0x18 # start bit + single-ended bit - commandout <<= 3 # we only need to send 5 bits here - for i in range(5): - if (commandout & 0x80): - GPIO.output(mosipin, True) - else: - GPIO.output(mosipin, False) - commandout <<= 1 - GPIO.output(clockpin, True) - GPIO.output(clockpin, False) - - adcout = 0 - # read in one empty bit, one null bit and 10 ADC bits - for i in range(12): - GPIO.output(clockpin, True) - GPIO.output(clockpin, False) - adcout <<= 1 - if (GPIO.input(misopin)): - adcout |= 0x1 - - GPIO.output(cspin, True) - - adcout >>= 1 # first bit is 'null' so drop it - resistance = temps.convert_to_resistance(adcout) - temp_c = temps.resistance_to_temp(resistance) - return temp_c - - if __name__ == "__main__": for i in range(15): test = read_temp(sensor="/sys/bus/w1/devices/3b-0000001921e8/w1_slave") diff --git a/pitmaster/tools/temps.py b/pitmaster/tools/temps.py index 631b377..ec28cce 100644 --- a/pitmaster/tools/temps.py +++ b/pitmaster/tools/temps.py @@ -17,28 +17,6 @@ from pitmaster.exceptions import * -def convert_to_resistance(reading=None): - resistor_size = 10000 - r1 = (1023.0 / reading) - 1.0 - resistance = resistor_size / r1 - return resistance - - -def resistance_to_temp(resistance=None): - sth_coef_a = 0.000436925136556 - sth_coef_b = 0.000230203788274 - sth_coef_c = 0.000000060486575 - - if resistance is None: - raise MissingPropertyException("resistance can not be None!") - t = sth_coef_a - t += sth_coef_b * (math.log(resistance)) - t += sth_coef_c * math.pow((math.log(resistance)), 3) - t = 1 / t - t -= 273.15 - return t - - def from_c_to_f(temp=None): """ Converts a given temp from Celsius to fahrenheit