Instructables

Step 5: Arduino + Twitter + Ethernet fun

Picture of Arduino + Twitter + Ethernet fun

A neat character named Neocat made the Arduino Twitter Library. Instead of storing your user ID and password in Arduino code, it issues a token to use the Twitter API. This token can be revoked at any time which is good if you accidentally post your whole code WITH the token to somewhere. Yes. I did that. Oops.

The difficulty I found is that it only accepts char[] arrays as tweets, so a character array must be declared then populated. Since my code generates Strings, I had to convert the String to a character array before passing it off to my Twitter function.

Here's what I added to the example code to make it a function. The Serial.print lines are there just to help with debugging. I also initialized the variable at the top of the sketch with char msg[125]; // make a nice fat buffer (125 characters) for tweets

If your tweet size exceeds that array size, YOU WILL HAVE PROBLEMS! I spent three hours trying to figure out why my analogRead functions were adding themselves to each other instead of generating new readings.

void postTweet(String tweet){
   	   String termTweet = tweet + "\0" ;// Terminate the tweet with a null
	   Serial.print(termTweet);
	   Serial.println(" - Terminated tweet");
	   int twtlen = (termTweet.length()+3); // count the characters, add 3 just in case
	   Serial.print(twtlen);
	   Serial.println(" - Tweet length");
	   termTweet.toCharArray(msg,twtlen); // Convert it to an array called msg
	   Serial.print("Attempted tweet ");
	   Serial.println(msg);
	   Serial.println("connecting ...");
	   if (twitter.post(msg)) {

The rest of the posting code is in the Twitter library.
 
Remove these adsRemove these ads by Signing Up