Go Down

Topic: Replace String by char in for loop... (Read 83 times) previous topic - next topic


Hello all,
My project upload several fields in thingspeak with sim800 with an arduino UNO.
I'm using String to generate the GET request.

I'm facing issues when using several String with operation in my code.
I guess due to memory overflow, String variable are not generated correctly.
I have read that String should not be used and it is recommended to use C string.

I'm using a for loop to generate my String and do not know how to do with C string.
Is it possible to do it ? 

Any example is highly welcome.

Many thanks in advance for your help

Below my test code to generate the GET request to sim800 with for loop

Code: [Select]
const int nbR = 4;
float station_Temp[nbR];
float station_Hum[nbR];
float station_Pds[nbR];
float station_Met[6];

void setup() {

  String strPoids="GET https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxx";
  String strTemp="GET https://api.thingspeak.com/update?api_key=yyyyyyyyyyyyyyy";
  String strHum="GET https://api.thingspeak.com/update?api_key=zzzzzzzzzzzzzzz";
  String strMet="GET https://api.thingspeak.com/update?api_key=aaaaaaaaaaaaaaa";

    for(int i=0; i<nbR; i++)
    strPoids += "&field" + String(i+1) + "=" + String(station_Pds[i]);
    strTemp += "&field" + String(i+1) + "=" + String(station_Temp[i]);
    strHum += "&field" + String(i+1) + "=" + String(station_Hum[i]);   



void loop() {

The output in serial monitor when it is OK
Code: [Select]
17:51:00.710 -> GET https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxx&field1=0.00&field2=0.00&field3=0.00&field4=0.00
17:51:00.812 -> GET https://api.thingspeak.com/update?api_key=yyyyyyyyyyyyyyy&field1=0.00&field2=0.00&field3=0.00&field4=0.00
17:51:00.916 -> GET https://api.thingspeak.com/update?api_key=zzzzzzzzzzzzzzz&field1=0.00&field2=0.00&field3=0.00&field4=0.00

The output in serial monitor when it is NG
Code: [Select]
17:51:00.710 -> GET https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxx&field1=0.00&field2=0.00&field3=0.00&field4=0.00
17:51:00.812 -> GET https://api.thingspeak.com/update?api_key=yyyyyyyyyyyyyyy&field1=0.00&field2=0.
17:51:00.916 -> GET https://api.thingspeak.com/update?api_key=zzzzzzzzzzzzzzz&field1=0.00&field2=


Here is an example of how to do one line.  The other lines would be done similarly.
Code: [Select]
  Serial.print(F("GET https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxx"));
  for (int i = 0; i < nbR; i++)
    Serial.print(i + 1);

The 'F()' macro is a trick to avoid copying a string constant from FLASH/PROGMEM to RAM just so it can be printed.  This saves a lot of RAM when many string constants are printed.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)


Hello John,
Many thanks for your reply.
Why I made things complicated when you can made them easy....
I have just modify my code with your suggestion and it works well.

Go Up