Hey mvaughan19, I thought you might like to see an example I made for Arduino Processing Twitter w/ twitter4j. The code for both Arduino and Processing are pasted below and you can go here: http://www.instructables.com/id/Simple-Tweet-Arduino-Processing-Twitter/ for directions on using OAuth.
I use Arduino to accept input from a switch to digital pin #10 and based on that value perform Serial.write(), which the Processing script reads and then posts a tweet using the twitter4j lib and OAuth.
It's bare bones as I tried to make it as uncomplicated as possible. It foundational code.
Good luck.
// simpleTweet_00_a
/*
simpleTweet_00 arduino sketch (for use with
simpleTweet_00 processing sketch) by @msg_box june2011
This script is intended for use with a magnetic reed switch,
but any on/off switch plugged into pin #10 will readily work.
The Arduino is connected to a circuit with a sensor that
triggers the code: Serial.write(n); where n = 1 or 2.
The Processing sketch listens for that message and then
uses the twitter4j library to connect to Twitter
via OAuth and post a tweet.
To learn more about arduino, processing, twitter4j,
OAuth, and registering your app with Twitter...
visit <http://www.instructables.com/id/Simple-Tweet-Arduino-Processing-Twitter/>
visit <http://www.twitter.com/msg_box>
This code was made possible and improved upon with
help from people across the internet. Thank You.
Special shoutouts to the helpful lurkers at twitter4j,
arduino, processing, and bloggers everywhere, and
to the adafruit & ladydada crowdsource.
And above all, to my lovely wife, without
whom, none of this would have been possible.
Don't be a dick.
*/
const int magReed_pin = 10; // pin number
int magReed_val = 0;
int currentDoorState = 1; // begin w/ circuit open
int previousDoorState = 1;
void setup(){
Serial.begin(9600);
pinMode(magReed_pin, INPUT);
}
void loop(){
watchTheDoor();
}
void watchTheDoor(){
magReed_val = digitalRead(magReed_pin);
if (magReed_val == LOW){ // open
currentDoorState = 1;
}
if (magReed_val == HIGH){ // closed
currentDoorState = 2;
}
compareStates(currentDoorState);
}
void compareStates(int i){
if (previousDoorState != i){
previousDoorState = i;
Serial.write(i);
delay(1000); //
}
}
// simpleTweet_00_p
/*
simpleTweet_00 processing sketch (for use with
simpleTweet_00 arduino sketch) by @msg_box june2011
The Arduino is connected to a circuit with a sensor that
triggers the code: Serial.write(n); where n = 1 or 2.
The Processing sketch listens for that message and then
uses the twitter4j library to connect to Twitter
via OAuth and post a tweet.
compareMsg() is added in case you want to compare
the current and previous tweets to prevent retweets.
This Processing code requires the twitter4j library
and your registered app info from dev.twitter.com
To learn more about arduino, processing, twitter4j,
OAuth, and registering your app with Twitter...
visit <http://www.instructables.com/id/Simple-Tweet-Arduino-Processing-Twitter/>
visit <http://www.twitter.com/msg_box>
This code was made possible and improved upon with
help from people across the internet. Thank You.
Special shoutouts to the helpful lurkers at twitter4j,
arduino, processing, and bloggers everywhere, and
to the adafruit & ladydada crowdsource.
And above all, to my lovely wife, without
whom, none of this would have been possible.
Don't be a dick.
*/
import processing.serial.*;
import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.http.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
static String OAuthConsumerKey = "YOUR CONSUMER KEY";
static String OAuthConsumerSecret = "YOUR CONSUMER SECRET";
static String AccessToken = "YOUR ACCESS TOKEN";
static String AccessTokenSecret = "YOUR ACCESS TOKEN SECRET";
Serial arduino;
Twitter twitter = new TwitterFactory().getInstance();
void setup() {
size(125, 125);
frameRate(10);
background(0);
println(Serial.list());
String arduinoPort = Serial.list()[0];
arduino = new Serial(this, arduinoPort, 9600);
loginTwitter();
}
void loginTwitter() {
twitter.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret);
AccessToken accessToken = loadAccessToken();
twitter.setOAuthAccessToken(accessToken);
}
private static AccessToken loadAccessToken() {
return new AccessToken(AccessToken, AccessTokenSecret);
}
void draw() {
background(0);
text("simpleTweet_00", 18, 45);
text("@msg_box", 30, 70);
listenToArduino();
}
void listenToArduino() {
String msgOut = "";
int arduinoMsg = 0;
if (arduino.available() >= 1) {
arduinoMsg = arduino.read();
if (arduinoMsg == 1) {
msgOut = "Opened door at "+hour()+":"+minute()+":"+second();
}
if (arduinoMsg == 2) {
msgOut = "Closed door at "+hour()+":"+minute()+":"+second();
}
compareMsg(msgOut); // this step is optional
// postMsg(msgOut);
}
}
void postMsg(String s) {
try {
Status status = twitter.updateStatus(s);
println("new tweet --:{ " + status.getText() + " }:--");
}
catch(TwitterException e) {
println("Status Error: " + e + "; statusCode: " + e.getStatusCode());
}
}
void compareMsg(String s) {
// compare new msg against latest tweet to avoid reTweets
java.util.List statuses = null;
String prevMsg = "";
String newMsg = s;
try {
statuses = twitter.getUserTimeline();
}
catch(TwitterException e) {
println("Timeline Error: " + e + "; statusCode: " + e.getStatusCode());
}
Status status = (Status)statuses.get(0);
prevMsg = status.getText();
String[] p = splitTokens(prevMsg);
String[] n = splitTokens(newMsg);
//println("("+p[0]+") -> "+n[0]); // debug
if (p[0].equals(n[0]) == false) {
postMsg(newMsg);
}
//println(s); // debug
}