Skip to content

Commit

Permalink
wireless temperature sensor initialization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vitotai committed Feb 29, 2024
1 parent 16d84e6 commit ce14288
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/TempSensorWireless.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class WirelessTempSensor: public BasicTempSensor
WirelessTempSensor(bool connected=false,fixed4_4 cal=0,uint32_t expiryTime=300){
setConnected(connected);
_expiryTime = expiryTime * 1000;

_temperatureValid = false;
const uint8_t shift = TEMP_FIXED_POINT_BITS-ONEWIRE_TEMP_SENSOR_PRECISION; // difference in precision between DS18B20 format and temperature adt
//temperature i fixed7_9, calibration fixed4_4
_calibrationOffset =constrainTemp16(temperature(cal)<<shift);
Expand All @@ -28,7 +28,7 @@ class WirelessTempSensor: public BasicTempSensor
}

void setTemp(double temp){
this->_connected = true;
_temperatureValid = true;
setValue(doubleToTemp(temp));
_updateTime = millis();
}
Expand All @@ -41,16 +41,28 @@ class WirelessTempSensor: public BasicTempSensor
bool isConnected() { return _connected; }

bool init() {
return read()!=TEMP_SENSOR_DISCONNECTED;
if(_temperatureValid){
if((millis() - _updateTime) < _expiryTime){
setConnected(true);
return true;
}
_temperatureValid=false;
}
return false;
}

temperature read() {
if (!isConnected())
return TEMP_SENSOR_DISCONNECTED;
// _connected should be set only in "init()" before "read()" is called
// so that the filters can be initialized() corrected.
if (!isConnected()) return TEMP_SENSOR_DISCONNECTED;

if((millis() - _updateTime) > _expiryTime){
this->_connected = false;
setConnected(false);
_temperatureValid = false;
return TEMP_SENSOR_DISCONNECTED;
}
}else{
setConnected(true);
}
temperature temp = _temperature + _calibrationOffset;
return temp;
}
Expand All @@ -63,6 +75,7 @@ class WirelessTempSensor: public BasicTempSensor
temperature _temperature;
temperature _calibrationOffset;
bool _connected;
bool _temperatureValid;
uint32_t _expiryTime;
uint32_t _updateTime;
};

1 comment on commit ce14288

@DMT07
Copy link

@DMT07 DMT07 commented on ce14288 Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. There is one more issue though.

In TempSensor.cpp there must be a check placed at the start of void TempSensor::update() to ensure that an update cannot be done if the TempSensor object has not been fully initialized.

Adding something like the following should fix it.

void TempSensor::update()
{
	// only update if filters are initialized
	if (failedReadCount<0){return;}
...
}

When a TempSensor() class object is instantiated, failedReadCount is set to -1 to indicate that it is not initialized. In void TempSensor::init() it is set to failedReadCount = 0; when sucessfully initialized. The problem still remains that when void TempSensor::update() is called, failedReadCount is incremented while the sensor is not initialized yet, leading to filter misbehaviour.

Please sign in to comment.