Facebook likes on display

Hi everyone,
I would like to display number of likes of certain FB account. I'm using Arduino Yun + LCD keypad shield. I've already tried it using this manual (Facebook Like Box | Skolti). There are few differences because I want to use Arduino Yun instead of Arduino Uno + Ethernet shield (used in source manual) and connection via WiFi (not Ethernet).
I don't know what should I change in the code to run it. It's not so difficult but I don't have any experiences with programming Arduino.
Another problem I have is MAC address of Arduino Yun board. I don't know how to convert standard form of MAC address (90:A2:DA:F7:08:5A) to byte form (e.g. 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01).
Could you please help with these issues?
Thank you for any advice :slight_smile:

With the Yun it is a different approach. Now all the web stuff is handled by the linux side.

You can use the HttpClient class provided by the Bridge library

to do the same thing you do with client.print in the sketch you linked.

From the code you posted you can take information about the Facebook API to retrieve the data you want.
For example, from here

client.println("GET /restserver.php?format=xml&method=fql.multiquery&pretty=0&queries={%22page_info%22%3A%22select%20name%2Cfan_count%2Cpage_url%2Ctype%20from%20page%20where%20page_id%20IN%20(27479046178)%22}&sdk=joey HTTP/1.1");

you can extract the request that you need in order to get likes number.

About the MAC problem, you need to write some code that analyzes each character of the string (90:A2:DA:F7:08:5A) and the write the exact data in byte form.

for example

myByte=0;
if (string(x) == 'A') myByte = 10 << 4 // for the high side of the byte
if (string(x) =='2') myByte += 2 // for the low side of the byte

This is only an example code that convert a character to the respective value. You have to parse the entire string and check if it is the value of the low side or the high side of the byte.

The byte form of the MAC address is decoded simply, since the MAC address is already in bytes:

MAC address (90:A2:DA:F7:08:5A) in byte form is (0x90, 0xA2, 0xDA, 0xF7, 0x08, 0x5A).

Like Angelo says, networking with the Yun is completely different than using the Ethernet shield. Fortunately, it's much easier.

Looking at that sketch, everything dealing with the following simply goes away:

  • Ethernet.h
  • mac address definition
  • IPAddress definition
  • EthernetClient
  • Getting a DHCP address

All of that is specific to the Ethernet shield, and simply doesn't apply for the Yun.

Take a look at the HttpClient Example and see how different it is from your sample sketch. All of the initialization code is simply replaced by Bridge.begin().

In the example, where it says:

 client.get("http://arduino.cc/asciilogo.txt");

You will instead use something like:

 client.get("http://api-read.facebook.com/restserver.php?format=xml&method=fql.multiquery...");

(I didn't paste in the whole URL, you are likely to want to change some details in there anyway...)

Then, where the example has:

while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  Serial.flush();

  delay(5000);
}

you will instead use the logic that is in the loop() function of your sample sketch. (Fortunately, the HttpClient object supports the same sort of .connected(), .available() and .read() calls as the EthernetClient object, so that part should be usable just as is.)

Play with the HttpClient example and see how it works. Then, going back to your sample sketch, you can strip out most of what is in setup(), just put in the Bridge.begin(). Leave loop() mostly alone, I think it will work as-is. Then, in connectToServer(), take out the whole IF statement with the client.print calls in it, and replace it with the single client.get() call described above. I think that will get you 99% of the way there.

Finally, why do you want to translate the Yun's MAC address? You probably don't need it. The EthernetShield does not have its own MAC address, so the sketch must define one. But the Yun already has a predefined MAX address and you don't have to do anything with it for most normal usage. You certainly don't need it for the purposes of this sketch.

I've been trying to figure it out somehow, but there's still something wrong or missing. I don't know how to get the "likes" count from graph.facebook API and then display it using lcd keypad shield. I wrote some code according to httpclient example (attachment) but I still don't know what should I add to complete this issue...

likes.txt (845 Bytes)

337,174 Total Page Likes Arduino

Get app_id, app_secret for facebook.

access_token=app_id|app_secret
nano /mnt/sda1/facebook.py
#!/usr/bin/python
import urllib, json
pagename="official.arduino"
accessToken = "app_id|app_secret"
url='https://graph.facebook.com/v2.3/'+pagename+'?access_token='+accessToken
data = json.load(urllib.urlopen(url))
#print data
print data['likes']
chmod 755 /mnt/sda1/facebook.py
/mnt/sda1/facebook.py
337174

ATmega32u4 code:

#include <Process.h>
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(4, 5, 10, 11, 12, 13);
void setup() {
  Bridge.begin();	// Initialize the Bridge
  Serial.begin(9600);	// Initialize the Serial
  // Wait until a Serial Monitor is connected.
  while (!Serial);
  //  lcd.begin(16, 2);
}
void loop() {
  Process p;
  String likes = "";
  p.runShellCommand("/mnt/sda1/facebook.py");
  while (p.available() > 0) {
    char c = p.read();
    likes.concat(c);
    //  lcd.print(c);
  }
  if (likes != "") {
    Serial.println(likes);
  }
  delay(5000);
}