The Physical Twitter Client (Arduino)

Video is at the end of this entry, feel free to skip to it directly.


In this project I’m building a physical client for Arduino, basically an arduino powered retweet indicator, so whenever any of my tweets is retweeted some physical action will happen to notify me of that. Having been able to finally find my old Lego set (or at least half of it), I salvaged a couple of servos from an old model plane I crashed a year ago, and I was set, I had all it took to build almost anything I could dream of, what I did lack though was time.

Nothing beats a quite evening in, especially when you have all your freshly washed Lego set on the floor waiting to be used, earlier I had written an arduino sketch that takes care of the computer interface part, I also had a Java application written to harvest my retweets and provides me with a number (of retweets), that I can use with my physical client, all I needed to do was assemble all of that and turn it into a working physical client.

arduino uno, 2 motors, 3 servos, Lego mechanical set.

Product Concept:

A device that notifies its user of retweets, this device will work on two levels, the first being a flag raised to indicate a new retweet (lowered after 15 seconds), the second is a blinking Led notifying him of the current number of retweets. This device is going to be connected to a computer running a java application that extracts the data from twitter, and the device’s purpose would be just to display that result.

Required Parts:

  • Yellow Led
  • 220 Ohm resistor
  • Servo motor (even a weak one would do, you don’t need a lot of torque)
  • Lego Blocks

Build :

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).

Java Part:

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.

public ArrayList<Status> get_retweets_from_last(String id)
      {
          Twitter twitter = new TwitterFactory().getInstance();
            ResponseList<Status> retweets=null;
            ArrayList<Status> ret_retweets=new ArrayList<Status>();
            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(30000);}catch(Exception e){}
                continue;
            }
            last_retweet=""+retweets.get(0).getId();
            st.write(retweets.size());
            try{Thread.sleep(30000);}catch(Exception e){}
        }

Arudino 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.

#include <Servo.h>
const int ledPin = 13;
int number_blinks=0;
byte brightness;
int ledState = LOW;
Servo myservo;
int servo_state=0;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("STARTED");
  myservo.attach(9);
  undo_physical();
  servo_state=0; //retaining the flag state is it up or down
}

void loop() {
  // check if data has been sent from the computer:
  if (Serial.available()) { //if input is recieved raise flag and then 15 seconds later lower it, update the number of blinks value
    brightness = Serial.read();
    number_blinks+=brightness;
      if(brightness==114)//i use this figure to reset the system (as an alternative to the reset button)
      { number_blinks=0;
      } else
      {
        if(servo_state==0){
          do_physical();//raise flag
          delay(15000);///keep flag up for 15 seconds
        }
      }
      if(servo_state==1)
      {
        undo_physical();//if flag up lower it.
      }

      Serial.println("new values");
      Serial.println(number_blinks);
      Serial.println(brightness);
  }
  for(int j=0; j<number_blinks; j++)//blink the led a number equivilant to the retweets
  {
    Serial.println(j);
    digitalWrite(ledPin, HIGH);
    delay(300);
    digitalWrite(ledPin, LOW);
    delay(100);
  }
  delay (1000);
}
//put the servo functionality here.
void undo_physical()
{
  Serial.println("--------SERVO ACTION--------");
  int pos;

    for(pos = 50; pos < 140; pos += 1)
    {
      myservo.write(pos);
      delay(15);                        // Slows down movements for dramatic reasons
    }
    servo_state=0;
}
void do_physical()
{
  Serial.println("--------SERVO ACTION--------");
  int pos;
     for(pos = 140; pos >= 50; pos -= 1)
    {
      myservo.write(pos);
      delay(15);          // Slows down movements for dramatic reasons
    }
    servo_state=1;
}

The Mechanical Part:

Servo and housing

This proved to be the most challenging, reality’s lack of proper exception handling is rather challenging, I mainly tried around until I decided to bind a rod to the servo using wire, and then placed that in a Lego housing I had built earlier, I then used my limited artistic skills to draw a flag on that rod.

Powered it up, and it worked.

—-UPDATE—-

Built another solution this time I’m using a dial to represent the number of replies, this uses a servo and a couple of gears for dramatic effect, I kept the blinking LED.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s