Introduction: Twitter Physical Client

Picture of Twitter Physical Client
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

Picture of 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

Once done you can turn it on and wait for someone to retweet any of your tweets once someone does the flag would be raised and the led will blink representing the number of retweets you received so far.

Another variation of the same project is 

Comments