ESPAsyncWebServer and C++Strings

franck102
Posts: 8
Joined: Thu Mar 12, 2020 5:45 pm

ESPAsyncWebServer and C++Strings

Postby franck102 » Fri Mar 13, 2020 8:24 am

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;
}
                      	

ESP_Sprite
Posts: 9043
Joined: Thu Nov 26, 2015 4:08 am

Re: ESPAsyncWebServer and C++Strings

Postby ESP_Sprite » Fri Mar 13, 2020 3:15 pm

Why do you think that would lead to memory issues?

franck102
Posts: 8
Joined: Thu Mar 12, 2020 5:45 pm

Re: ESPAsyncWebServer and C++Strings

Postby franck102 » Fri Mar 13, 2020 4:07 pm

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;

ESP_Sprite
Posts: 9043
Joined: Thu Nov 26, 2015 4:08 am

Re: ESPAsyncWebServer and C++Strings

Postby ESP_Sprite » Sat Mar 14, 2020 9:48 am

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.

lbernstone
Posts: 669
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESPAsyncWebServer and C++Strings

Postby lbernstone » Sat Mar 14, 2020 3:36 pm

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.

Who is online

Users browsing this forum: No registered users and 71 guests