Twitter + Processing + Arduino

The journey from sensor to Twitter is a little complex but we are providing you with examples and code so you only get to do the fun, conceptual part. The system works like this:

1- Sensor input is read by Arduino
2- Arduino talks to Processing through serial
3- Processing uses a library called Twitter4j to interface with the Twitter API (Application Programming Interface)
4- The Twitter API tweets on the selected account

And vice versa.
Please note that if Processing crashes you’ll probably need to restart and unplug the board. And you can’t have
We’ll break this down into simple steps.

Twitter
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
  • Create a Twitter application
  • 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”. Don’t forget to SAVE the settings
  • Click on the button to request AccessToken and AccessTokenSecret. If you cange the “read and write” settings you have to create new tokens
  • Copy and paste your credentials: OAuthConsumerKey and OAuthConsumerSecret, AccessToken and AccessTokenSecret in the Processing sketch
  • Twitter4j will require those credentials in order for you to do stuff.

Tweeting from Processing

Here’s a simple demo that Tweets from Processing:
processing_to_twitter.zip

Important: You have to change OAuthConsumerKey, OAuthConsumerSecret, AccessToken, AccessTokenSecret or you will tweet with my credentials @ems2tweet.

The documentation for the library (included in the zip) is here:
http://twitter4j.org/en/javadoc/index.html

Arduino
If you successfully sent your first tweet on your account, then you should be able to integrate it with Arduino and Firmata.

Exercise
Try to send something with a switch (possibly de-bounced) or another sensor. Please make sure your application doesn’t send too many tweets (test it offline first) or you account may be suspended.

Reading tweets from processing
This is an example that reads new tweets from your account. In this way you can control a local system (Processing/Arduino) remotely from everywhere.
tweetReceiver.zip

Exercise
Turn an LED on and off by sending certain tweets.

Twitter + Arduino

Here’s a complete example of bi-directional communication between Twitter and Arduino via processing. Tweet anything with the word “sensor” in it and the script will read and tweet the value of the sensor and turn on an LED.
twitterArduino.zip

Various Snippets

Serial issues with Firmata
It’s rare but sometimes you need to manually change the constructor for the Arduino in processing

println(Arduino.list());  
arduino = new Arduino(this, Arduino.list()[0], 57600);

Read the output of the “println(Arduino.list())” and look for the slot corresponding to /dev/tty.usbmodem… That’s the number you have to put between the brackets instead of 0 in Arduino.list()[0].

Comparing strings
You can’t compare strings in processing using the symbol ==. You have to use “equals”:

String p = "potato";
if (p.equals("potato")) {
  println("Yes, the contents of p and potato are the same.");
}

Parsing
Here are some basic snippets you should use to parse a tweet.
Looking for the word “love” in the tweet:

println ("Tweet = " + mostRecentTweetString) ;
 
    String keyword = "love";
    //indexOf returns the char position OR -1 if no mathces are found
    int locationOfTestStringInTweet = mostRecentTweetString.indexOf(keyword);
    if (locationOfTestStringInTweet != -1) {
      println ("Test string exists! (At character index: " + locationOfTestStringInTweet + ")");
    }

Looking for a number and converting it to integer:

//first I split my string into single words in an array of strings
    //separating with " " space
    String tweetWords[] = split(mostRecentTweetString, " ");
 
    //check all the words
    for (int i=0; i<tweetWords.length; i++) {      
      //try to see if there is a number
      try {
        int valueFetchedFromTweet = Integer.parseInt(tweetWords[i]);
        //if the number is parseable do something
        println("number found = " + valueFetchedFromTweet);
      }
      catch(Exception e) {
        //if it's not a number it throws an error
        println("No numbers in "+tweetWords[i]);
      }
    }

Parsing a formatted “command” from a tweet. A command in this example is a string followed by a space and a number like “servo 180”

//splitting the tweet into words
    String arrayOfWords[] = split (mostRecentTweetString, " ");
 
    if (arrayOfWords[0].indexOf("servo") != -1)
    {
      //if the first word is "servo" I look for an angle in the second word
      int servoAngle = Integer.parseInt(tweetWords[1]);
 
      println("moving servo to " + servoAngle);
      //careful if the command is not formatted it will throw an error
      arduino.analogWrite(servoPin1, servoAngle);
    }
 
    if (arrayOfWords[0].indexOf("LED") != -1)
    {
      println("BLINK BLINK");
      //...and so on
    }

Controlling Servos from Processing (and Twitter)
Unfortunately the Arduino/Firmata library for processing doesn’t allow you to use servo. In order to do that you have you create your own Arduino to Processing communication interface. You can find some examples online:
Controlling a servo motor with processing

Single servo example

Reading tweets from specific users
The code above reads from your account. But if you change this line:

statuses = twitter.getUserTimeline();

With something like this:

statuses = twitter.getUserTimeline (17454769);

You’ll get the last tweet of the user associated to that number.
To find the id of a certain user insert the twitter handle here:
http://www.idfromuser.com/

For example putting together this code with the one we saw on monday I can make an application that
1. Reads from @foxnews (Id: 1367531)
2. Looks for the string “Occupy Wall Street”
3. Makes an led blink
4. And “responds” with a tweet such as “@foxnews bullshit!” (not that you need a parser to detect bullshit on fox news)

Reading tweets from all users
You can check when a word is posted from all the users on twitter. Twitter is huge so pick an uncommon word or an hashtag such as #art.
tweetChecker.zip

Uploading a webcam snapshot to Flickr
I haven’t tested this one recently.
http://frontiernerds.com/upload-to-flickr-from-processing

Uploading image + status on Twitpic
I haven’t tested this one recently.
The integration with Twitpic is better than Flickr so if you want to quickly upload images and make them visible on Twitter try this example:
processingTwitpic.zip

It uses another library (included in the “code” folder). It uploads an image and post a twitter update with just a line of code. In this case the image is randomly generated and is uploaded after a keystroke. Use your Twitter account username and password (no API developer keys are needed).

IF THIS THEN THAT

ITTT is a social network glue that allows you to link and trigger events across web 2.0 services
Some examples here

For this assignment you can use your dedicated twitter account connected to processing as intermediary for all the other networks. es:

Facebook -> Twitter -> Processing -> Arduino -> Circuit

So many things can go wrong here :-P

Posted in

Post a comment