Tweets

CIID Twitter Examples:
http://golancourses.net/ciid/resources/TwitterInProcessing.zip


Here are some supplementary materials, which I’ve snagged from another of my courses, relating to Twitter in Processing, and using Twitter with Arduino, and Java String processing.

In this exercise, we can also explore the idea of the “Internet of Things“, and Things that Tweet, by making networked objects. Things may act on signals they receive, or they may transmit signals from things they sense, or both. They can even act on a signal from the other side of the world. We’ll take advantage of open protocols like Twitter, open-source arts engineering tools like Processing and Arduino, and API’s like Twitter4j and the Java String API in order to do our work:

In order to do this project, you’ll need to have a Twitter account, and then create an authorized “App”. Basically, Twitter wants you to have fun, but they also want to keep track of what you’re doing, so that you don’t send lots of spam. (Computer programs are good at that.) You’ll need to get credentials by doing the following:

  • Get a Twitter account
  • Go to https://dev.twitter.com/apps/new
  • Fill out the description of your application (e.g. “Caroline’sEMS2App”)
  • Agree to the developer’s license, fill out the CAPTCHA, etc…
  • Change your app settings (Settings tab) to “Read and Write”
  • Copy and paste your credentials: OAuthConsumerKey and OAuthConsumerSecret
  • Request, then copy and paste your AccessToken and AccessTokenSecret
  • Twitter4j will require those credentials in order for you to do stuff.

Here are TWO PIECES OF IMPORTANT INFO:

  • Please don’t use my (Golan’s) credentials, which happen to appear in the code examples linked below. I don’t want to be blacklisted because you sent a bunch of spam. Please get your own.
  • Limit your query rate! Twitter is pretty concerned with limiting the rates of queries you make. Your app can get blacklisted (for 15 days!) if you exceed 350 queries per hour. That’s why I have delay(12000) in the code examples that fetch data from Twitter.

DEMO CODE

(Please note that you’ll need to substitute in your own Twitter-Developer credentials for these to work.)

1. Here’s a simple demo of Tweeting from Processing:
http://www.cmuems.com/2011/resources/processingTweeter.zip

2. Here’s code for Arduino + Processing, such that pressing a button on the Arduino sends a tweet via Processing:
http://www.cmuems.com/2011/resources/arduinoprocessingtwitter.zip

3. Here’s an Arduino+Processing project that moves a servo in response to a tweet. The servo is set to a position which comes from a number parsed from the Tweet:
http://www.cmuems.com/2011/resources/tweetReceiver.zip

4. Here’s a Processing project that checks Twitter periodically to see if someone — anyone — has recently Tweeted a certain word. (I chose the word “finagle” because it doesn’t come up too often.) When that word is Tweeted, a signal is sent to an Arduino:
http://www.cmuems.com/2011/resources/tweetChecker_processing.zip

Strings

Here are some simple examples of String parsing… for example, if you were trying to parse Tweets:

//--------------------------------------------------------
// Testing to see if a Tweet contains a certain word
String tweetString = "I sure love bees";
String testString = "love";

int locationOfTestStringInTweet = tweetString.indexOf(testString);
if (locationOfTestStringInTweet != -1) {
  println ("Test string exists! (At character index: " + locationOfTestStringInTweet + ")");
}

//--------------------------------------------------------
// Converting a single number (encoded as a String) into an integer
String singleIntegerString = "123"; // that's a String, not an integer, folks

// If you want to be really safe, you have to make sure it's actually an integer.
// We do that with the try/catch business, which takes care of errors.
try {
  int integerFetchedFromString = Integer.parseInt (singleIntegerString);
  println ("The encoded integer = " + integerFetchedFromString) ;
}
catch(NumberFormatException e) {
  println("That wasn't an integer, silly!"); // you gots problems
}

//--------------------------------------------------------
// Parsing some numbers from a tweet.
// Here, I'm relying on an assumption that our tweet is correctly formatted, so no try/catch....
String tweetMixingStringsAndNumbers = "127 180 255 happy";
String arrayOfWords[] = split (tweetMixingStringsAndNumbers, " ");
int valueA = Integer.parseInt (arrayOfWords[0]);
int valueB = Integer.parseInt (arrayOfWords[1]);
int valueC = Integer.parseInt (arrayOfWords[2]);
String mood = arrayOfWords[3];
println("Commands: " + valueA + " " + valueB + " " + valueC);
This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.