How to write the value of a variable in text

float tempC=request.temp();
gprsTest.sendSMS(PHONE_NUMBER,"Temperature is ");

I dont want to split it because i will send 2 messages. How is that possible?

strcat()

Or:

char buf[50]; // Make this more than long enough to prevent buffer overflow

sprintf(buf, "Temperature is %d ", tempC); gprsTest.sendSMS(PHONE_NUMBER, buf);

I like this because you can add more parameters and it still works fine.

sprintf(buf, "Temperature is %d and humidity is %d%%", tempC, humidP); // the double % inserts a single %

sailorgreg: Or:

char buf[50]; // Make this more than long enough to prevent buffer overflow

sprintf(buf, "Temperature is %d ", tempC); gprsTest.sendSMS(PHONE_NUMBER, buf);

I like this because you can add more parameters and it still works fine.

sprintf(buf, "Temperature is %d and humidity is %d%%", tempC, humidP); // the double % inserts a single %

Unless either value is a 'float'. Then 'sprintf()' won't work. 'dtostrf()' is needed for 'float' types. (Edit: Then aarg's 'strcat()' to build the full string.) OK for 'int' though, as in your example.

Hi dpiralis,

try this:
char buf[50]; // Make this more than long enough to prevent buffer overflow

sprintf(buf, "Temperature is %f ", tempC);
gprsTest.sendSMS(PHONE_NUMBER, buf);

The % are very flexible formatting command to the family of *printf commands of
C.
If you want more of this :slight_smile: take a look here:

Dont be shocked by the shear flood of possibilties.
To feel more confortable with a “more than long enough buffer” (RAM is holy on a ATmega…) you
can limit the precision of the decimals with the %f-command.

Good luck!
Have a nice weekend!
Cheers
mcc

The output is Temperature is ? it doesn't work

Use dtostrf. %f isn't implemented.

How to use dtostrf ? If i do I have to use strcat, that means on every loop it will add and add and on the same string the result with be like "temp is 31.5" (on first loop), "temp is 31.531.5"(on second loop), "temp is 31.531.531.5"(on third loop).

Google "dtostrf Arduino"

dtostrf convert a float to string. But i dont want to send only the converted float I want to send a text + float

I want to send a text + float

That's not what the thread title suggests.

I want so send message with this text "temperature is " not just “tempC”

I'm sorry, I'm not seeing g what the problem is here. If tempC is a float (I'm not convinced it is), then using dtostrf will convert it to a C string. After that use strcat, or sprintf. Or just print one string after the other.

As AWOL says, (and I said in reply #3), 'dtostrf()' and 'strcat()'. I also used 'strcpy_P()' to avoid wasting RAM. Run this example:-

void setup()
{
    Serial.begin(115200);
    
    char buf[20];
    char tBuf[6];
    float tempC = 31.5;
    strcpy_P(buf, PSTR("Temperature is "));
    dtostrf(tempC, 0, 1, tBuf);  // 1 decimal place
    strcat(buf, tBuf);

    Serial.println(buf);  // Prints: Temperature is 31.5
}

void loop(){}

Edit:

If i do I have to use strcat, that means on every loop it will add and add and on the same string the result with be like "temp is 31.5" (on first loop), "temp is 31.531.5"(on second loop), "temp is 31.531.531.5"(on third loop).

Surely you're smart enough to think of a way around this?