In this project I’m building a physical Twitter client using Arduino and a flag, basically an arduino powered retweet indicator, whenever any of my tweets is retweeted some physical action will happen to notify me of that.
You’ll need:
1. Flag.
2. Arduino.
3. Laptop with a java IDE installed on it.
4. Yellow Led
5. 330 Ohm resistor
6. Servo motor (even a weak one would do, you don’t need a lot of torque)
7. Lego Blocks
8. Twitter ( Consumer Key / Consumer Secret) from the website.
This project requires basic Arduino skills and some java development background.
Step 1: Register a Twitter Application
You need to register your application with twitter to get consumer key and consumer secret.
1. Log into dev.twitter.com
2. Sign in using your user name.
3. Click on create an app.
4. Fill in the values, You can put in anything in call back URL or even leave it blank since its not a web app.
5. Copy the consumer key and secret values (you’ll need them later). you’ll find them in the OAuth section.
Step 2: Start by building your java application
The Java code goes in queries for retweets, sets a floor (to avoid listing historical retweets), and starting from that floor every time it queries it returns the number of retweets since the last query, this is done using an index that gets set with every query (rather than saving it to disk or DB).
I’m attaching the code, you’ll need to download it and modify the following classes.
1. Open the project on netbeans.
2. Make sure that the libraries “RXTXcomm.jar” and “twitter4j-core-2.2.2.jar” are imported, if they weren’t import them manually (you’ll find them in the lib folder).
3. Check which port your Arduino is connected to, in my case it was COM10, and so i modified class “serial_test” constructor to reflect that.
a. open serial_test
b. go to static ports array PORT_NAMES and modify the windows entry to match yours (if you are using linux modify the linux value).
4. Open class “Arduino” and change ConsumerKey and ConsumerSecret to match the ones you have from step 1.
5. Clean and compile, now the code is read.
Code Explanation
Built using Twitter4j this requires that you should call the authenticate class first, slightly out of scope here but I’m willing to provide any needed help if required
here is the method that harvests the number of retweets.
public ArrayList get_retweets_from_last(String id)
{
Twitter twitter = new TwitterFactory().getInstance();
ResponseList retweets=null;
ArrayList ret_retweets=new ArrayList();
try{
retweets=twitter.getRetweetsOfMe();
for (Status s: retweets)
{
if(s.getId()<=Long.parseLong(id))
break;
ret_retweets.add(s);
}
System.out.println(“found “+ret_retweets.size()+” retweets”);
}catch(Exception e)
{e.printStackTrace();}
return ret_retweets;
}
I then use the RXTXcomm api to transfer this value to the Arduino, the Arduino keeps track of how many retweets it received since it was reseted, this number is incremented every time it receives a new one from the Java application.
public void write(int s)
{
try{
output.write(s);
}catch(Exception e)
{
e.printStackTrace();
}
}
Finally I manage all of this from a control for loop with a timed delay (the reason here is that you only get a limited number of Twitter API calls per hour, if you exceed that you get blocked),
while(true)
{
// retweets=t.get_retweets_from_last(last_retweet);
retweets=t.get_mentions_from_last(last_retweet);
if (retweets.isEmpty())
{
try{Thread.sleep(60000);}catch(Exception e){}
continue;
}
last_retweet=””+retweets.get(0).getId();
st.write(retweets.size());
try{Thread.sleep(30000);}catch(Exception e){}
}
Step 3: Components Assembly
Using the prototyping board connect the following
For more detail: Twitter Physical Client