Skip to content

Commit

Permalink
Effective temperature reading from multiple sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsakoff committed Nov 16, 2020
1 parent 7403d31 commit fe06d18
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
18 changes: 10 additions & 8 deletions digitemp/device/thermometer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
import struct
import warnings

from ..utils import *
from ..master import UART_Adapter
Expand Down Expand Up @@ -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:
Expand All @@ -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 ]----

Expand Down
15 changes: 13 additions & 2 deletions digitemp/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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):
Expand Down
File renamed without changes.
37 changes: 37 additions & 0 deletions examples/read_sensors_effective.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit fe06d18

Please sign in to comment.