Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************
- * An compilation of library sample code written by Brian Lough
- * https://github.com/witnessmenow/
- *
- * Smushed together with a dash of seven segment displays by Becky Stern
- * https://www.instructables.com/id/EOGVPMLJD2FC7WM/
- *******************************************************************/
- // requires the following libraries, search in Library Manager or download from github:
- #include <Wire.h> // installed by default
- #include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
- #include "Adafruit_LEDBackpack.h" // https://github.com/adafruit/Adafruit_LED_Backpack
- #include <TwitterApi.h> // https://github.com/witnessmenow/arduino-twitter-api
- #include <InstructablesApi.h> // https://github.com/witnessmenow/arduino-instructables-api
- #include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
- #include "InstagramStats.h" // https://github.com/witnessmenow/arduino-instagram-stats
- #include "JsonStreamingParser.h" // https://github.com/squix78/json-streaming-parser
- // these libraries are included with ESP8266 support
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- //------- Replace the following! ------
- char ssid[] = "Network Name"; // your network SSID (name)
- char password[] = "pa$$w0rd"; // your network key
- // Normally we would use these to generate the bearer token but its not working yet :/
- // Use steps on the readme to generate the Bearer Token
- #define BEARER_TOKEN "longStringofLettersAndNumbers"
- //Using curl to get bearer token
- // curl -u "$CONSUMER_KEY:$CONSUMER_SECRET" \
- // --data 'grant_type=client_credentials' \
- // 'https://api.twitter.com/oauth2/token'
- WiFiClientSecure secureClient;
- WiFiClient client;
- TwitterApi TwitterStats(secureClient);
- InstagramStats instaStats(secureClient);
- InstructablesApi iblesStats(client);
- unsigned long api_delay = 1 * 60000; //time between api requests (1mins)
- unsigned long api_due_time;
- //Inputs
- String screenName = "TwitterName"; // Twitter
- String SCREEN_NAME = "InstructablesName"; // Instructables
- String userName = "InstagramName"; // from their instagram url https://www.instagram.com/userName/
- bool haveBearerToken = false;
- // label the displays with their i2c addresses
- struct {
- uint8_t addr; // I2C address
- Adafruit_7segment seg7; // 7segment object
- } disp[] = {
- { 0x71, Adafruit_7segment() }, // High digits Twitter
- { 0x70, Adafruit_7segment() }, // Low digits Twitter
- { 0x74, Adafruit_7segment() }, // High digits Instagram
- { 0x73, Adafruit_7segment() }, // Low digits Instagram
- { 0x75, Adafruit_7segment() }, // High digits Instructables
- { 0x72, Adafruit_7segment() } // Low digits Instructables
- };
- void setup() {
- Serial.begin(115200);
- for(uint8_t i=0; i<6; i++) { // Initialize displays
- disp[i].seg7.begin(disp[i].addr);
- disp[i].seg7.clear();
- disp[i].seg7.writeDisplay();
- }
- // Set WiFi to station mode and disconnect from an AP if it was Previously
- // connected
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- delay(100);
- // Attempt to connect to Wifi network:
- Serial.print("Connecting Wifi: ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- IPAddress ip = WiFi.localIP();
- Serial.println(ip);
- TwitterStats.setBearerToken(BEARER_TOKEN);
- haveBearerToken = true;
- //getTwitterStats(screenName);
- }
- void loop() {
- if (millis() > api_due_time) {
- getInstructablesStats();
- delay(200);
- if(haveBearerToken){
- getTwitterStats(screenName);
- }
- delay(200);
- getInstagramStatsForUser();
- api_due_time = millis() + api_delay;
- }
- }
- void getTwitterStats(String name) {
- Serial.println("Getting twitter stats for " + name);
- String responseString = TwitterStats.getUserStatistics(name);
- Serial.println(responseString);
- DynamicJsonBuffer jsonBuffer;
- JsonObject& response = jsonBuffer.parseObject(responseString);
- if (response.success()) {
- Serial.println("parsed Json");
- // Use Arduino Json to parse the data
- int followerCount = response["followers_count"];
- uint16_t hi = followerCount / 10000, // Value on left (high digits) display
- lo = followerCount % 10000; // Value on right (low digits) display
- disp[0].seg7.print(hi, DEC); // Write values to each display...
- disp[1].seg7.print(lo, DEC);
- // print() does not zero-pad the displays; this may produce a gap
- // between the high and low sections. Here we add zeros where needed...
- if(hi) {
- if(lo < 1000) {
- disp[1].seg7.writeDigitNum(0, 0);
- if(lo < 100) {
- disp[1].seg7.writeDigitNum(1, 0);
- if(lo < 10) {
- disp[1].seg7.writeDigitNum(3, 0);
- }
- }
- }
- } else {
- disp[0].seg7.clear(); // Clear 'hi' display
- }
- disp[0].seg7.writeDisplay(); // Push data to displays
- disp[1].seg7.writeDisplay();
- } else {
- Serial.println("Failed to parse Json");
- }
- }
- void getInstagramStatsForUser() {
- Serial.println("Getting instagram user stats for " + userName );
- InstagramUserStats response = instaStats.getUserStats(userName);
- Serial.println("Response:");
- Serial.print("Number of followers: ");
- Serial.println(response.followedByCount);
- uint16_t hi = response.followedByCount / 10000, // Value on left (high digits) display
- lo = response.followedByCount % 10000; // Value on right (low digits) display
- disp[2].seg7.print(hi, DEC); // Write values to each display...
- disp[3].seg7.print(lo, DEC);
- // print() does not zero-pad the displays; this may produce a gap
- // between the high and low sections. Here we add zeros where needed...
- if(hi) {
- if(lo < 1000) {
- disp[3].seg7.writeDigitNum(0, 0);
- if(lo < 100) {
- disp[3].seg7.writeDigitNum(1, 0);
- if(lo < 10) {
- disp[3].seg7.writeDigitNum(3, 0);
- }
- }
- }
- } else {
- disp[2].seg7.clear(); // Clear 'hi' display
- }
- disp[2].seg7.writeDisplay(); // Push data to displays
- disp[3].seg7.writeDisplay();
- }
- void getInstructablesStats() {
- instructablesAuthorStats stats;
- stats = iblesStats.getAuthorStats(SCREEN_NAME);
- if(stats.error.equals(""))
- {
- Serial.println("---------Author Stats---------");
- Serial.print("Views: ");
- Serial.println(stats.views);
- Serial.print("Followers: ");
- Serial.println(stats.followersCount);
- Serial.print("Instructables Count: ");
- Serial.println(stats.instructablesCount);
- Serial.print("Featured Count: ");
- Serial.println(stats.featuredCount);
- // The following variables are available from the response (e.g. stats.collaborationsCount)
- /*
- long commentCount;
- long views;
- long featuredCount;
- long favoritesCount;
- long instructablesCount;
- long publishedCollectionsCount;
- long lessonCount;
- long courseCount;
- long topicsCount;
- long questionsCount;
- long answersCount;
- long bestAnswersCount;
- long followersCount;
- long subscriptionsCount;
- long collaborationsCount;
- String error // This will be blank if it gets in here
- */
- Serial.println("------------------------");
- uint16_t hi = stats.followersCount / 10000, // Value on left (high digits) display
- lo = stats.followersCount % 10000; // Value on right (low digits) display
- disp[4].seg7.print(hi, DEC); // Write values to each display...
- disp[5].seg7.print(lo, DEC);
- // print() does not zero-pad the displays; this may produce a gap
- // between the high and low sections. Here we add zeros where needed...
- if(hi) {
- if(lo < 1000) {
- disp[5].seg7.writeDigitNum(0, 0);
- if(lo < 100) {
- disp[5].seg7.writeDigitNum(1, 0);
- if(lo < 10) {
- disp[5].seg7.writeDigitNum(3, 0);
- }
- }
- }
- } else {
- disp[4].seg7.clear(); // Clear 'hi' display
- }
- disp[4].seg7.writeDisplay(); // Push data to displays
- disp[5].seg7.writeDisplay();
- } else {
- Serial.print("Error getting Author Stats: ");
- Serial.println(stats.error);
- disp[5].seg7.print(2029, DEC);
- disp[5].seg7.writeDisplay();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement