Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wierd result for get data #8

Closed
Hexagon opened this issue Dec 12, 2011 · 3 comments
Closed

Wierd result for get data #8

Hexagon opened this issue Dec 12, 2011 · 3 comments

Comments

@Hexagon
Copy link

Hexagon commented Dec 12, 2011

I don't know if i'm doing something wrong, but im getting really wierd results

void report_temp (float temp, int unit) {

  char buf[16];
  char buf_full[32];                                        // 32 == Wierdness, 128 is working

  // Construct GET payload
  String s = "t=";
  s = s + String(dtostrf(temp,7,3,buf));
  s = s + "&u=";
  s = s + String(unit);

  s = s.replace(" ","");

  // Convert s to char *
  s.toCharArray(buf_full, sizeof(buf_full));

  Serial.println(buf_full);        // <--- Always looks fine wether i use 32 or 128

  ether.browseUrl(PSTR(SITE_REPORT_URL), buf_full, website, callback);

  }

When i am using 32 as size for buf_full, i get a request that looks like this on server side;

  [13/Dec/2011:00:11:37 +0100] "GET /temp/report.php?ºÙ\x08 HTTP/1.1" 200 19 "-" "-"

When using 128 as buffer size, i get something that makes sense

  [13/Dec/2011:00:47:20 +0100] "GET /temp/report.php?t=23.250&u=0 HTTP/1.1" 200 12 "-" "-"

How is this possible? The resulting string is not more than 16 chars? And when i do Serial.println() it seems fine

Anyways, thanks for a great peice of software :)

// Robin

@jcw
Copy link

jcw commented Dec 13, 2011

I don't know what's happening, but for an ATmega with limited RAM, I'd prefer to use calls with predictable behavior:

// Construct GET payload
strcpy(buf_full, "t=");
strcat(buf_full, dtostrf(temp,7,3,buf));
strcat(buf_full, "&u=";
strcat(buf_full, String(unit)); // or perhaps with itoa?

This is more old-fashioned C, but might work better here.

@thiseldo
Copy link
Contributor

You are using the String object, which as it says, is an object. I've not looked at the implementation, but it has to allocate some memory just to create itself and a few extra bytes for its own use. It would create space for a string of characters of a minimum size so that it doesnt have to keep allocating more memory as the string is added to.

As jcw commented, using a fixed length character buffer and traditional C functions is a better solution for limited memory systems, you know where you are and how many bytes you are using.

Andy

@jcw
Copy link

jcw commented Dec 28, 2011

I'm assuming this issue has been resolved by now. Please re-open if this is not the case.

@jcw jcw closed this as completed Dec 28, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants