Twitter Library for Arduino
Author:  NeoCat

Be advised that this library does not tweet directly from your Arduino. It sends your tweets to a website, which then tweets it from there.

However, username/password will NOT be sent to the site by OAuth mechanism. You can invalidate the OAuth token for this library anytime at http://twitter.com/settings/connections.


Navigation


Current version

1.3.0 2011-12-04: Support IDE 1.0.


History

1.0.0 2009-04-12: First release.
1.0.1 2009-07-11: Fix a bug on multiple posts.
1.1.0 2010-03-20: Support OAuth and displaying client name ('via Arduino')
1.2.0 2010-10-24: Port EthernetDNS library for IDE 0019 or later
1.2.1 2010-11-13: Add a response output for debugging.
1.2.2 2011-02-05: Modified for ArduinoEthernet Version 1.0b4, and support both IDE 0019(or later) and 0018(or earlier).
1.3.0 2011-12-04: Support IDE 1.0.


Description

Twitter Library is a library for Arduino to tweet on Twitter via the Arduino Ethernet Shield.

It is convenient to making a bot for Twitter which posts messages automatically from various sensors.

Notice: Tweets are sent via a shared server. Please avoid sending more than 1 request per minute not to overload the server.


Download, install and import

Download here: Attach:Library-Twitter-1.3.zip

and put the Twitter directory in "~/Documents/Arduino/libraries (Mac) or My Documents\Arduino\libraries\ (Windows)" of Arduino IDE.

If you are using Arduino IDE 0022 or earlier, you also need to install EthernetDNS library 1.0b4 or later, which is available at gkaindl.com. You don't need this with IDE 1.0 or later, because DNS facility is included in EthernetClient library.

The library is automatically complied when you launch the Arduino IDE.

Before tweeting, get a token at http://arduino-tweet.appspot.com/ . This library send a tweet via the site to avoid using up the memory of Arduino by complex OAuth signature stuff.

You can see an example sketch from "File -> Sketchbook -> Example -> Library-Twitter -> SimplePost".

If you are using Arduino IDE 0022 or earlier, please uncomment inclusion of EthernetDNS.h to build the sketch correctly.

To create a new sketch, select from the menubar "Sketch->Import Library->Twitter". Once the library is imported, an '#include <Twitter.h>' line will appear at the top of your Sketch. You need also import Ethernet library and EthernetDNS library in the same way for IDE 0022 or earlier.


Creation

You need to create an instance of Twitter class like below:

  1. Twitter twitter("YOUR-TOKEN");  // this was YourID:Password in 1.0.1

You need also begin Ethernet library.


Functions

bool post(const char *message)

Begin posting the specified message to Twitter. If connection to twitter.com is established successfully, this function returns true.

If failed to connect Twitter, returns false. (Check Ethernet is correctly configured.)

Posting is not done yet even it returns true. You should call twitter.checkStatus() periodically or twitter.wait().

bool checkStatus(Print *debug = NULL)

Check if the posting request is running. Returns true if it's still running.

You can specify the debug argument to output the response from server for debugging, e.g.

  1. checkStatus(&Serial);

You can omit the debug argument if the output is not required.

int status(void)

Returns the HTTP status code in response from Twitter, e.g. 200 - OK.

Only available after posting the message is done and checkStatus() returns false.

int wait(Print *debug = NULL)

Wait until posting the message is done. Return value of this function is HTTP status code in response from Twitter.

Equivalent for code below:

  1. while (checkStatus(debug));
  2.         return statusCode;


Example

  1. /* Post a simple message to Twitter  */
  2. #if defined(ARDUINO) && ARDUINO > 18
  3. #include <SPI.h>
  4. #endif
  5. #include <Ethernet.h>
  6. //#include <EthernetDNS.h> // only for IDE 0022 or earlier
  7. #include <Twitter.h>
  8.  
  9. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  10. byte ip[] = { 10, 0, 0, 177 };
  11. byte gateway[] = { 10, 0, 0, 1 };
  12. byte subnet[] = { 255, 255, 0, 0 };
  13.  
  14. Twitter twitter("YOUR-TOKEN");
  15. char msg[] = "Hello, World! I'm Arduino!";
  16.  
  17. void setup()
  18. {
  19.   Ethernet.begin(mac, ip, gateway, subnet);
  20.   Serial.begin(9600);
  21.  
  22.   delay(1000);
  23.  
  24.   Serial.println("connecting ...");
  25.   if (twitter.post(msg)) {
  26.     int status = twitter.wait(&Serial);
  27.     if (status == 200) {
  28.       Serial.println("OK.");
  29.     } else {
  30.       Serial.print("failed : code ");
  31.       Serial.println(status);
  32.     }
  33.   } else {
  34.     Serial.println("connection failed.");
  35.   }
  36. }
  37.  
  38. void loop()
  39. {
  40. }

Notice: Twitter seems to reject repeated tweets with the same contenet (returns error 403).

Notice: Tweets are sent via a shared server. Please avoid sending more than 1 request per minute not to overload the server.


Information about this page

Last Modified: July 13, 2012, at 10:51 PM
By: NeoCat

Share