Adding a variable to a message sent to Twitter

I'm using the Neocat twitter library.

I wanted to add a variable to the messages I'm sending to Twitter so that Twitter won't reject on the basis that they're not unique.

        Serial.println("connecting to Twitter...");
        if (twitter.post("Button Pressed @")) {
          int status = twitter.wait();
          if (status == 200) {
            Serial.println("Posted!");
          } 
          else {
            Serial.print("failed : code ");
            Serial.println(status);
          }
        } 
        else {
          Serial.println("connection failed.");
        }

But no matter how many brackets, + signs or quotation marks I use, I can't find the magic combination that'll allow me to do:

Serial.println("connecting to Twitter..."); if (twitter.post("Button 1 pressed @ " RTC.now() )) { int status = twitter.wait(); if (status == 200) { Serial.println("Posted!"); } else { Serial.print("failed : code "); Serial.println(status); } } else { Serial.println("connection failed."); }

Any tips? Thanks!

But no matter how many brackets, + signs or quotation marks I use, I can't find the magic combination that'll allow me to do

That's because brackets, + signs, quotation marks or voodoo are not the answer. There is no "glue these two things that are not both strings together as a string" function in C or C++.

There is a sprintf() function that allows you to create a formatted string from strings and non-string variables.

A little time search would have told you this.

PaulS: There is no "glue these two things that are not both strings together as a string" function in C or C++. There is a sprintf() function that allows you to create a formatted string from strings and non-string variables.

hmm..

OK, I've got the following:-

char x = RTC.now();
char button1msg[162];
sprintf(button1msg, "ID%d: tweet message", x);

It almost works, but tells me "error: cannot convert 'DateTime' to 'char' in initialization" on compile. I can't find what type of variable DateTime is anywhere - otherwise I'd know how to declare it. I tried word, unsigned long, char, int, etc - same result.

P.S. I don't know why ID%d inserts x.

It's telling you that RTC.now() returns a DateTime type, which it can't convert to a char.

Are you using RTClib.h? If you are, you might want to use year(), month(), day(), hour(), minute() and second().

P.S. I don't know why ID%d inserts x.

The format specifier needs to match the variable type that is to be formatted. %d is the format specifier for int. x is a char. They don't match.

1

int y = now.hour();
int x = now.minute();
char button1msg[162];
sprintf(button1msg, "Button 1 registered @ Hour %d, Minute %d", y, x);

2

DateTime y = RTC.now();
char button1msg[162];
sprintf(button1msg, "Button 1 registered @ %d", y);

1 works but 2 compiles but then hangs the loop.

sprintf(button1msg, "Button 1 registered @ %d", y);

The variable y is NOT an int.

PaulS: The variable y is NOT an int.

mmm, I got that, but I don't know what type of variable DateTime is? I think it is itself a sprintf from the Ladyada RTClib.h library. So that means I'm should use the below, right?

int v = now.day();
int w = now.month();
int x = now.year();
int y = now.minute();
int z = now.hour();
char button1msg[162];
sprintf(button1msg, "Button 1 registered @ %d/ %d/ %d, %d:%d", v, w, x, y, z);

So that means I'm should use the below, right?

There is no reason to create a bunch of intermediate variables.

sprintf(button1msg,
   "Button 1 registered @ %d/ %d/ %d, %d:%d",
   now.day(),
   now.month(),
   now.year(),
   now.hour(),
   now.minute());

This way, it is clear what is going where (you had hour and minute reversed).

but I don't know what type of variable DateTime is?

DateTime is not a variable. It IS a type. The type of now (the variable) IS DateTime.

DateTime is a class. The variable now holds an instance of that class.