khoih-prog / ESPAsync_WiFiManager_Lite

Library using AsyncWebServer to configure MultiWiFi/Credentials at runtime for ESP32 (including ESP32-S2 and ESP32-C3) and ESP8266 boards. You can also specify DHCP HostName, static AP and STA IP. Use much less memory compared to full-fledge WiFiManager. Config Portal will be auto-adjusted to match the number of dynamic custom parameters. Optional default Credentials to be autoloaded into Config Portal to use or change instead of manually input. Credentials are saved in LittleFS, SPIFFS or EEPROM. New powerful-yet-simple-to-use feature to enable adding dynamic custom parameters from sketch and input using the same Config Portal. Double or MultiDetectDetector as well as Virtual Switches feature permits entering Config Portal as requested.
MIT License
57 stars 14 forks source link

make loadDynamicData() and saveDynamicData() public methods #20

Closed hmueller01 closed 1 year ago

hmueller01 commented 1 year ago

See discussion #17. Also added a flag to not load data twice, if already loaded. Also renamed CREDENTIALS_FILENAME and wm_cred.dat to DYNAMIC_DATA_FILENAME and wm_dynamic.dat to align with method names (the credentials are stored in CONFIG_FILENAME). I tested/compiled the code on ESP8266 with LittleFS and EEPROM. What I did not check is ESP32, as I do not have the dev env.

khoih-prog commented 1 year ago

Hi @hmueller01

Thanks for your precious time to make the PR.

But sorry I think this way is too drastic, and requires much more time to test, verify to be sure OK and not breaking anything.

I suggest you just create 2 new public functions, such as

bool extLoadDynamicData(bool forceLoad = false)
{
  // Condition to be satisfied before calling this
  if (checking_everything_OK)
  {
    return loadDynamicData(forceLoad);
  }
  else
  {
    ESP_WML_LOGERROR("Some error");
    return false;
  }
}

void extSaveDynamicData()
{
  // similar checking, etc before calling  saveDynamicData()
  // Condition to be satisfied before calling this
  if (checking_everything_OK)
  {
    saveDynamicData();
  }
  else
  {
    ESP_WML_LOGERROR("Some error");
  }
}

then you can do whatever you'd like without worrying breaking anything.

hmueller01 commented 1 year ago

It took me much time to implement the feature in way of this PR. For the moment it works good for me so I do not like to do more work on this. I'll keep my branch and can merge to your main any time.

hmueller01 commented 1 year ago

BTW your suggestion won't work. I added hadDynamicData to avoid loading dynamic data a second time. This needs to be located in your private methods. And this is important to avoid loading data a second time while changed and possibly not yet saved data are in RAM. So the main change needs to be done anyway.

I'll give you an example:

  // host_name is in dynamic data and is empty per default
  m_wifi_manager->loadDynamicData();
  if (os_strlen(host_name) == 0) {
    Serial.println(F("host_name was empty, setting default host name"));
    os_sprintf(host_name, "ESP-%08X", ESP.getChipId());
    m_wifi_manager->saveDynamicData(); // if you forget this before begin() changes are lost without hadDynamicData flag!
  }
  m_wifi_manager->setConfigPortal(host_name, "password");
  // Set customized DHCP HostName
  m_wifi_manager->begin(host_name);