Arduino Twitter Library with OAuth Support
Arduino Twitter Library allows you to tweet directly from
your Arduino board (equipped with
an Arduino
Ethernet Shield) without any intermediate servers or proxies. The
library requires about 1400 bytes of SRAM and about 22kB of Flash
memory so its runs quite nicely
on Arduino
Duemilanove and newer boards.
You can download the Arduino Twitter Library with all supporting
libraries from GitHub.
The git repository includes a fully-functional Arduino Sketch for
tweeting 1-Wire temperature sensor values to a configured Twitter
account. Please, see the Twitter/Twitter.pde for the
details.
Recent Changes
- 2012-02-27: Updated the project to compile with the Arduino IDE 1.0.
Getting Started
The following sections describe the basic library configuration and
usage in more details.
Basic Twitter Library Configuration
- Instantiate one Twitter instance and pass a working buffer for
it. The twitter library needs around 400 bytes for its operation:
/* Work buffer for twitter client. This shold be fine for normal
operations, the biggest items that are stored into the working
buffer are URL encoded consumer and token secrets and HTTP response
header lines. */
char buffer[512];
Twitter twitter(buffer, sizeof(buffer));
- Configure Twitter endpoint names, Twitter (or HTTP proxy)
server IP address and TCP port number, optional HTTP proxy usage,
and Twitter client identifiers (i.e. application consumer key and
secret) in your setup() function:
void
setup()
{
...
twitter.set_twitter_endpoint(PSTR("api.twitter.com"),
PSTR("/1/statuses/update.json"),
server_ip, server_port, true);
twitter.set_client_id(PSTR("*** consumer key here ***"),
PSTR("*** consumer secret here ***"));
...
}
Getting Twitter Account Access Tokens
The Arduino Twitter library implements
the OAuth
request signing but it does not implement
the OAuth Access Token
retrieval flow. This means that you need to authorize your
Arduino Twitter application with your PC before you can start
accessing the Twitter account from Arduino. Once you have retrieved
the account access token and token secret, you can configure them for
the Arduino Twitter library and you can start using the library.
I have used the following method for retrieving the account access
tokens (but there might be easier ways too):
- Download OAuthTwitterExample.zip
from oauth-signpost
wiki
- Open src/Main.java and configure you twitter
application's (the software running on the Arduino board) consumer
key and consumer secret for the oauth-signpost's OAuthConsumer:
OAuthConsumer consumer = new DefaultOAuthConsumer("iIlNngv1KdV6XzNYkoLA",
"exQ94pBpLXFcyttvLoxU2nrktThrlsj580zjYzmoM",
SignatureMethod.HMAC_SHA1);
- Compile src/Main.java
- Run OAuthTwiterExample and follow the instructions:
java -cp commons-codec-1.3.jar:signpost-core-1.1-SNAPSHOT.jar:bin Main
Fetching request token from Twitter...
Request token: g1re4iYfknOYMB62JZkddjwhCfvfn4WPdZrzgscMOA
Token secret: wKStK0dmhPcuFxBki3imJv7yVNXgRndmW4LSmuCg
Now visit:
http://twitter.com/oauth/authorize?oauth_token=g1re4iYfknOYMB62JZkddjwhCfvfn4WPdZrzgscMOA ... and grant this app authorization
Enter the PIN code and hit ENTER when you're done:
xxxxxxx
Fetching access token from Twitter...
Access token: 14418463-Xt70aZ8b2jz19MzY7JnQJ6HglPh0Hc5p939gLH5YI
Token secret: TkG689FOx7amgdX2ta6epE5MYsmZxVuO9ith7FtJrs
Sending request to Twitter...
Response: 200 OK
- Now you have an access token and secret for your Twitter account.
Configuring Twitter Account Access Tokens for Arduino
There are two ways how you can configure the account access tokens for
the Twitter library:
- Inline the credentials in your arduino sketch and pass them from
the program memory to the Arduino Twitter instance:
/* Set OAuth account identification from program memory. */
twitter.set_account_id(PSTR("*** set account access token here ***"),
PSTR("*** set account token secret here ***"));
- Store the credentials in EEPROM and pass the EEPROM addresses to
the Arduino Twitter instance:
/* Read OAuth account identification from EEPROM. */
twitter.set_account_id(256, 384);
And Finally, Start Tweeting!
After the library configuration has been done, it is very easy to send
tweets with the library:
void
loop()
{
...
if (twitter.is_ready())
{
if (twitter.post_status("Hello, Arduino!"))
Serial.println("Status updated");
else
Serial.println("Update failed");
}
...
}
Smallprint
Please, read and follow
the Twitter
Automation Rules and Best Practices guidelines.
Copyright © 2011-2012 Markku Rossi
<mtr@iki.fi>