Arduino Processing Twitter w/ twitter4j
in
Share your Work
•
2 years ago
Howdy y'all. Just wanted to post this Processing twitter4j code here to help those who might want it.
For a full explanation please visit the instructable here:
Cheers.
- // 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
}
2