diff --git a/README.rst b/README.rst index 2652fb5..7573aee 100644 --- a/README.rst +++ b/README.rst @@ -17,7 +17,11 @@ Introduction :target: https://github.com/psf/black :alt: Code Style: Black -CircuitPython library for the TI TMP117 Temperature sensor +CircuitPython library for the TI TMP116, TMP117, TMP119 Temperature sensor + +It is forked from Adafruit_CircuitPython_TMP117, updated and extended to use it with TMP116 and TMP119 + +All those sensor have common register map WARNING: Library may not run on some boards with less RAM such as boards using the SAMD21 @@ -41,13 +45,13 @@ PyPI `_. To install for .. code-block:: shell - pip3 install adafruit-circuitpython-tmp117 + pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git To install system-wide (this may be required in some cases): .. code-block:: shell - sudo pip3 install adafruit-circuitpython-tmp117 + sudo pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git To install in a virtual environment in your current project: @@ -56,7 +60,7 @@ To install in a virtual environment in your current project: mkdir project-name && cd project-name python3 -m venv .venv source .venv/bin/activate - pip3 install adafruit-circuitpython-tmp117 + pip3 install git+https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git Usage Example ============= @@ -65,13 +69,15 @@ Usage Example import time import board - import adafruit_tmp117 + import adafruit_tmp11X + from adafruit_tmp11X import TMP117, TMP116, TMP119, AverageCount, MeasurementMode, MeasurementDelay i2c = board.I2C() # uses board.SCL and board.SDA - tmp117 = adafruit_tmp117.TMP117(i2c) - + t1 = TMP116(i2c_bus=i2c, address=0x49) + # t2 = TMP117(i2c_bus=i2c, address=0x49) + # t2 = TMP119(i2c_bus=i2c, address=0x49) while True: - print("Temperature: %.2f degrees C"%tmp117.temperature) + print("Temperature:", t1.temperature) time.sleep(1) Documentation diff --git a/adafruit_tmp117.py b/adafruit_tmp11X.py similarity index 92% rename from adafruit_tmp117.py rename to adafruit_tmp11X.py index 58d861b..a17a339 100644 --- a/adafruit_tmp117.py +++ b/adafruit_tmp11X.py @@ -2,10 +2,10 @@ # # SPDX-License-Identifier: MIT """ -`adafruit_tmp117` +`adafruit_tmp11X` ================================================================================ -CircuitPython library for the TI TMP117 Temperature sensor +CircuitPython library for the TI TMP116,TMP117,TMP119 Temperature sensor * Author(s): Bryan Siepert, Ian Grant @@ -53,7 +53,7 @@ pass __version__ = "0.0.0+auto.0" -__repo__ = "https:#github.com/adafruit/Adafruit_CircuitPython_TMP117.git" +__repo__ = "https://github.com/ami3go/Adafruit_CircuitPython_TMP11X.git" _I2C_ADDR = 0x48 # default I2C Address @@ -67,7 +67,10 @@ _TEMP_OFFSET = const(0x07) _EEPROM3 = const(0x08) _DEVICE_ID = const(0x0F) -_DEVICE_ID_VALUE = 0x0117 +_TMP117_DEVICE_ID_VALUE = const(0x0117) +_TMP116_DEVICE_ID_VALUE = const(0x1116) +_TMP119_DEVICE_ID_VALUE = const(0x0117) + _TMP117_RESOLUTION = ( 0.0078125 # Resolution of the device, found on (page 1 of datasheet) ) @@ -113,10 +116,12 @@ def is_valid(cls, value: int) -> bool: """Validate that a given value is a member""" return value in cls.string - class AverageCount(CV): """Options for `averaged_measurements`""" - + AVERAGE_1X = None + AVERAGE_8X = None + AVERAGE_32X = None + AVERAGE_64X = None AverageCount.add_values( ( @@ -128,9 +133,17 @@ class AverageCount(CV): ) + class MeasurementDelay(CV): """Options for `measurement_delay`""" - + DELAY_0_0015_S = None + DELAY_0_125_S = None + DELAY_0_250_S = None + DELAY_0_500_S = None + DELAY_1_S = None + DELAY_4_S = None + DELAY_8_S = None + DELAY_16_S = None MeasurementDelay.add_values( ( @@ -148,16 +161,20 @@ class MeasurementDelay(CV): class AlertMode(CV): """Options for `alert_mode`. See `alert_mode` for more information.""" - - + WINDOW = None + HYSTERESIS = None AlertMode.add_values( - (("WINDOW", 0, "Window", None), ("HYSTERESIS", 1, "Hysteresis", None)) + ( + ("WINDOW", 0, "Window", None), + ("HYSTERESIS", 1, "Hysteresis", None)) ) class MeasurementMode(CV): """Options for `measurement_mode`. See `measurement_mode` for more information.""" - + CONTINUOUS = None + ONE_SHOT = None + SHUTDOWN = None MeasurementMode.add_values( ( @@ -166,11 +183,15 @@ class MeasurementMode(CV): ("SHUTDOWN", 1, "Shutdown", None), ) ) +class Symbols: + deg = u'\N{DEGREE SIGN}' + degC = deg + "C" + degK = deg + "K" + degF = deg + "F" class TMP117: """Library for the TI TMP117 high-accuracy temperature sensor""" - _part_id = ROUnaryStruct(_DEVICE_ID, ">H") _raw_temperature = ROUnaryStruct(_TEMP_RESULT, ">h") _raw_high_limit = UnaryStruct(_T_HIGH_LIMIT, ">h") @@ -192,12 +213,17 @@ class TMP117: def __init__(self, i2c_bus: I2C, address: int = _I2C_ADDR): self.i2c_device = i2c_device.I2CDevice(i2c_bus, address) - if self._part_id != _DEVICE_ID_VALUE: - raise AttributeError("Cannot find a TMP117") + self._check_dev_id() # currently set when `alert_status` is read, but not exposed self.reset() self.initialize() + + # thsi methods is unique for each device + def _check_dev_id(self): + id = _TMP117_DEVICE_ID_VALUE + if self._part_id != id: + raise AttributeError("Cannot find a TMP117") def reset(self): """Reset the sensor to its unconfigured power-on state""" self._soft_reset = True @@ -218,6 +244,10 @@ def temperature(self): return self._read_temperature() + @property + def temperature_updated(self): + return self._wait_for_measurement() + @property def temperature_offset(self): """User defined temperature offset to be added to measurements from `temperature` @@ -520,9 +550,11 @@ def serial_number(self): def _set_mode_and_wait_for_measurement(self, mode: int) -> float: self._mode = mode # poll for data ready + return self._wait_for_measurement() + + def _wait_for_measurement(self)-> float: while not self._read_status()[2]: time.sleep(0.001) - return self._read_temperature() # eeprom write enable to set defaults for limits and config @@ -539,4 +571,17 @@ def _read_status(self) -> Tuple[int, int, int]: return (high_alert, low_alert, data_ready) def _read_temperature(self) -> float: - return self._raw_temperature * _TMP117_RESOLUTION + return round(self._raw_temperature * _TMP117_RESOLUTION, 3) + + +class TMP119(TMP117): + def _check_dev_id(self): + id = _TMP119_DEVICE_ID_VALUE + if self._part_id != id: + raise AttributeError("Cannot find a TMP119") + +class TMP116(TMP117): + def _check_dev_id(self): + id = _TMP116_DEVICE_ID_VALUE + if self._part_id != id: + raise AttributeError("Cannot find a TMP116") diff --git a/docs/TMP116-TMP117-TMP119.png b/docs/TMP116-TMP117-TMP119.png new file mode 100644 index 0000000..9308a63 Binary files /dev/null and b/docs/TMP116-TMP117-TMP119.png differ diff --git a/examples/Scanner.py b/examples/Scanner.py new file mode 100644 index 0000000..7d6c947 --- /dev/null +++ b/examples/Scanner.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries +# +# SPDX-License-Identifier: Unlicense +import time +import os +os.environ['BLINKA_FT232H'] = "1" +import board +import time +import busio +import datetime +i2c = busio.I2C(board.SCL, board.SDA) + +while not i2c.try_lock(): + pass + +while True: + print(datetime.datetime.now(),"I2C addresses found:", [hex(device_address) + for device_address in i2c.scan()]) + time.sleep(2) diff --git a/examples/Sensors_array.py b/examples/Sensors_array.py new file mode 100644 index 0000000..ec542b5 --- /dev/null +++ b/examples/Sensors_array.py @@ -0,0 +1,78 @@ +import os +os.environ['BLINKA_FT232H'] = "1" +import board +from adafruit_tmp11X import TMP117, TMP116, TMP119, AverageCount, MeasurementMode, MeasurementDelay, Symbols +import time +import datetime +import Logging_class +global sensors + + + +i2c = board.I2C() # uses board.SCL and board.SDA +t1 = TMP117(i2c_bus=i2c, address=0x48) +t2 = TMP117(i2c_bus=i2c, address=0x49) +t3 = TMP117(i2c_bus=i2c, address=0x4a) +t4 = TMP117(i2c_bus=i2c, address=0x4b) + + +# add to this array multiple sensors. +# address limitation for single device allow to add up to 4 devises +# However you may add different I2C busses to array +sensors = [t1, t2, t3, t4] +# print(AverageCount.AVERAGE_32X) + +for sensor in sensors: + sensor.measurement_mode = MeasurementMode.CONTINUOUS + sensor.averaged_measurements = AverageCount.AVERAGE_32X + sensor.measurement_delay = MeasurementDelay.DELAY_0_0015_S + + +def get_temperature(ret_as="array"): + global sensors + for i in range(10): + try: + temp_array = [] + temp_dict = {} + for sensor in sensors: + temp_array.append(sensor.temperature_updated) + if ret_as == "arrya": + return temp_array + else: + for i, item in enumerate(temp_array): + key = f"T{i}" + temp_dict[key] = item + return temp_dict + except Exception as e: + + print(f"query[{i}]: Error: {e}") + time.sleep(5) + i2c = board.I2C() # uses board.SCL and board.SDA + t1 = TMP117(i2c_bus=i2c, address=0x48) + t2 = TMP117(i2c_bus=i2c, address=0x49) + t3 = TMP117(i2c_bus=i2c, address=0x4a) + t4 = TMP117(i2c_bus=i2c, address=0x4b) + sensors = [t1, t2, t3, t4] + for sensor in sensors: + sensor.measurement_mode = MeasurementMode.CONTINUOUS + sensor.averaged_measurements = AverageCount.AVERAGE_32X + sensor.measurement_delay = MeasurementDelay.DELAY_0_0015_S + + +def get_csv_keys(): + global sensors + keys = [] + for i in range(len(sensors)): + keys.append(f"T{i}") + return keys + +if __name__ == "__main__": + logger = Logging_class.txt_logger() + logger.init("temperature_TMP117") + z = 0 + while True: + + txt = f"[{z}]{get_temperature('dict')} Temperature:[{Symbols.degC}] " + logger.print_and_log(txt) + time.sleep(1) + z = z + 1 \ No newline at end of file diff --git a/examples/Sensors_array_v2.py b/examples/Sensors_array_v2.py new file mode 100644 index 0000000..203b9c3 --- /dev/null +++ b/examples/Sensors_array_v2.py @@ -0,0 +1,72 @@ +import os +os.environ['BLINKA_FT232H'] = "1" +import board +from adafruit_tmp11X import TMP117, TMP116, TMP119, AverageCount, MeasurementMode, MeasurementDelay, Symbols +import time +import datetime +import Logging_class +global sensors + + + + + + + +# add to this array multiple sensors. +# address limitation for single device allow to add up to 4 devises +# However you may add different I2C busses to array + + + +def get_temperature(ret_as="array"): + + for i in range(10): + i2c = board.I2C() # uses board.SCL and board.SDA + t1 = TMP117(i2c_bus=i2c, address=0x48) + t2 = TMP117(i2c_bus=i2c, address=0x49) + t3 = TMP117(i2c_bus=i2c, address=0x4a) + t4 = TMP117(i2c_bus=i2c, address=0x4b) + sensors = [t1, t2, t3, t4] + # print(AverageCount.AVERAGE_32X) + + for sensor in sensors: + sensor.measurement_mode = MeasurementMode.CONTINUOUS + sensor.averaged_measurements = AverageCount.AVERAGE_8X + sensor.measurement_delay = MeasurementDelay.DELAY_0_0015_S + + try: + temp_array = [] + temp_dict = {} + for sensor in sensors: + temp_array.append(sensor.temperature_updated) + if ret_as == "arrya": + return temp_array + else: + for i, item in enumerate(temp_array): + key = f"T{i}" + temp_dict[key] = item + i2c.deinit() + return temp_dict + except Exception as e: + print(f"query[{i}]: Error: {e}") + + + + + +def get_csv_keys(): + keys = [] + for i in range(4): + keys.append(f"T{i}") + return keys + +if __name__ == "__main__": + logger = Logging_class.txt_logger() + logger.init("temperature_TMP117") + + while True: + + txt = f"{get_temperature('dict')} {Symbols.degC} " + logger.print_and_log(txt) + time.sleep(2) \ No newline at end of file diff --git a/examples/tmp117_limits_test.py b/examples/tmp117_limits_test.py index 5692849..c3cfe53 100644 --- a/examples/tmp117_limits_test.py +++ b/examples/tmp117_limits_test.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: Unlicense import time import board -from adafruit_tmp117 import TMP117, AlertMode +from adafruit_tmp11X import TMP117, AlertMode i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller diff --git a/examples/tmp117_offset_test.py b/examples/tmp117_offset_test.py index fcd6a05..8a98042 100644 --- a/examples/tmp117_offset_test.py +++ b/examples/tmp117_offset_test.py @@ -3,12 +3,12 @@ # SPDX-License-Identifier: Unlicense import time import board -import adafruit_tmp117 +from adafruit_tmp11X import TMP117 i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller -tmp117 = adafruit_tmp117.TMP117(i2c) +tmp117 = TMP117(i2c) print("Temperature without offset: %.2f degrees C" % tmp117.temperature) tmp117.temperature_offset = 10.0 diff --git a/examples/tmp117_rate_and_averaging_test.py b/examples/tmp117_rate_and_averaging_test.py index 58a57ed..0515eab 100644 --- a/examples/tmp117_rate_and_averaging_test.py +++ b/examples/tmp117_rate_and_averaging_test.py @@ -7,7 +7,7 @@ # such as the one built into the Mu editor. import time import board -from adafruit_tmp117 import TMP117, AverageCount, MeasurementDelay +from adafruit_tmp11X import TMP117, AverageCount, MeasurementDelay i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller @@ -15,12 +15,12 @@ # uncomment different options below to see how it affects the reported temperature # tmp117.averaged_measurements = AverageCount.AVERAGE_1X -# tmp117.averaged_measurements = AverageCount.AVERAGE_8X +tmp117.averaged_measurements = AverageCount.AVERAGE_8X # tmp117.averaged_measurements = AverageCount.AVERAGE_32X # tmp117.averaged_measurements = AverageCount.AVERAGE_64X # tmp117.measurement_delay = MeasurementDelay.DELAY_0_0015_S -# tmp117.measurement_delay = MeasurementDelay.DELAY_0_125_S +tmp117.measurement_delay = MeasurementDelay.DELAY_0_125_S # tmp117.measurement_delay = MeasurementDelay.DELAY_0_250_S # tmp117.measurement_delay = MeasurementDelay.DELAY_0_500_S # tmp117.measurement_delay = MeasurementDelay.DELAY_1_S diff --git a/examples/tmp117_simpletest.py b/examples/tmp117_simpletest.py index 9128457..8f8ac27 100644 --- a/examples/tmp117_simpletest.py +++ b/examples/tmp117_simpletest.py @@ -3,11 +3,11 @@ # SPDX-License-Identifier: Unlicense import time import board -import adafruit_tmp117 +from adafruit_tmp11X import TMP117 i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller -tmp117 = adafruit_tmp117.TMP117(i2c) +tmp117 = TMP117(i2c) while True: print("Temperature: %.2f degrees C" % tmp117.temperature) diff --git a/examples/tmp117_single_measurement_test.py b/examples/tmp117_single_measurement_test.py index d019621..be9dba4 100644 --- a/examples/tmp117_single_measurement_test.py +++ b/examples/tmp117_single_measurement_test.py @@ -1,31 +1,34 @@ # SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries # # SPDX-License-Identifier: Unlicense +import os +os.environ['BLINKA_FT232H'] = "1" import board -from adafruit_tmp117 import TMP117, AverageCount + +from adafruit_tmp11X import TMP116, AverageCount i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller -tmp117 = TMP117(i2c) +t1 = TMP116(i2c_bus=i2c, address=0x49) # uncomment different options below to see how it affects the reported temperature # and measurement time -# tmp117.averaged_measurements = AverageCount.AVERAGE_1X -# tmp117.averaged_measurements = AverageCount.AVERAGE_8X -# tmp117.averaged_measurements = AverageCount.AVERAGE_32X -# tmp117.averaged_measurements = AverageCount.AVERAGE_64X +# t1.averaged_measurements = AverageCount.AVERAGE_1X +# t1.averaged_measurements = AverageCount.AVERAGE_8X +t1.averaged_measurements = AverageCount.AVERAGE_32X +# t1.averaged_measurements = AverageCount.AVERAGE_64X print( "Number of averaged samples per measurement:", - AverageCount.string[tmp117.averaged_measurements], + AverageCount.string[t1.averaged_measurements], ) print( "Reads should take approximately", - AverageCount.string[tmp117.averaged_measurements] * 0.0155, + AverageCount.string[t1.averaged_measurements] * 0.0155, "seconds", ) while True: - print("Single measurement: %.2f degrees C" % tmp117.take_single_measurement()) + print("Single measurement: %.2f degrees C" % t1.take_single_measurement()) # time.sleep(1) diff --git a/requirements.txt b/requirements.txt index fcef575..601c549 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,20 @@ Adafruit-Blinka adafruit-circuitpython-register adafruit-circuitpython-busdevice + +PyVISA~=1.13.0 +numpy~=1.26.0 +requests~=2.31.0 +matplotlib~=3.9.1 +pyserial~=3.5 +pandas~=2.1.1 +colorama~=0.4.6 +nidaqmx~=1.0.0 +VotschTechnikClimateChamber~=0 +pip~=24.0 +lxml~=4.9.2 +tornado~=6.4 +typing_extensions~=4.8.0 +setuptools~=57.4.0 +Cython~=3.0.7 +future~=0.18.3 \ No newline at end of file