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

View File

@ -408,8 +408,7 @@ function loadPresets(callback = null)
clearErrorToast(); clearErrorToast();
pJson = json; pJson = json;
populatePresets(); populatePresets();
// Create UI update WS handler reconnectWS();
if (!ws && lastinfo.ws > -1) setTimeout(makeWS,1000);
if (callback) callback(); if (callback) callback();
}) })
.catch(function (error) { .catch(function (error) {
@ -435,8 +434,7 @@ function loadPalettes(callback = null)
clearErrorToast(); clearErrorToast();
lJson = Object.entries(json); lJson = Object.entries(json);
populatePalettes(); populatePalettes();
// Create UI update WS handler reconnectWS();
if (!ws && lastinfo.ws > -1) setTimeout(makeWS,1000);
if (callback) callback(); if (callback) callback();
}) })
.catch(function (error) { .catch(function (error) {
@ -566,8 +564,7 @@ function loadInfo(callback=null)
pmt = json.fs.pmt; pmt = json.fs.pmt;
showNodes(); showNodes();
populateInfo(json); populateInfo(json);
// Create UI update WS handler reconnectWS();
if (!ws && json.ws > -1) setTimeout(makeWS,1000);
reqsLegal = true; reqsLegal = true;
if (callback) callback(); if (callback) callback();
}) })
@ -762,8 +759,7 @@ function loadNodes()
.then(json => { .then(json => {
clearErrorToast(); clearErrorToast();
populateNodes(lastinfo, json); populateNodes(lastinfo, json);
// Create UI update WS handler reconnectWS();
if (!ws && lastinfo.ws > -1) setTimeout(makeWS,1000);
}) })
.catch(function (error) { .catch(function (error) {
showToast(error, true); showToast(error, true);
@ -1083,6 +1079,10 @@ function makeWS() {
} }
} }
function reconnectWS() {// Create UI update WS handler
if (!ws && lastinfo.ws > -1) setTimeout(makeWS,1000);
}
function readState(s,command=false) function readState(s,command=false)
{ {
if (!s) return false; if (!s) return false;
@ -1209,8 +1209,7 @@ function requestJson(command, rinfo = true, verbose = true, callback = null)
var s = json.state ? json.state : json; var s = json.state ? json.state : json;
readState(s); readState(s);
reqsLegal = true; reqsLegal = true;
// Create UI update WS handler reconnectWS();
if (!ws && lastinfo.ws > -1) setTimeout(makeWS,1000);
if (callback) callback(); if (callback) callback();
}) })
.catch(function (error) { .catch(function (error) {

File diff suppressed because it is too large Load Diff