stop crashing when ETH used and Analog Sensor is pin 21/SDA or 22/SCC (I2C conflict). Show when its a bad GPIO in log.

This commit is contained in:
proddy
2024-06-27 14:52:48 +02:00
parent 320035fd72
commit 139d32c431
2 changed files with 22 additions and 6 deletions

View File

@@ -57,10 +57,12 @@ void AnalogSensor::reload(bool get_nvs) {
remove_ha_topic(sensor.type(), sensor.gpio()); remove_ha_topic(sensor.type(), sensor.gpio());
sensor.ha_registered = false; sensor.ha_registered = false;
} }
if (!analog_enabled_) { if (!analog_enabled_) {
sensors_.clear(); sensors_.clear();
return; return;
} }
// load the list of analog sensors from the customization service // load the list of analog sensors from the customization service
// and store them locally and then activate them // and store them locally and then activate them
EMSESP::webCustomizationService.read([&](WebCustomization & settings) { EMSESP::webCustomizationService.read([&](WebCustomization & settings) {
@@ -69,7 +71,7 @@ void AnalogSensor::reload(bool get_nvs) {
// update existing sensors // update existing sensors
bool found = false; bool found = false;
for (const auto & sensor : settings.analogCustomizations) { // search customlist for (const auto & sensor : settings.analogCustomizations) { // search customlist
if (System::is_valid_gpio(sensor.gpio) && sensor_.gpio() == sensor.gpio) { if (sensor_.gpio() == sensor.gpio) {
// for output sensors set value to new start-value // for output sensors set value to new start-value
if ((sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT) if ((sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT)
&& (sensor_.type() != sensor.type || sensor_.offset() != sensor.offset || sensor_.factor() != sensor.factor)) { && (sensor_.type() != sensor.type || sensor_.offset() != sensor.offset || sensor_.factor() != sensor.factor)) {
@@ -94,14 +96,14 @@ void AnalogSensor::reload(bool get_nvs) {
for (const auto & sensor : settings.analogCustomizations) { for (const auto & sensor : settings.analogCustomizations) {
bool found = false; bool found = false;
for (const auto & sensor_ : sensors_) { for (const auto & sensor_ : sensors_) {
if (System::is_valid_gpio(sensor.gpio) && sensor_.gpio() == sensor.gpio) { if (sensor_.gpio() == sensor.gpio) {
found = true; found = true;
} }
} }
if (!found) { if (!found) {
if (!System::is_valid_gpio(sensor.gpio)) { // if (!System::is_valid_gpio(sensor.gpio)) {
continue; // continue;
} // }
sensors_.emplace_back(sensor.gpio, sensor.name, sensor.offset, sensor.factor, sensor.uom, sensor.type); sensors_.emplace_back(sensor.gpio, sensor.name, sensor.offset, sensor.factor, sensor.uom, sensor.type);
sensors_.back().ha_registered = false; // this will trigger recreate of the HA config sensors_.back().ha_registered = false; // this will trigger recreate of the HA config
if (sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT) { if (sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT) {
@@ -130,6 +132,14 @@ void AnalogSensor::reload(bool get_nvs) {
// activate each sensor // activate each sensor
for (auto & sensor : sensors_) { for (auto & sensor : sensors_) {
sensor.ha_registered = false; // force HA configs to be re-created sensor.ha_registered = false; // force HA configs to be re-created
// first check if the GPIO is valid. If not, force set it to disabled
if (!System::is_valid_gpio(sensor.gpio())) {
LOG_WARNING("Bad GPIO %d for Sensor %s", sensor.gpio(), sensor.name().c_str());
sensor.set_type(AnalogType::NOTUSED);
continue;
}
if (sensor.type() == AnalogType::ADC) { if (sensor.type() == AnalogType::ADC) {
LOG_DEBUG("ADC Sensor on GPIO %02d", sensor.gpio()); LOG_DEBUG("ADC Sensor on GPIO %02d", sensor.gpio());
// analogSetPinAttenuation does not work with analogReadMilliVolts // analogSetPinAttenuation does not work with analogReadMilliVolts
@@ -261,6 +271,7 @@ void AnalogSensor::measure() {
} }
} }
} }
// poll digital io every time with debounce // poll digital io every time with debounce
// go through the list of digital sensors // go through the list of digital sensors
for (auto & sensor : sensors_) { for (auto & sensor : sensors_) {

View File

@@ -440,6 +440,11 @@ bool System::is_valid_gpio(uint8_t pin, bool has_psram) {
#endif #endif
return false; // bad pin return false; // bad pin
} }
// extra check for pins 21 and 22 (I2C) when ethernet is enabled
if ((EMSESP::system_.ethernet_connected()) && (pin >= 21 && pin <= 22)) {
return false; // bad pin
}
return true; return true;
} }