diff --git a/esp32/include/DS2438.h b/esp32/include/DS2438.h index b8eecc8..83375c0 100644 --- a/esp32/include/DS2438.h +++ b/esp32/include/DS2438.h @@ -67,9 +67,8 @@ typedef uint8_t DeviceAddress[8]; class DS2438 { public: - DS2438(OneWire *ow, float currentShunt); - DS2438(OneWire *ow, uint8_t *address); - + DS2438(OneWire *ow, float currentShunt, int retryOnCRCError); + void begin(); void update(); double getTemperature(); @@ -94,6 +93,7 @@ class DS2438 { float _voltageB; float _current; float _currentShunt; + int _retryOnCRCError; long _CCA; long _DCA; long _ICA; diff --git a/esp32/src/DS2438.cpp b/esp32/src/DS2438.cpp index c4ff991..79b4dac 100644 --- a/esp32/src/DS2438.cpp +++ b/esp32/src/DS2438.cpp @@ -24,9 +24,10 @@ #define DS2438MODEL 0x26 -DS2438::DS2438(OneWire *ow, float currentShunt = 1.0f) { +DS2438::DS2438(OneWire *ow, float currentShunt, int retryOnCRCError) { _ow = ow; _currentShunt = currentShunt; + _retryOnCRCError = retryOnCRCError; }; void DS2438::begin(){ @@ -234,22 +235,26 @@ void DS2438::writePage(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 - _ow->reset(); - _ow->select(_address); - _ow->write(DS2438_RECALL_MEMORY_COMMAND, 0); - if ((page >= PAGE_MIN) && (page <= PAGE_MAX)) { + 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 + _ow->reset(); + _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); - } else { - return false; + for (int i = 0; i < 9; i++){ + data[i] = _ow->read(); + } + valid = _ow->crc8(data, 8) == data[8]; } - _ow->reset(); - _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]; + return valid; } diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index c1f001f..d5c2e40 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -79,7 +79,7 @@ unsigned long setupFinishedTimestamp; OneWire oneWire(SENSOR_ONEWIRE); DallasTemperature sensors(&oneWire); -DS2438 battery(&oneWire, 0.0333333f); +DS2438 battery(&oneWire, 0.0333333f, AMOUNT_SENOR_QUERYS); Plant mPlants[MAX_PLANTS] = { Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0, &plant0, &mSetting0), @@ -190,8 +190,19 @@ void readOneWireSensors(bool withMQTT) { DeviceAddress ds18b20Address; sensors.getAddress(ds18b20Address, i); - float temp = sensors.getTempC(ds18b20Address); - Serial << "OneWire sensor " << i << " has value " << temp << endl; + bool valid = false; + 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]; snprintf(buf, sizeof(buf), "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X", ds18b20Address[0], @@ -203,28 +214,36 @@ void readOneWireSensors(bool withMQTT) ds18b20Address[6], 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) { - 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(); @@ -651,7 +670,7 @@ void loop() { mPlants[lastPumpRunning].deactivatePump(); } - if (lastPumpRunning >= MAX_PLANTS) + if (lastPumpRunning >= MAX_PLANTS) { digitalWrite(OUTPUT_ENABLE_PUMP, LOW); nextBlink = millis() + 500;