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,9 +67,8 @@ 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();
double getTemperature(); double getTemperature();
@ -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,22 +235,26 @@ void DS2438::writePage(int page, uint8_t *data) {
} }
boolean DS2438::readPage(int page, uint8_t *data) { boolean DS2438::readPage(int page, uint8_t *data) {
//TODO if all data is 0 0 is a valid crc, but most likly not as intended bool valid = false;
_ow->reset(); for(int retry = 0;retry < this->_retryOnCRCError && !valid; retry ++){
_ow->select(_address); //TODO if all data is 0 0 is a valid crc, but most likly not as intended
_ow->write(DS2438_RECALL_MEMORY_COMMAND, 0); _ow->reset();
if ((page >= PAGE_MIN) && (page <= PAGE_MAX)) { _ow->select(_address);
_ow->write(DS2438_RECALL_MEMORY_COMMAND, 0);
if ((page >= PAGE_MIN) && (page <= PAGE_MAX)) {
_ow->write(page, 0);
} else {
return false;
}
_ow->reset();
_ow->select(_address);
_ow->write(DS2438_READ_SCRATCHPAD_COMMAND, 0);
_ow->write(page, 0); _ow->write(page, 0);
} else { for (int i = 0; i < 9; i++){
return false; data[i] = _ow->read();
}
valid = _ow->crc8(data, 8) == data[8];
} }
_ow->reset(); return valid;
_ow->select(_address);
_ow->write(DS2438_READ_SCRATCHPAD_COMMAND, 0);
_ow->write(page, 0);
for (int i = 0; i < 9; i++){
data[i] = _ow->read();
}
return _ow->crc8(data, 8) == data[8];
} }

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,28 +214,36 @@ void readOneWireSensors(bool withMQTT)
ds18b20Address[6], ds18b20Address[6],
ds18b20Address[7]); ds18b20Address[7]);
if (String(lipoSensorAddr.get()).compareTo(String(buf)))
if (valid)
{ {
Serial << "OneWire sensor " << String(buf) << " has value " << temp << endl;
if (String(lipoSensorAddr.get()).compareTo(String(buf)))
{
if (withMQTT)
{
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(temp));
}
Serial << "Lipo Temperatur " << temp << " °C " << endl;
}
else if (String(waterSensorAddr.get()).compareTo(String(buf)))
{
if (withMQTT)
{
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(temp));
}
Serial << "Water Temperatur " << temp << " °C " << endl;
}
/* Always send the sensor address with the temperatur value */
if (withMQTT) if (withMQTT)
{ {
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(temp)); sensorTemp.setProperty(String(buf)).send(String(temp));
} }
Serial << "Lipo Temperatur " << temp << " °C " << endl; Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl;
} else {
Serial << "OneWire sensor " << String(buf) << " could not be read " << temp << endl;
} }
else if (String(waterSensorAddr.get()).compareTo(String(buf)))
{
if (withMQTT)
{
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(temp));
}
Serial << "Water Temperatur " << temp << " °C " << endl;
}
/* Always send the sensor address with the temperatur value */
if (withMQTT)
{
sensorTemp.setProperty(String(buf)).send(String(temp));
}
Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl;
} }
battery.update(); battery.update();
@ -651,7 +670,7 @@ void loop()
{ {
mPlants[lastPumpRunning].deactivatePump(); mPlants[lastPumpRunning].deactivatePump();
} }
if (lastPumpRunning >= MAX_PLANTS) if (lastPumpRunning >= MAX_PLANTS)
{ {
digitalWrite(OUTPUT_ENABLE_PUMP, LOW); digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
nextBlink = millis() + 500; nextBlink = millis() + 500;