Arduino. How to create long char array/string? Need to make http request

981 views Asked by At

The problem is that Arduino (ATmega328p) limits string length. I need to generate request: (char array) to send to the SIM900 library.

"GET /send.php?gpsCoords="+urlencode(gpsCoords)+"&humidity="+urlencode(humidity)+"&temperature2="+urlencode(temperature2)+"&pressure="+urlencode(pressure)+"&altitude="+urlencode(altitude)+"&temperatureGround="+urlencode(temperatureGround)+"&temperature="+urlencode(temperature)+" HTTP/1.0\r\nHost: mysite.org\r\n\r\n";

So I'm creating a string with that data and it becomes empty as it is too long:

String http_cmd_str = "GET /send.php?gpsCoords="+urlencode(gpsCoords)+"&humidity="+urlencode(humidity)+"&temperature2="+urlencode(temperature2)+"&pressure="+urlencode(pressure)+"&altitude="+urlencode(altitude)+"&temperatureGround="+urlencode(temperatureGround)+"&temperature="+urlencode(temperature)+" HTTP/1.0\r\nHost: dangerd.org\r\n\r\n";
Serial.println(http_cmd_str); // RETURNS EMPTY!!!
int str_len = http_cmd_str.length() + 1; 
char http_cmd[str_len];
http_cmd_str.toCharArray(http_cmd, str_len);

Any suggestions?

1

There are 1 answers

0
slash-dev On

You don't need to build the complete GET string and then print it with one statement. Just print the pieces:

Serial.print( F("GET /send.php?gpsCoords=") );
Serial.print( urlencode(gpsCoords) );
Serial.print( "&humidity=" );
  ...
Serial.print( F(" HTTP/1.0\r\nHost: dangerd.org\r\n\r\n") );

And the F macro saves a bunch of RAM, too.

In general, use char arrays instead of the String class to avoid memory and stability problems.