From fe06d18cdb9f276e7b06e107b55c655e74923b6a Mon Sep 17 00:00:00 2001 From: Alexey McSakoff Date: Mon, 16 Nov 2020 21:03:31 +0400 Subject: [PATCH] Effective temperature reading from multiple sensors --- digitemp/device/thermometer.py | 18 +++++---- digitemp/master.py | 15 +++++++- .../{detect_sensors.py => read_sensors.py} | 0 examples/read_sensors_effective.py | 37 +++++++++++++++++++ 4 files changed, 60 insertions(+), 10 deletions(-) rename examples/{detect_sensors.py => read_sensors.py} (100%) create mode 100644 examples/read_sensors_effective.py diff --git a/digitemp/device/thermometer.py b/digitemp/device/thermometer.py index 4538975..e7b4912 100644 --- a/digitemp/device/thermometer.py +++ b/digitemp/device/thermometer.py @@ -1,5 +1,6 @@ import time import struct +import warnings from ..utils import * from ..master import UART_Adapter @@ -77,6 +78,13 @@ def get_temperature(self, attempts=3): attempts = attempts if attempts > 1 else 1 self._reset() self._convert_T() + return self.read_temperature(attempts=attempts) + + def read_temperature(self, attempts=3): + # type: (int) -> float + """ + Read scratchpad memory and calculates the temperature. + """ for i in range(attempts): self._reset() try: @@ -90,14 +98,8 @@ def get_temperature(self, attempts=3): def convert_T_all(self): # type: () -> None - """ - This forces all temperature sensors to calculate temperature and set/unset alarm flag. - """ - self.bus.skip_ROM() - self.bus.write_byte(0x44) - # We do not know if there are any DS18B20 or DS1822 on the line and what are their resolution settings. - # So, we just wait max(T_conv) that is 750ms for currently supported devices. - time.sleep(self.T_CONV) + warnings.warn("deprecated", DeprecationWarning) + return self.bus.measure_temperature_all() # ---[ Function Commands ]---- diff --git a/digitemp/master.py b/digitemp/master.py index 072cbe0..3bfd858 100644 --- a/digitemp/master.py +++ b/digitemp/master.py @@ -16,6 +16,7 @@ For details see: Using an UART to Implement a 1-Wire Bus Master (http://www.maximintegrated.com/en/app-notes/index.mvp/id/214) """ +import time import serial import platform from .utils import * @@ -237,7 +238,6 @@ def read_ROM(self): # type: () -> bytes """ READ ROM [33h] - This command can only be used when there is one device on the bus. It allows the bus driver to read the device's 64-bit ROM code without using the Search ROM procedure. If this command is used when there is more than one device present on the bus, a data collision will occur when all the devices attempt to @@ -255,7 +255,6 @@ def match_ROM(self, rom_code): # type: (bytes) -> None """ MATCH ROM [55h] - The match ROM command allows to address a specific device on a multidrop or single-drop bus. Only the device that exactly matches the 64-bit ROM code sequence will respond to the function command issued by the master; all other devices on the bus will wait for a reset pulse. @@ -267,6 +266,7 @@ def match_ROM(self, rom_code): def skip_ROM(self): # type: () -> None """ + SKIP ROM [CCh] The master can use this command to address all devices on the bus simultaneously without sending out any ROM code information. """ @@ -330,6 +330,17 @@ def search(current_rom=None): return complete_roms + def measure_temperature_all(self): + # type: () -> None + """ + This forces all temperature sensors to calculate temperature and set/unset alarm flag. + """ + self.skip_ROM() + self.write_byte(0x44) + # We do not know if there are any DS18B20 or DS1822 on the line and what are their resolution settings. + # So, we just wait max(T_conv) that is 750ms for currently supported devices. + time.sleep(0.75) + # ---[ Helper Functions ]---- def get_connected_ROMs(self): diff --git a/examples/detect_sensors.py b/examples/read_sensors.py similarity index 100% rename from examples/detect_sensors.py rename to examples/read_sensors.py diff --git a/examples/read_sensors_effective.py b/examples/read_sensors_effective.py new file mode 100644 index 0000000..9672170 --- /dev/null +++ b/examples/read_sensors_effective.py @@ -0,0 +1,37 @@ +import time +from digitemp.master import UART_Adapter +from digitemp.device import TemperatureSensor +from digitemp.exceptions import OneWireException + +bus = UART_Adapter('/dev/ttyS0') + +sensors = [] +for rom in bus.get_connected_ROMs(): + try: + sensors.append(TemperatureSensor(bus, rom)) + except OneWireException: + pass + +print(55 * "=") +for sensor in sensors: + sensor.info() + print(55 * "=") + +# Instead of calling sensor.get_temperature() for each sensor we call bus.measure_temperature_all() once +# and then do sensor.read_temperature() for each sensor. + +try: + while True: + measurements = [] + bus.measure_temperature_all() + for sensor in sensors: + try: + measurements.append("%3.02fÂșC" % sensor.read_temperature()) + except OneWireException: + measurements.append(" error") + print(" ".join(measurements)) + time.sleep(3) +except KeyboardInterrupt: + pass +finally: + bus.close()