Understanding the evil String()


i have working wood boiler aquastat,  not trying add ethernet report temperature of boiler website.   i have working, regulating , logging temperatures want.   however....

i reading evils of arduino string  hoping didn't cause myself problems down road.

every 5 min code calls httprequest() function  function uses string() extensively convert numbers , concatenate strings.

my questions this:
because called function arduino release memory after function finished?

is there better way build post string , or should not using string() @ all?


thank you




below function:

code: [select]


//my variables
float mtemp = 0.0;
long mintemp = 165;
long maxtemp = 180;
long overheat = 200;
float maxhitemp = 0.0;
float maxlowtemp = 250.0;
long overheatcnt = 0;
boolean overheatswtch = false;
boolean fanstate = true;

void httprequest() {
  client.stop();

  string putstring;
  putstring = "get /boilerset.cfm";
  putstring = putstring + "?curtemp=" + string(mtemp);
  putstring = putstring + "&mintemp=" + string(maxlowtemp);
  putstring = putstring + "&maxtemp=" + string(maxhitemp);
  putstring = putstring + "&overcnt=" + string(overheatcnt);
  putstring = putstring + "&fanstate=" + string(fanstate);
  putstring = putstring + "&isoverheat=" + string(overheatswtch);
  putstring = putstring + "&setfanon=" + string(mintemp);
  putstring = putstring + "&setfanoff=" + string(maxtemp);
  putstring = putstring + "&setoverheat=" + string(overheat);
  putstring = putstring + " http/1.1";

  serial.println(putstring);
 
  if (client.connect(server, 80)) {
    client.println(putstring);
    client.println("host: www.mydomain.com");
    client.println("user-agent: arduino-ethernet");
    client.println("connection: close");
    client.println();
  }
  lastconnectiontime = millis();
}

most of recommend not use strings @ all. cause memory problems , program crashes arduinos.

the sprintf() function easy use , far more versatile using strings format numbers.

an exception default sprintf() not support floating point numbers on arduino. can enable option @ cost of program memory, or use dtostrf() format them instead (or not use floats @ all, preferred option sensor data).


Arduino Forum > Using Arduino > Programming Questions > Understanding the evil String()


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor