Step 3Processing
You'll need to install the twitter4j library so it can be used by Processing.
Get it here: http://twitter4j.org/en/index.html#download
Install it here (or equivalent): C:\Program Files\processing-1.5.1\modes\java\libraries
You're done.
Access it here: Processing > Sketch > Import Library... > twitter4j
And when you do, it'll add this to the top of your code:
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.*;
Incidentally, you'll also add Serial I/O from the Sketch > Library, but that's not important right now.
So why do you need twitter4j? The short answer is that it provides you with simple functionality so you don't have to write a whole bunch of crazy code every time you want to access Twitter. We use twitter4j because it's awesome and it makes our job easier.
// ################################################################
// ################################################################
// twitterMentionMoodLight_processing
// for use w/ twitterMentionMoodLight_arduino
//
// Communicate w/ arduino over Serial
// Communicate w/ twitter via twitter4j library
// Shout out to Adafruit, twitter4j, lurkers and msg boards everywhere.
// learn more at http://www.instructables.com/member/pdxnat/
// http://twitter4j.org/en/javadoc/twitter4j/Twitter.html
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();
String oldID = "";
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("@msg_box", 35, 65);
listenToArduino();
getMention();
delay(15000); // wait 15 seconds to avoid Twitter Rate Limit
}
void listenToArduino() {
String msgOut = "";
String arduinoMsg = "";
if (arduino.available() >= 1) {
arduinoMsg = arduino.readString();
msgOut = arduinoMsg+" at "+hour()+":"+minute()+":"+second()+" "+year()+month()+day();
updateStatus(msgOut);
}
}
void getMention() {
List mentions = null;
try {
mentions = twitter.getMentions();
}
catch(TwitterException e) {
println("Exception: " + e + "; statusCode: " + e.getStatusCode());
}
Status status = (Status)mentions.get(0);
String newID = str(status.getId());
if (oldID.equals(newID) == false){
oldID = newID;
println(status.getText()+", by @"+status.getUser().getScreenName());
arduino.write(1); // arduino gets 1
}
}
void updateStatus(String s) {
try {
Status status = twitter.updateStatus(s);
println("new tweet --:{ " + status.getText() + " }:--");
}
catch(TwitterException e) {
println("Status Error: " + e + "; statusCode: " + e.getStatusCode());
}
}
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|











































