[SOLVED] Should I use "String" or 'char arrays' ?

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

[SOLVED] Should I use "String" or 'char arrays' ?

Postby GeorgeFlorian1 » Tue Feb 19, 2019 12:36 pm

Hello there, my dear community !
I am a newbie when it comes to C, C++, Arduino IDE, ESP32 and so on. I do go to an electrical engineering university but they teach us nothing there.

Here is the thing. I've read online that String is too big for such a little RAM memory, and that char is better for the memory and for manipulation. I thought that changing from String to char was easy, but boy was I wrong. I find working with String the most simple thing to do in Arduino IDE, but char kills me.

The sketch connects to a WiFi network, using the stored credentials from a .txt file inside the Flash Memory. It works ! How would you optimize it ? Does String do so much harm ?

The sketch will get bigger than this. This is what I am planning to do:
I am looking to make my little ESP32 do the following:
1 - boot ;
2 - read two (2) lines from a file (SSID and Password), if it has values in that file start in Station Mode and serve a webpage (HTML and CSS from inside the flash memory) ;
2.1 - if the file doesn't exist or it's empty start in Acces Point Mode with "ESP32AP" name and "password123" as password and serve another webpage(HTML and CSS from inside the flash memory) containing two (2) inputs: SSID and Password and a "Connect" Button
2.2 - when clicking on Button it will save the credentials in the above mentioned file, and restart

Code: Select all

#include <SPIFFS.h>
#include <WiFi.h>

String v[2];
 
void setup() {
  
  Serial.begin(115200);

   
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
 
 
    //---------- Read file
    File fileToRead = SPIFFS.open("/inputs.txt");
 
    if(!fileToRead){
        Serial.println("Failed to open file for reading");
        return;
    }
 
    Serial.println("File Content: ");

    int i = 0;
    while(fileToRead.available()){
        String line= fileToRead.readStringUntil('\n');        
        v[i] = line;
        i++;        
        String string2 = String("Linia ") + i + ": " + line;
        Serial.println(string2);
        Serial.println();      
    }
 
    fileToRead.close();
    
    for(i = 0; i<2; i++) {
        Serial.println(v[i]);
      }

  WiFi.begin(v[0].c_str(),v[1].c_str());  

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println((String)"Connected to " + v[0] + " with IP addres: " + WiFi.localIP().toString() );
  
  Serial.println(SPIFFS.exists("/inputs.txt"));

}
 
void loop() {}
Last edited by GeorgeFlorian1 on Wed Mar 13, 2019 2:03 pm, edited 3 times in total.

Denis Brion
Posts: 9
Joined: Tue Feb 19, 2019 4:45 pm

Re: Should I use "String" or 'char arrays' ?

Postby Denis Brion » Tue Feb 19, 2019 6:53 pm

you should not worry that much about optimizing resources, as ESP32 are way bigger and faster than 8 bits arduino.
Both String and char arrays are comfortable for me, and conversion is very fast using
.c_str (ask arduino help : is in your IDE): creates a pointer, no RAM displacement (very fast);
toCharArray (makes a copy in RAM of the String) considered as worse in [url][https://arduino.stackexchange.com/quest ... rarray/url]

IMO, if you are accustomed to plain C, the C family is enough for you.... but less flexible.

if you prefer C++ (habits, for self teaching, long term strategy ) you should try to stick to C++ and do conversions only if needed (ex :wifi connection a null terminated char array is necessary)
and, though conversions are easy, code cluttered with conversions might become difficult to read after some months...

Who is online

Users browsing this forum: No registered users and 47 guests