[SOLVED] Should I use "String" or 'char arrays' ?
Posted: 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 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() {}