News of The Wolf

Random, hopefully interesting technical ramblings

Twitter Popularity Meter – Code

Posted by chemicaloliver on 06/10/2009

Recently I’ve been working on a popularity meter which searches twitter and displays a bar graph of the popularity of the search term on an LED Matrix driven by an Arduino. It currently relies on a PHP script on my webserver to do the actual twitter search and the arduino reads the page via ethernet and then displays the number on it bar graph.

Arduino Twitter Meter

Arduino Twitter Meter



The PHP script searches twitter receiving the results as a JSON feed and counting the results, this is just a basic script I threw together and is still very much work in progress as if there are more than ten results the arduino gets a bit confused. This is an abbreviated version of my code which is still functional.

$q = "%40chemicaloliver";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://search.twitter.com/search.json?q=".$q."");
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($curl);
$json_result = json_decode($result);
$count = 0;
foreach ($json_result->results as $res)
{
$count++;
}
curl_close($curl);
echo $count;

This searches for replies to my posts and displays in a number in a minimal html page.

The arduino then reads this page every time its main loop is executed and adds a new bar to the LED bar graph using the following code and using the TimerOne library.

#include 
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 1, 1, 67 };
byte server[] = { 10, 1, 1, 6 }; // Google
byte subnet[] = {255,0,0,0};
byte gateway[] = {10,1,1,100};
char end =0;

Client client(server, 80);

//Pin connected to Pin 12 of 74HC595 (Latch)
int latchPin = 2;
//Pin connected to Pin 11 of 74HC595 (Clock)
int clockPin = 3;
//Pin connected to Pin 14 of 74HC595 (Data)
int dataPin = 4;
uint8_t led[8];
void setup() {
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

led[0] = B00000000;
led[1] = B00000000;
led[2] = B00000000;
led[3] = B00000000;
led[4] = B00000000;
led[5] = B00000000;
led[6] = B00000000;
led[7] = B00000000;

Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);

Timer1.initialize(10000);
Timer1.attachInterrupt(screenUpdate);
}


void loop() {
  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /twitterpop/ HTTP/1.0");
    client.println();
  }
  delay(100);
  if (client.available()) {
    while(client.available())
    {
    char c = client.read();
    //Serial.print(c);
    end = c;
    }
    Serial.print(end);
    client.stop();
  }
    int inByte = /*Serial.read();*/end;
    if (inByte == 48)
    {
      for(int no=7;no>0;)
      {
        led[no]=led[no-1];
        no=no-1;
      }
      led[0]= B00000000;
    }
    else if(inByte == 49)
    {
      for(int no=7;no>0;)
      {
        led[no]=led[no-1];
        no=no-1;
      }
      led[0] = B10000000;
    }
    
    else if(inByte == 50)
    {
      for(int no=7;no>0;)
      {
        led[no]=led[no-1];
        no=no-1;
      }
      led[0] = B11000000;
    }
    
    else if(inByte == 51)
    {
      for(int no=7;no>0;)
      {
        led[no]=led[no-1];
        no=no-1;
      }
      led[0] = B11100000;
    }
    
    else if(inByte == 52)
    {
      for(int no=7;no>0;)
      {
        led[no]=led[no-1];
        no=no-1;
      }
      led[0] = B11110000;
    }
    
    else if(inByte == 53)
    {
      for(int no=7;no>0;)
      {
        led[no]=led[no-1];
        no=no-1;
      }
      led[0] = B11111000;
    }
    
    else if(inByte == 54)
    {
      for(int no=7;no>0;)
      {
        led[no]=led[no-1];
        no=no-1;
      }
      led[0] = B11111100;
    }
    
    else if(inByte == 55)
    {
      for(int no=7;no>0;)
      {
        led[no]=led[no-1];
        no=no-1;
      }
      led[0] = B11111110;
    }
    
    else if(inByte == 56)
    {
      for(int no=7;no>0;)
      {
        led[no]=led[no-1];
        no=no-1;
      }
      led[0] = B11111111;
    }
delay(3000);
Serial.print("loop\n");
}

void screenUpdate() {
uint8_t row = B00000001;
for (byte k = 0; k < 9; k++) {
// Open up the latch ready to receive data
digitalWrite(latchPin, LOW);
shiftIt(~row );
shiftIt(led[k] ); // LED array
// Close the latch, sending the data in the registers out to the
//matrix
digitalWrite(latchPin, HIGH); row = row << 1;
}
}


void shiftIt(byte dataOut) {
// Shift out 8 bits LSB first,
// on rising edge of clock
boolean pinState;
//clear shift register read for sending data
digitalWrite(dataPin, LOW);
// for each bit in dataOut send out a bit
for (int i=0; i<8; i++) {
//set clockPin to LOW prior to sending bit
digitalWrite(clockPin, LOW);
// if the value of DataOut and (logical AND) a bitmask
// are true, set pinState to 1 (HIGH)
if ( dataOut & (1<<i) ) {
pinState = HIGH;
}
else {
pinState = LOW;
}
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
//send bit out on rising edge of clock
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
}
//stop shifting
digitalWrite(clockPin, LOW);
}

More information about the hardware will be included in a later blog post.

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

 
%d bloggers like this: