Processing + Arduino + Twitter + OAuth

Posted by Erin, the RobotGrrl on Wednesday, September 15th, 2010


WARNING: There’s a newer version of this code here: “Simple Processing Twitter”!

People who devise these spectacular security protocols and stuff really make it complicated to communicate to cool social networking sites. Yesterday I got carried away and ended up making a Processing sketch that could successfully connect to Twitter through its OAuth method. This is primarily for a project where Tweets are read, sent into Processing, which are then sent to the Arduino, which then broadcasts the message through its Xbee, and then the other robots pick up on the message. If the message is addressed to them, then they will parse it and do what it says. The first step though was to try and get the OAuth to work. Below is the link to the Processing code and some steps that I took that may help others. :)

Click to download ToArduinoAndTwitter v02 pde

1. Set up a Twitter application

Here is a screenshot of the settings that I used:

Twitter App

2. Open up the code and fill in the OAuth consumer information (line 131)

  1. twitter.setOAuthConsumer("***", "***"); // consumer key, consumer secret

3. Uncomment the initTwitter() method call (and comment out the other twitter method calls) in void setup()

4. Open the sketch’s folder (keyboard shortcut: command-K). Open up url.text and go to that website. Copy the pin.

5. Enter the pin in pin.txt and press enter. For some reason there has to be a new line at the end, otherwise it won’t work :P

6. The code should now give you the access token and secret that you need. This should also be in tokens.txt now.

7. Take the tokens and put them into the code (line 238)

  1. String token = "***";// load from a persistent store
  2. String tokenSecret = "***"; // load from a persistent store

8. Comment out initTwitter() method call, uncomment connectTwitter(). You can also uncomment getSearchTweets too!

Hopefully those steps worked for you. It’s not totally automatic, with the whole copying and pasting of the tokens, but it seems to be the quickest and easiest way to get this done with Processing. Luckily you don’t have to do those steps all the time! Since it’s open source, maybe someone can improve on it! :) If you find any bugs or have improvements, be sure to leave a comment or notify me! Happy tweeting!

Processing and Twitter

This is the screenie when it worked! Woohoo!


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. GO OPEN SOURCE! :)

Posted in: Programming, Projects.
Tags: , , , , , ,

13 Responses to “Processing + Arduino + Twitter + OAuth”

  1. Loan Myers Says:

    Thats so cool! I was trying to do this a couple of weeks ago but I didn’t get very far. There wasn’t much documentation online.

    Thanks heaps! : )

  2. Ankkit Says:

    I still cant seem to get the Oauth to work. I made an app and registered it. I have the consumer keys.. now how do i go foward. I have a lcd screen and first just want to get the tweets out on the screen.

  3. Erin, the RobotGrrl Says:

    Did you add the keys to the code? Did you put the pin into pin.txt and get the access tokens?

    Just wondering where in the steps you got lost…

  4. gui Says:

    hello, erin!

    thanks for the post, it’s very helpful =)

    for some unidentified reason, though, it’s not quite working for me: after registering the app, getting the url and finally the pin, the token is not generated. instead, I receive a “unable to get access token” message and processing throws an exception:

    TwitterException{exceptionCode=[fa54b184-3bf2623f], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.5-SNAPSHOT(build: d372a51b9b419cbd73d416474f4a855f3e889507)}

    do you have any idea what could be triggering the error?

    thanks in advance!

  5. roj Says:

    hello…i downloaded and extracted the zip file above and i cannot see any url in url.txt. could you please give me the link? thank you.

  6. Hans Says:

    I am having the same problem as gui, after entering the pin, adding a new line and savign the text file, the program gives an error:

    TwitterException{exceptionCode=[fa54b184-3bf2623f], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.5-SNAPSHOT(build: d372a51b9b419cbd73d416474f4a855f3e889507)}

    The problem could arise from the commenting out of the arduino libraries. I am learning Processing, and no very little about arduino. As of now I am trying to learn datamining, and after searching for accessing twitter I was directed here. I am trying to access twitter for use with Processing alone, is there a way to alter this code to do so?

  7. emma Says:

    Hi Erin,

    I have the same problem as Roj – there’s no URL in the url.txt file. Is it a problem opening it up in Text Edit on a mac? Or am I being really stupid?

    Cheers
    Emma

  8. idle hands › i_am_larsulrich tweets again! Says:

    [...] my lars ulrich bot, but what the hey I then found a little tutorial on how to do it. Here it is : http://robotgrrl.com/blog/2010/09/15/processing-arduino-twitter-oauth/ very [...]

  9. emma Says:

    Hi again,

    Actually now, I can get as far as the url and the pin, but it won’t fetch the access token as Hans and gui mention.

    The Twitter app page has the details of the access token on it, so I’ve put these in and now plan to carry on! Would still like to be able to figure this out… just wondering why you need to go through the process of getting the access tokens with the code, if you can just copy them in from your Twitter application pages?

    Thanks
    Em

  10. Akoz Says:

    Fantastic work. Followed the steps and am up and running. Thanks!
    I am working on a similar project that involves controlling some bots via twitter.

    Interested to know a few things as my Processing knowledge is super basic and so am having a hard time going through the code.

    How exactly is the data from Twitter stored in Processing and transmitted to Arduino- is there an array that stores this?

    What is the purpose of the the rectangle and the mouseover(debugging?)

    Is it possible to set the code to query for new tweets every 30 seconds?
    After setting up the app, I am wondering if its possible to listen to tweets of a specific group of users , and listen specifically for emoticons?
    It seems the search is only limited to one account/one app at the moment…

    Thanks,
    Alex

  11. Joe McKay Says:

    I also was unable to get the access tokens through processing but was able to inserted them directly from twitter’s site.

    Here’s the stupid thing that tripped me up. Don’t forget that when you switch over to using connectTwitter that you need to update the line twitter.setOAuthConsumer (“xxxx”, “xxxx”);

    Thanks for the walkthrough!

  12. Erin, the RobotGrrl Says:

    Hey everyone! Thanks for all the comments ;) I had a chance to update the code. Read about it here: http://robotgrrl.com/blog/2011/02/21/simple-processing-twitter/

  13. soon Says:

    seems twitter enforcing the read only mode, they have tighten the security. See here: https://dev.twitter.com/docs/application-permission-model. But thanks a lot for your code as I need to retrieve information rather than posting info.

    thank you.

Leave a Reply