Helge Johnsen
Published © MIT

Facebook Fan Count

Inspired by Becky Stern's YouTube Subscriber Counter, I wanted to make a display of the number of likes for a Facebook page.

IntermediateFull instructions provided1 hour2,333
Facebook Fan Count

Things used in this project

Hardware components

LED Display Tube 4-Digit 7-segments
×1
NodeMCU Mini
×1
Ikea Ribba frame
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Facebook Fan Count

C/C++
AAA = Facebook Page ID or name
BBB = Facebook App ID
CCC = App Secret
DDD = Fingerprint
#include <ESP8266WiFi.h> 
#include <WiFiClientSecure.h> 
#include "SevenSegmentTM1637.h" 
#include <ArduinoJson.h> 
// Wifi 
const char* ssid = ""; 
const char* password = ""; 
// Facebook 
// https://github.com/gbrault/esp8266-Arduino/blob/master/doc/esp8266wifi/client-secure-examples.md 
const char* host = "graph.facebook.com"; 
const String url = "/AAA?access_token=BBB|CCC&fields=fan_count"; 
const int httpsPort = 443; 
const char* fingerprint = "DDD"; 
// Display https://github.com/bremme/arduino-tm1637 
const byte PIN_CLK = D3;   // define CLK pin (any digital pin) 
const byte PIN_DIO = D4;   // define DIO pin (any digital pin) 
String payload; 
SevenSegmentTM1637 display(PIN_CLK, PIN_DIO); 
void setup() { 
 Serial.begin(115200); 
 // Wifi 
 WiFi.begin(ssid, password); 
 Serial.println(); 
 Serial.print("Connecting "); 
 while (WiFi.status() != WL_CONNECTED) { 
   delay(500); 
   Serial.print("."); 
   display.print("CONNECTING"); 
 } 
 Serial.println(); 
 Serial.print("Connected, IP address: "); 
 Serial.println(WiFi.localIP()); 
 // Display   
 display.begin();            // initializes the display 
 display.setBacklight(100);  // set the brightness to 100 % 
 display.print("START"); 
} 
void loop() { 
 String wifistatus = "WIFI OK"; 
 // Use WiFiClientSecure class to create TLS connection 
 WiFiClientSecure client; 
 if (!client.connect(host, httpsPort)) { 
   String wifistatus = "WIFI FAIL"; 
   Serial.println("connection failed"); 
   return; 
 } 
 display.print(wifistatus); 
 if (client.verify(fingerprint, host)) { 
   Serial.println("certificate matches"); 
 } else { 
   Serial.println("certificate doesn't match"); 
 } 
 client.print(String("GET ") + url + " HTTP/1.1\r\n" + 
              "Host: " + host + "\r\n" + 
              "User-Agent: BuildFailureDetectorESP8266\r\n" + 
              "Connection: close\r\n\r\n"); 
 Serial.println("request sent"); 
 while (client.connected()) { 
   String line = client.readStringUntil('\n'); 
   if (line == "\r") { 
     Serial.println("headers received"); 
     break; 
   } 
 } 
 // JSON 
 String line = client.readStringUntil('\n'); 
 DynamicJsonBuffer jsonBuffer; 
 JsonObject& root = jsonBuffer.parseObject(line); 
 int fancount = root[String("fan_count")]; 
 if (fancount > 0) { 
   Serial.print("Fans: "); 
   Serial.println(fancount); 
   display.clear(); 
   display.print(fancount); 
 } else { 
   display.clear(); 
   display.print("ERR1"); 
   display.blink(); 
 } 
 delay(30000); 
} 

Credits

Helge Johnsen

Helge Johnsen

0 projects • 0 followers
Playing with electronics at my spare time.
Thanks to Becky Stern.

Comments