Variable empty when using more than 50% of memory

Hi community,

I am working with arduino uno. Trying to send an http request to a web api.
When I compile the code, it shows this message about memory

"Global variables use 1453 bytes (70%) of dynamic memory, leaving 595 bytes for local variables. Maximum is 2048 bytes."

I have this variable to do the http request:

String postRequest = "POST " + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Accept: " + "/" + "\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/json\r\n" +
"\r\n" + data;

But it is empty when I do the request to the serial. If I remove some code to free some memory, the variable does not get empty.

Any idea what I need to do?

Thanks

Yes I know exactly what you need to do. Post your entire sketch, in code tags.

I have a class APIClient to create the Http request.

bool APIClient::Authenticate(String host, String url, String deviceId, String sshKey, String & token) {    
  String data = "{deviceguid:\"" + deviceId + "\", accesskey:\"" + sshKey + "\"}"; 
  String postRequest = "POST " + url  + " HTTP/1.1\r\n" +
                       "Host: " + host + "\r\n" +
                       "Accept: *" + "/" + "*\r\n" +
                       "Content-Length: " + String(data.length()) + "\r\n" +
                       "Content-Type: application/json\r\n" +
                       "\r\n" + data;    
  WIFIConnection->SendAPIRequest(postRequest, token);
  return true;
}

Then the WIFIConnection method is

bool WIFIConnectionUno::SendAPIRequest(String request, String & token) {
  if (_DEBUG) Serial.println(" - Sending API Request: ");
  String data = "";
  ClearMemory(); 
  
  String command = "AT+CIPSEND=" + request.length();
  if (_DEBUG) Serial.println(request);
  WIFISerial.println(command);
  delay(1000);
  while (WIFISerial.available()) {
    data = WIFISerial.readStringUntil('\n');
  }  
  if (_DEBUG) Serial.println(data);
}

This is the setup method

APIClient apiClient(&wifiConnection);

void setup() {  
  Serial.begin(_PORT_PC);
  Serial.println("WIFI Setup Start ...");
  wifiConnection.Begin();
  wifiConnection.Init();  
  wifiConnection.SetUpWIFI(_SSID, _PASSWORD);  //connects to the WIFI network  
  wifiConnection.GetIP();
  wifiConnection.SetSingleMode();   
  wifiConnection.End();
  Serial.println("WIFI Setup Done ...");  
  delay(2000);  
  Serial.println("GPS Setup Start ...");
  gpsSensor.Begin();
  gpsSensor.End();  
  Serial.println("GPS Setup End ...");
  delay(2000);  
  wifiConnection.Begin();                   
  apiClient.InitHttpRequest(_HOST, _HTTP_PORT); //opens the HTTP connection to the API server   
  authenticated = apiClient.Authenticate(_HOST, _AUTHENTICATE_URL, _DEVICE_ID, _SSH_KEY, returnedToken); //opens the HTTP connection to the API server 
  delay(2000);  
  wifiConnection.End();
}

Your use of prints suggests you are very profligate with your use of RAM.
Try using the F() macro more to free up RAM.

Thank you very much, I started using the PROGMEM and now the String is not getting empty. :slight_smile: