Bring Social Buzz To Life
Wouldn't it be cool if you could have a bell ring every time someone liked or shared your web page? In this arduino project I wanted to be able to connect directly to the facebook graph api which turned out to a bit tricky because facebook doesn't respond to just any request. After lots of trial and error I got it working!
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 7, 6, 5, 4, 3, 2);
int back_light = 9;
int share_count = -1;
int tone_pin = 0; //DO NOT USE Serial.begin() !!!
//You will also need to unplug the wire from the piezo
//While uploading sketches!
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte my_ip[] = {192,168,1,119};
byte fb_api_server[] = {66,220,146,50};
Client client(fb_api_server, 80);
//The URL of the website you are counting shares
String location = "/http://www.underthemountain.org/";
void setup() {
Ethernet.begin(mac, my_ip);
pinMode(back_light, OUTPUT);
digitalWrite(back_light, HIGH);
lcd.begin(16,2);
lcd.clear();
}
void loop() {
lcd.setCursor(0,1);
lcd.print("Connecting...");
if(!client.connect()){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Conn Failed!");
}
else{
//Start the get request
client.print("GET ");
client.print(location);
client.println(" HTTP/1.0");
client.println("Host: graph.facebook.com");
//Important! FB Api does not respond to user agents that it does not support
client.println("User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1");
client.println();
//Note, a good way to test if you are sending the right data in your request is to send the same as above
//using telnet:
//telnet graph.facebook.com 80
//...
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Connected");
//Give time for a response
boolean response_ok = true;
boolean timeout = 10;
int response_wait = 0;
while(!client.available()){
delay(100);
response_wait++;
if(response_wait > timeout * 10){
response_ok = false;
break;
}
}
//If there is a response, read it
String server_data = String();
if(response_ok){
while(client.available()){
char c = client.read();
server_data += c;
}
}
//Close the connection
client.stop();
client.flush();
//Parse out the share count
int shares_start = server_data.indexOf("\"shares\": ");
String shares = String("Error!");
if(shares_start != -1){
int shares_end = server_data.indexOf("\n",shares_start);
shares = String(server_data.substring(shares_start + 10, shares_end));
//Get the int falue of the shares count
char temp_char[shares.length() + 1];
shares.toCharArray(temp_char, sizeof(temp_char));
int new_share_count = atoi(temp_char);
if(new_share_count > share_count){
share_count = new_share_count;
play_happy_sound();
}
}
//Display The share count
lcd.clear();
lcd.setCursor(0,0);
lcd.print(shares+" Shares!");
}
//Update every 60 seconds * x minutes
delay(60000 * 15);
}
void play_happy_sound(){
tone(tone_pin, 262);
delay(1500*.025);
noTone(tone_pin);
tone(tone_pin, 262);
delay(1500*0.125);
noTone(tone_pin);
tone(tone_pin, 392);
delay(1500*0.25);
noTone(tone_pin);
}
Copyright © 2011, Aaron Blondeau
Drupal theme by Kiwi Themes.