Fix for odd Dallas sensor "not found" behaviour.

Minor flash use reduction.
This commit is contained in:
Blaz Kristan
2021-07-04 14:23:53 +02:00
parent de8a244500
commit bbdd1915eb
3 changed files with 2206 additions and 2202 deletions

View File

@@ -42,6 +42,8 @@ class UsermodTemperature : public Usermod {
bool waitingForConversion = false;
// flag set at startup if DS18B20 sensor not found, avoids trying to keep getting
// temperature if flashed to a board without a sensor attached
bool sensorFound = false;
bool enabled = true;
// strings to reduce flash memory usage (used more than twice)
@@ -87,7 +89,9 @@ class UsermodTemperature : public Usermod {
uint8_t deviceAddress[8] = {0,0,0,0,0,0,0,0};
// find out if we have DS18xxx sensor attached
oneWire->reset_search();
delay(10);
while (oneWire->search(deviceAddress)) {
DEBUG_PRINTLN(F("Found something..."));
if (oneWire->crc8(deviceAddress, 7) == deviceAddress[7]) {
switch (deviceAddress[0]) {
case 0x10: // DS18S20
@@ -114,15 +118,16 @@ class UsermodTemperature : public Usermod {
if (temperaturePin >= 0 && pinManager.allocatePin(temperaturePin)) {
oneWire = new OneWire(temperaturePin);
if (!oneWire->reset())
enabled = false; // resetting 1-Wire bus yielded an error
sensorFound = false; // resetting 1-Wire bus yielded an error
else
while ((enabled=findSensor()) && retries--) delay(25); // try to find sensor
while ((sensorFound=findSensor()) && retries--) delay(25); // try to find sensor
} else {
if (temperaturePin >= 0) DEBUG_PRINTLN(F("Temperature pin allocation failed."));
temperaturePin = -1; // allocation failed
enabled = false;
sensorFound = false;
}
}
lastMeasurement = millis() - readingInterval + 10000;
initDone = true;
}
@@ -190,7 +195,7 @@ class UsermodTemperature : public Usermod {
JsonArray temp = user.createNestedArray(FPSTR(_name));
//temp.add(F("Loaded."));
if (temperature <= -100) {
if (temperature <= -100.0 || (!sensorFound && temperature == -1.0)) {
temp.add(0);
temp.add(F(" Sensor Error!"));
return;
@@ -262,8 +267,10 @@ class UsermodTemperature : public Usermod {
temperaturePin = newTemperaturePin;
DEBUG_PRINTLN(F(" config loaded."));
} else {
DEBUG_PRINTLN(F(" config (re)loaded."));
// changing paramters from settings page
if (newTemperaturePin != temperaturePin) {
DEBUG_PRINTLN(F("Re-init temperature."));
// deallocate pin and release memory
delete oneWire;
pinManager.deallocatePin(temperaturePin);
@@ -271,7 +278,6 @@ class UsermodTemperature : public Usermod {
// initialise
setup();
}
DEBUG_PRINTLN(F(" config (re)loaded."));
}
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features
return !top[FPSTR(_parasite)].isNull();