view history edit print login register

Twitter Library for Arduino
Author:  NeoCat


Navigation


Current version

1.0.1 2009-07-11: Fix a bug on multiple posts.


History

1.0.0 2009-04-12: First release.
1.0.1 2009-07-11: Fix a bug on multiple posts.


Description

Twitter Library is a library for the Arduino to post a message to Twitter via the Arduino Ethernet Shield.

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


Download, install and import

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

and put the Twitter directory in "hardware/libraries/" of Arduino IDE.

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

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

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 in the same way.


Creation

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

  1. Twitter twitter("YourID:Password");

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(void)

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

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(void)

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());
  2.         return statusCode;


Example

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


Information about this page

Last Modified: July 11, 2009, at 04:07 AM
By: NeoCat