Can I use const chars and integers together?

I’d like to send a tweet using ethernet shield and found an example code in library. But it only allows me to send a predetermined const char such as this: “Hello World”. I’d like to squeeze some integers in between the text. I have a DHT22 temp and humidity sensor with me and I’d like to send a tweet like “My room humidity is: 50% and my room temp is 23C”. Is there a simple way to use both integers and const chars to feed my tweet? Here’s the example code btw:

// Twitter client sketch for ENC28J60 based Ethernet Shield. Uses 
// arduino-tweet.appspot.com as a OAuth gateway.
// Step by step instructions:
// 
//  1. Get a oauth token:
//     http://arduino-tweet.appspot.com/oauth/twitter/login
//  2. Put the token value in the TOKEN define below
//  3. Run the sketch!
//
//  WARNING: Don't send more than 1 tweet per minute!
//  NOTE: Twitter rejects tweets with identical content as dupes (returns 403)

#include <EtherCard.h>

// OAUTH key from http://arduino-tweet.appspot.com/
#define TOKEN   "Insert-your-token-here"

// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

const char website[] PROGMEM = "arduino-tweet.appspot.com";

static byte session;

byte Ethernet::buffer[700];
Stash stash;

static void sendToTwitter () {
  Serial.println("Sending tweet...");
  byte sd = stash.create();

  const char tweet[] = "@solarkennedy the test Twitter sketch works!";
  stash.print("token=");
  stash.print(TOKEN);
  stash.print("&status=");
  stash.println(tweet);
  stash.save();
  int stash_size = stash.size();

  // Compose the http POST request, taking the headers below and appending
  // previously created stash in the sd holder.
  Stash::prepare(PSTR("POST http://$F/update HTTP/1.0" "\r\n"
    "Host: $F" "\r\n"
    "Content-Length: $D" "\r\n"
    "\r\n"
    "$H"),
  website, website, stash_size, sd);

  // send the packet - this also releases all stash buffers once done
  // Save the session ID so we can watch for it in the main loop.
  session = ether.tcpSend();
}

void setup () {
  Serial.begin(57600);
  Serial.println("\n[Twitter Client]");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println(F("Failed to access Ethernet controller"));
  if (!ether.dhcpSetup())
    Serial.println(F("DHCP failed"));

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  if (!ether.dnsLookup(website))
    Serial.println(F("DNS failed"));

  ether.printIp("SRV: ", ether.hisip);

  sendToTwitter();
}

void loop () {
  ether.packetLoop(ether.packetReceive());

  const char* reply = ether.tcpReply(session);
  if (reply != 0) {
    Serial.println("Got a response!");
    Serial.println(reply);
  }
}

I'd like to squeeze some integers in between the text. I have a DHT22 temp and humidity sensor with me and I'd like to send a tweet like "My room humidity is: 50% and my room temp is 23C".

Have a look at the printf() function.

UKHeliBob:
Have a look at the printf() function.

Or the sprintf() function.

That too, of course :slight_smile:

Or better still, the sprintf_P function

Or even snprintf_P.

Do I have to replace some files in the Arduino directories or something to use these ?

No

printf/snprint is a bit tricky to use the first time.

Something like this should give you the general idea (untested):

   byte humidity = 50;
   byte temperature = 34;
   char buffer[60]; // large enough to hold the whole string
   snprintf(buffer, sizeof(buffer), "My room humidity is: %d%% and my room temp is %dC", humidity, temperature); // format the string we want to send
   stash.print(buffer);

FYI, although printf() is neat, you can do what you are asking for with consecutive Serial.print() calls.

To add to aarg’s point, printf and his friends will use up a lot more program memory that consecutive Serial.print(…) calls.

I stumbled on this a while ago which gives you the best of both worlds:
http://arduiniana.org/libraries/streaming/

It would let you write:

Serial << F("My room temperature is ") << Temperature << F("°C, and the humidity is ") << Humidity << "%";
Serial.println();

The F("…") will save you more program memory (right at the end).

Paul_Martinsen:
I stumbled on this a while ago which gives you the best of both worlds:
Streaming | Arduiniana

Yes, it should be included in the IDE releases.

arduinodlb:
printf/snprint is a bit tricky to use the first time.

Something like this should give you the general idea (untested):

   byte humidity = 50;

byte temperature = 34;
  char buffer[60]; // large enough to hold the whole string
  snprintf(buffer, sizeof(buffer), "My room humidity is: %d%% and my room temp is %dC", humidity, temperature); // format the string we want to send
  stash.print(buffer);

Sprintf worked great, thanks. One last thing though. In my region "%" sign comes before the number so to do that I played with characters but all I could do was breaking the code. How do I do that?

Use two %'s: "%%%d" will print a percent and then the integer.

KeithRB:
Use two %'s: “%%%d” will print a percent and then the integer.

It only print the number “4”. I’m sure the sensor reports a higher humidity value becaue 4 is too low. So the code is probably omitting the last digit.

So the code is probably omitting the last digit.

Well, it's your code, and you didn't post it, so, good luck fixing it.

cagancelik:
It only print the number “4”. I’m sure the sensor reports a higher humidity value becaue 4 is too low. So the code is probably omitting the last digit.

Not likely.

But look here to see how to format printf/snprintf strings:
http://www.cplusplus.com/reference/cstdio/printf/

I am in New Mexico. 4 is about right. 8^)