Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
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.
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();
ArrayList ret_retweets=new ArrayList();
for (Status s: retweets)
System.out.println("found "+ret_retweets.size()+" 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)
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),
Step 3: Components Assembly
Using the prototyping board connect the following
Pin 13 to the resistor
the +Ve side of the led to the resistor, the ground side goes back to the ground pin on the arduino.
arduino pin 9 goes to the control pin of the servo.
the servo +ve pin connects to arduino +ve and the ground connects to the ground
Build the lego assembly, secure the servo inside using either putty or wires (i used both as it kept on wobbling.)
attach a long stick with a flag attached to it to the servo.
Step 4: Arduino Sketch
The loop checks if there was a new serial input from the computer, if there wasn’t it blinks the LED a number of times equal to the number of retweets, if a new value is received the servo is told to move (raising the flag), and it stays up for 15 seconds before getting lowered again.
I used a delay in the servo movement as the servo was too fast and i want it to raise the flag slowly.
I'm attaching the sketch I sketch I wrote so you can use it right away.
Step 5: Operation
Another variation of the same project is