sblantipodi / arduino_bootstrapper

Utility classes for bootstrapping Arduino projects with Wifi management, OTA upload management, memory management, MQTT and queue management. (ESP8266/ESP32 ready)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bad result from BootstrapManager::readValueFromFile for float values

Pronoe opened this issue · comments

In the frame of my functional tests for the branch https://github.com/Pronoe/arduino_bootstrapper/tree/fix-missing-init-and-file-close-ESP32 I have identified an unattended result from the BootstrapManager::readValueFromFile for float value.
The test json file is:

{
  "STRING_VALUE": "test readValueFromFile",
  "INT_VALUE": 50,
  "FLOAT_VALUE": 200.3
}

The read file is the same and is correct.
Test result is:

intValue: 50 read value: 50 OK
stringValue: test readValueFromFile read value: test readValueFromFile OK
floatValue: 200.30 read value: 200 KO

The involved code in BootstrapManager::readValueFromFile is:

JsonVariant answer = jsonDoc[paramName];
        if (answer.is<char*>()) {
          returnStr = answer.as<String>();
        } else {
          auto returnVal = answer.as<int>();
          returnStr = String(returnVal);
        }

and does not provide the processing of float values.
Actually, this is not a showstopper for my own application because I don't use this function but only readSPIFFS but it could be for some users.

I propose the following modified code:

  returnStr = answer.as<String>();
  if (!answer.is<const char*>()) {
    if (returnStr.indexOf('.') >= 0)
    {
      auto returnVal = answer.as<float>();
      returnStr = String(returnVal, 6);
      Serial.print("float - ");
    }
    else
    {
      auto returnVal = answer.as<int>();
      returnStr = String(returnVal);
      Serial.print("int - ");
    }
  }

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days