retry on crc error for onewire

This commit is contained in:
Your Name 2021-05-27 21:43:15 +02:00
parent 3d45a3fca3
commit 057e2b37c3
3 changed files with 64 additions and 40 deletions

View File

@ -67,8 +67,7 @@ typedef uint8_t DeviceAddress[8];
class DS2438 { class DS2438 {
public: public:
DS2438(OneWire *ow, float currentShunt); DS2438(OneWire *ow, float currentShunt, int retryOnCRCError);
DS2438(OneWire *ow, uint8_t *address);
void begin(); void begin();
void update(); void update();
@ -94,6 +93,7 @@ class DS2438 {
float _voltageB; float _voltageB;
float _current; float _current;
float _currentShunt; float _currentShunt;
int _retryOnCRCError;
long _CCA; long _CCA;
long _DCA; long _DCA;
long _ICA; long _ICA;

View File

@ -24,9 +24,10 @@
#define DS2438MODEL 0x26 #define DS2438MODEL 0x26
DS2438::DS2438(OneWire *ow, float currentShunt = 1.0f) { DS2438::DS2438(OneWire *ow, float currentShunt, int retryOnCRCError) {
_ow = ow; _ow = ow;
_currentShunt = currentShunt; _currentShunt = currentShunt;
_retryOnCRCError = retryOnCRCError;
}; };
void DS2438::begin(){ void DS2438::begin(){
@ -234,6 +235,8 @@ void DS2438::writePage(int page, uint8_t *data) {
} }
boolean DS2438::readPage(int page, uint8_t *data) { boolean DS2438::readPage(int page, uint8_t *data) {
bool valid = false;
for(int retry = 0;retry < this->_retryOnCRCError && !valid; retry ++){
//TODO if all data is 0 0 is a valid crc, but most likly not as intended //TODO if all data is 0 0 is a valid crc, but most likly not as intended
_ow->reset(); _ow->reset();
_ow->select(_address); _ow->select(_address);
@ -250,6 +253,8 @@ boolean DS2438::readPage(int page, uint8_t *data) {
for (int i = 0; i < 9; i++){ for (int i = 0; i < 9; i++){
data[i] = _ow->read(); data[i] = _ow->read();
} }
return _ow->crc8(data, 8) == data[8]; valid = _ow->crc8(data, 8) == data[8];
}
return valid;
} }

View File

@ -79,7 +79,7 @@ unsigned long setupFinishedTimestamp;
OneWire oneWire(SENSOR_ONEWIRE); OneWire oneWire(SENSOR_ONEWIRE);
DallasTemperature sensors(&oneWire); DallasTemperature sensors(&oneWire);
DS2438 battery(&oneWire, 0.0333333f); DS2438 battery(&oneWire, 0.0333333f, AMOUNT_SENOR_QUERYS);
Plant mPlants[MAX_PLANTS] = { Plant mPlants[MAX_PLANTS] = {
Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0, &plant0, &mSetting0), Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0, &plant0, &mSetting0),
@ -190,8 +190,19 @@ void readOneWireSensors(bool withMQTT)
{ {
DeviceAddress ds18b20Address; DeviceAddress ds18b20Address;
sensors.getAddress(ds18b20Address, i); sensors.getAddress(ds18b20Address, i);
float temp = sensors.getTempC(ds18b20Address); bool valid = false;
Serial << "OneWire sensor " << i << " has value " << temp << endl; float temp = -127;
for (int retry = 0; retry < AMOUNT_SENOR_QUERYS && !valid; retry++)
{
temp = sensors.getTempC(ds18b20Address);
if (temp != -127)
{
valid = true;
}
}
//TODO is -127 bus error? if so add retry code
char buf[sizeof(DeviceAddress) * 2]; char buf[sizeof(DeviceAddress) * 2];
snprintf(buf, sizeof(buf), "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X", snprintf(buf, sizeof(buf), "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X",
ds18b20Address[0], ds18b20Address[0],
@ -203,6 +214,11 @@ void readOneWireSensors(bool withMQTT)
ds18b20Address[6], ds18b20Address[6],
ds18b20Address[7]); ds18b20Address[7]);
if (valid)
{
Serial << "OneWire sensor " << String(buf) << " has value " << temp << endl;
if (String(lipoSensorAddr.get()).compareTo(String(buf))) if (String(lipoSensorAddr.get()).compareTo(String(buf)))
{ {
if (withMQTT) if (withMQTT)
@ -225,6 +241,9 @@ void readOneWireSensors(bool withMQTT)
sensorTemp.setProperty(String(buf)).send(String(temp)); sensorTemp.setProperty(String(buf)).send(String(temp));
} }
Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl; Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl;
} else {
Serial << "OneWire sensor " << String(buf) << " could not be read " << temp << endl;
}
} }
battery.update(); battery.update();