Page 1 of 1

ESPAsyncWebServer and C++Strings

Posted: Fri Mar 13, 2020 8:24 am
by franck102
Hi all,

After some googling around I settled on using the https://github.com/me-no-dev/ESPAsyncWebServer library to implement a small web server for a heat regulation unit.

One concern however is that the template API of that library uses C++ Strings all around, I am wondering if that won't lead to memory issues, and how to best avoid them?

After reading this page: https://cpp4arduino.com/2018/11/21/eigh ... ently.html
I came up with the approach below... am I on the right track?

Thanks!
Franck

Code: Select all

String RegulControl::getvar(const String &var)
{
    if (var == "REGUL_MODE") {
        switch (_state.mode) {
            case RegulMode::REGUL:
                return F("REGUL");

            case RegulMode::SAFETY:
                return F("SAFETY");

            case RegulMode::FORCED:
                String result = F("FORCED[");
                switch (_state.forced) {
                    case ForcedState::OFF:
                        result += F("OFF]");
                        break;
                    case ForcedState::F1:
                        result += F("1h]");
                        break;
                    case ForcedState::F2:
                        result += F("2h]");
                        break;
                    case ForcedState::F4:
                        result += F("4h]");
                        break;
                    case ForcedState::F8:
                        result += F("8h]");
                        break;
                    case ForcedState::F24:
                        result += F("24h]");
                        break;
                    case ForcedState::PERM:
                        result += F("PERM]");
                        break;
                }
                return result;
        }
    }
    return var;
}
                      	

Re: ESPAsyncWebServer and C++Strings

Posted: Fri Mar 13, 2020 3:15 pm
by ESP_Sprite
Why do you think that would lead to memory issues?

Re: ESPAsyncWebServer and C++Strings

Posted: Fri Mar 13, 2020 4:07 pm
by franck102
From the numerous posts about using std::String & malloc on chips with limited memory... I come from the Arduino, is this not an issue with the ESP32?

Here is one example: https://hackingmajenkoblog.wordpress.co ... o-strings/

I have already modifed my previous code to avoid using the String + operator, am I wasting energy?

Code: Select all

static char result[16];

    if (var == "REGUL_MODE") {
        switch (_state.mode) {
           ...
            case RegulMode::FORCED:
--->>         strcpy(result, "FORCED[");
                switch (_state.forced) {
                    case ForcedState::OFF:
--->>                 strcat(result, "OFF]");
                        break;
           ...
           return result;

Re: ESPAsyncWebServer and C++Strings

Posted: Sat Mar 14, 2020 9:48 am
by ESP_Sprite
I'd not worry too much about strings on the ESP32 unless you really run into memory problems. The article you mention is talking about an Arduino with about 2K of RAM; allocating a string takes up a significant amount of room. The ESP32 has about 500K of RAM, more than 200x as much, so the space taken up by strings is much less significant.

Re: ESPAsyncWebServer and C++Strings

Posted: Sat Mar 14, 2020 3:36 pm
by lbernstone
If you localize your Strings and don't pass them between functions, you will have no issues. If you will have huge strings, either store them on SD and deliver JIT, or get an esp32 with PSRAM and store them there.