sprintf not working

I have the following code as part of a large programme that reads in 2 float values from a DHT11 sensors and another int value from an LDR connected to A0.

The code that reads in the values is as follows:

float h = dht.readHumidity();
  float t = dht.readTemperature();
  int l = analogRead(A0);

My aim is to send this data to a webserver as part of an HTTP GET request (i.e. a string).

The problem I have is that when I use the following it adds 4k onto my sketch size (too big):

 sprintf(buffer, "GET /submit.php?temp=%.1f&hum=%.1f&light=%d HTTP/1.0", t, h, l);

This method works and the data gets to the server however it is too big for the sketch.

If I convert h,t and l to a string beforehand the sketch size drops by 4k:

String temperature = String((int) t);
    String humidity = String((int) h);
    String light = String((int) l);

sprintf(buffer, "GET /submit.php?temp=%s&hum=%s&light=%s HTTP/1.0", temperature, humidity, light);

The problem, however is that the second method doesn't work, the data doesn't get to the server.

Does anyone know: a) why the second method isn't working? b) why the first method is considerably larger in sketch size?

Thanks

a) why the second method isn't working?

The address of a String is not a string. If you are happy with ints instead of floats, change the variable types to int, and use %d. Don't even think of using Strings.

If you need the fractional portion, use dtostrf() to convert the float to a string.

You do NOT need to send all the data as one string. It is more efficient, but efficiency isn't everything.

b) why the first method is considerably larger in sketch size?

Does it matter? The %f format specifier isn't supported, anyway.

Yes size does matter here as I dropped the string class for sprintf to reduce the sketch size.

I am nearly at the limit.

Yes size does matter here as I dropped the string class

There is NOT a string class.

I am nearly at the limit.

So am I, with people think that string and String are the same thing (they are NOT) or that they are interchangeable (they are NOT).

As in I've changed this:

String request = "GET "+ repository + "submit.php?temp=" + temperature + "&hum=" + humidity + " HTTP/1.0";

for sprintf

As in I've changed this...for sprintf

That replaced the String class, not the string class. Case matters!

The %f format specifier is not supported, so:

sprintf(buffer, "GET /submit.php?temp=%.1f&hum=%.1f&light=%d HTTP/1.0", t, h, l);

would have produced something like: "GET /submit.php?temp=?&hum=?&light=12 HTTP/1.0" in buffer.

Given that that is useless, please explain again how the amount of code needed to produce useless results matters.

Ok then:

sprintf(buffer, "GET /submit.php?temp=%d&hum=%d&light=%d HTTP/1.0", t, h, l);

That would work but it uses a huge amount of memory.

You haven’t answered my original question.

String temperature = String((int) t);
    String humidity = String((int) h);
    String light = String((int) l);

sprintf(buffer, "GET /submit.php?temp=%s&hum=%s&light=%s HTTP/1.0", temperature, humidity, light);

That uses 4k less memory - why doesn’t it work?

I don’t have room for an extra 4k on the chip.

That uses 4k less memory - why doesn't it work?

I did answer the question. You are providing a String object as the thing that the %s format specifier is to deal with. A String object IS NOT A string. Never has been! Never will be! The code does work, except that it doesn't produce the results you expect. It does produce the results I expect.

It is no different from providing a float to be output using %s or providing a string to be output using %d. If the type of variable does not match the format specifier, the results are undefined.

You still haven't explained why you think you need to generate the output using a single string. The output could be generated with several calls to client.print() without needing to generate a single string, and the output COULD include floats.