Skip to content

Commit 16445c5

Browse files
committed
Add retry for getTempC.
I have only implemented this for getTempC because that's the method I use in project (i.e. not getTempF).
1 parent e39e8ab commit 16445c5

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

DallasTemperature.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -723,22 +723,27 @@ int32_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress,
723723
// the numeric value of DEVICE_DISCONNECTED_RAW is defined in
724724
// DallasTemperature.h. It is a large negative number outside the
725725
// operating range of the device
726-
int32_t DallasTemperature::getTemp(const uint8_t* deviceAddress) {
726+
int32_t DallasTemperature::getTemp(const uint8_t* deviceAddress, byte retryCount = 0) {
727727

728728
ScratchPad scratchPad;
729-
if (isConnected(deviceAddress, scratchPad))
730-
return calculateTemperature(deviceAddress, scratchPad);
731-
return DEVICE_DISCONNECTED_RAW;
732-
729+
730+
byte retries = 0;
731+
732+
while (retries++ <= retryCount) {
733+
if (isConnected(deviceAddress, scratchPad))
734+
return calculateTemperature(deviceAddress, scratchPad);
735+
}
736+
737+
return DEVICE_DISCONNECTED_RAW;
733738
}
734739

735740
// returns temperature in degrees C or DEVICE_DISCONNECTED_C if the
736741
// device's scratch pad cannot be read successfully.
737742
// the numeric value of DEVICE_DISCONNECTED_C is defined in
738743
// DallasTemperature.h. It is a large negative number outside the
739744
// operating range of the device
740-
float DallasTemperature::getTempC(const uint8_t* deviceAddress) {
741-
return rawToCelsius(getTemp(deviceAddress));
745+
float DallasTemperature::getTempC(const uint8_t* deviceAddress, byte retryCount = 0) {
746+
return rawToCelsius(getTemp(deviceAddress, retryCount));
742747
}
743748

744749
// returns temperature in degrees F or DEVICE_DISCONNECTED_F if the

DallasTemperature.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ class DallasTemperature {
159159
request_t requestTemperaturesByIndex(uint8_t);
160160

161161
// returns temperature raw value (12 bit integer of 1/128 degrees C)
162-
int32_t getTemp(const uint8_t*);
162+
int32_t getTemp(const uint8_t*, byte retryCount = 0);
163163

164164
// returns temperature in degrees C
165-
float getTempC(const uint8_t*);
165+
float getTempC(const uint8_t*, byte retryCount = 0);
166166

167167
// returns temperature in degrees F
168168
float getTempF(const uint8_t*);

0 commit comments

Comments
 (0)