Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.81 KB | None | 0 0
  1. /*******************************************************************
  2.  *  An compilation of library sample code written by Brian Lough            
  3.  *  https://github.com/witnessmenow/        
  4.  *                                                                  
  5.  *  Smushed together with a dash of seven segment displays by Becky Stern
  6.  *  https://www.instructables.com/id/EOGVPMLJD2FC7WM/
  7.  *******************************************************************/
  8. // requires the following libraries, search in Library Manager or download from github:
  9.  
  10. #include <Wire.h>                  // installed by default
  11. #include <Adafruit_GFX.h>          // https://github.com/adafruit/Adafruit-GFX-Library
  12. #include "Adafruit_LEDBackpack.h"  // https://github.com/adafruit/Adafruit_LED_Backpack
  13. #include <TwitterApi.h>            // https://github.com/witnessmenow/arduino-twitter-api
  14. #include <InstructablesApi.h>      // https://github.com/witnessmenow/arduino-instructables-api
  15. #include <ArduinoJson.h>           // https://github.com/bblanchon/ArduinoJson
  16. #include "InstagramStats.h"       // https://github.com/witnessmenow/arduino-instagram-stats
  17. #include "JsonStreamingParser.h"  // https://github.com/squix78/json-streaming-parser
  18.  
  19. // these libraries are included with ESP8266 support
  20. #include <ESP8266WiFi.h>
  21. #include <WiFiClientSecure.h>
  22.  
  23. //------- Replace the following! ------
  24. char ssid[] = "Network Name";       // your network SSID (name)
  25. char password[] = "pa$$w0rd";  // your network key
  26.  
  27. // Normally we would use these to generate the bearer token but its not working yet :/
  28. // Use steps on the readme to generate the Bearer Token
  29.  
  30. #define BEARER_TOKEN "longStringofLettersAndNumbers"
  31.  
  32. //Using curl to get bearer token
  33. // curl -u "$CONSUMER_KEY:$CONSUMER_SECRET" \
  34. //    --data 'grant_type=client_credentials' \
  35. //    'https://api.twitter.com/oauth2/token'
  36.  
  37. WiFiClientSecure secureClient;
  38. WiFiClient client;
  39. TwitterApi TwitterStats(secureClient);
  40. InstagramStats instaStats(secureClient);
  41. InstructablesApi iblesStats(client);
  42.  
  43. unsigned long api_delay = 1 * 60000; //time between api requests (1mins)
  44. unsigned long api_due_time;
  45.  
  46. //Inputs
  47.  
  48. String screenName = "TwitterName";  // Twitter
  49. String SCREEN_NAME = "InstructablesName";  // Instructables
  50. String userName = "InstagramName";    // from their instagram url https://www.instagram.com/userName/
  51.  
  52. bool haveBearerToken = false;
  53.  
  54. // label the displays with their i2c addresses
  55. struct {
  56.   uint8_t           addr;         // I2C address
  57.   Adafruit_7segment seg7;         // 7segment object
  58. } disp[] = {
  59.   { 0x71, Adafruit_7segment() },  // High digits Twitter
  60.   { 0x70, Adafruit_7segment() },  // Low digits Twitter
  61.   { 0x74, Adafruit_7segment() },  // High digits Instagram
  62.   { 0x73, Adafruit_7segment() },  // Low digits Instagram
  63.   { 0x75, Adafruit_7segment() },  // High digits Instructables
  64.   { 0x72, Adafruit_7segment() }   // Low digits Instructables
  65. };
  66.  
  67. void setup() {
  68.  
  69.   Serial.begin(115200);
  70.  
  71.   for(uint8_t i=0; i<6; i++) {       // Initialize displays
  72.     disp[i].seg7.begin(disp[i].addr);
  73.     disp[i].seg7.clear();
  74.     disp[i].seg7.writeDisplay();
  75.   }
  76.  
  77.   // Set WiFi to station mode and disconnect from an AP if it was Previously
  78.   // connected
  79.   WiFi.mode(WIFI_STA);
  80.   WiFi.disconnect();
  81.   delay(100);
  82.  
  83.   // Attempt to connect to Wifi network:
  84.   Serial.print("Connecting Wifi: ");
  85.   Serial.println(ssid);
  86.   WiFi.begin(ssid, password);
  87.   while (WiFi.status() != WL_CONNECTED) {
  88.     Serial.print(".");
  89.     delay(500);
  90.   }
  91.   Serial.println("");
  92.   Serial.println("WiFi connected");
  93.   Serial.println("IP address: ");
  94.   IPAddress ip = WiFi.localIP();
  95.   Serial.println(ip);
  96.  
  97.   TwitterStats.setBearerToken(BEARER_TOKEN);
  98.   haveBearerToken = true;
  99.   //getTwitterStats(screenName);
  100.  
  101. }
  102.  
  103. void loop() {
  104.  if (millis() > api_due_time)  {
  105.     getInstructablesStats();
  106.     delay(200);
  107.     if(haveBearerToken){
  108.       getTwitterStats(screenName);
  109.     }
  110.     delay(200);
  111.     getInstagramStatsForUser();
  112.     api_due_time = millis() + api_delay;
  113.   }
  114.  
  115. }
  116.  
  117. void getTwitterStats(String name) {
  118.   Serial.println("Getting twitter stats for " + name);
  119.    String responseString = TwitterStats.getUserStatistics(name);
  120.     Serial.println(responseString);
  121.     DynamicJsonBuffer jsonBuffer;
  122.     JsonObject& response = jsonBuffer.parseObject(responseString);
  123.     if (response.success()) {
  124.       Serial.println("parsed Json");
  125.       // Use Arduino Json to parse the data
  126.       int followerCount = response["followers_count"];
  127.       uint16_t hi = followerCount / 10000, // Value on left (high digits) display
  128.                lo = followerCount % 10000; // Value on right (low digits) display
  129.       disp[0].seg7.print(hi, DEC);   // Write values to each display...
  130.       disp[1].seg7.print(lo, DEC);
  131.  
  132.       // print() does not zero-pad the displays; this may produce a gap
  133.       // between the high and low sections. Here we add zeros where needed...
  134.       if(hi) {
  135.         if(lo < 1000) {
  136.           disp[1].seg7.writeDigitNum(0, 0);
  137.           if(lo < 100) {
  138.             disp[1].seg7.writeDigitNum(1, 0);
  139.             if(lo < 10) {
  140.               disp[1].seg7.writeDigitNum(3, 0);
  141.             }
  142.           }
  143.         }
  144.        } else {
  145.          disp[0].seg7.clear(); // Clear 'hi' display
  146.         }
  147.        disp[0].seg7.writeDisplay(); // Push data to displays
  148.        disp[1].seg7.writeDisplay();
  149.     } else {
  150.       Serial.println("Failed to parse Json");
  151.     }
  152. }
  153.  
  154. void getInstagramStatsForUser() {
  155.   Serial.println("Getting instagram user stats for " + userName );
  156.   InstagramUserStats response = instaStats.getUserStats(userName);
  157.   Serial.println("Response:");
  158.   Serial.print("Number of followers: ");
  159.   Serial.println(response.followedByCount);
  160.   uint16_t hi = response.followedByCount / 10000, // Value on left (high digits) display
  161.            lo = response.followedByCount % 10000; // Value on right (low digits) display
  162.       disp[2].seg7.print(hi, DEC);   // Write values to each display...
  163.       disp[3].seg7.print(lo, DEC);
  164.  
  165.       // print() does not zero-pad the displays; this may produce a gap
  166.       // between the high and low sections. Here we add zeros where needed...
  167.       if(hi) {
  168.         if(lo < 1000) {
  169.           disp[3].seg7.writeDigitNum(0, 0);
  170.           if(lo < 100) {
  171.             disp[3].seg7.writeDigitNum(1, 0);
  172.             if(lo < 10) {
  173.               disp[3].seg7.writeDigitNum(3, 0);
  174.             }
  175.           }
  176.         }
  177.        } else {
  178.          disp[2].seg7.clear(); // Clear 'hi' display
  179.         }
  180.        disp[2].seg7.writeDisplay(); // Push data to displays
  181.        disp[3].seg7.writeDisplay();
  182. }
  183.  
  184. void getInstructablesStats() {
  185.       instructablesAuthorStats stats;
  186.     stats = iblesStats.getAuthorStats(SCREEN_NAME);
  187.     if(stats.error.equals(""))
  188.     {
  189.       Serial.println("---------Author Stats---------");
  190.       Serial.print("Views: ");
  191.       Serial.println(stats.views);
  192.       Serial.print("Followers: ");
  193.       Serial.println(stats.followersCount);
  194.       Serial.print("Instructables Count: ");
  195.       Serial.println(stats.instructablesCount);
  196.       Serial.print("Featured Count: ");
  197.       Serial.println(stats.featuredCount);
  198.  
  199.       // The following variables are available from the response (e.g. stats.collaborationsCount)
  200.       /*
  201.       long commentCount;
  202.       long views;
  203.       long featuredCount;
  204.       long favoritesCount;
  205.       long instructablesCount;
  206.       long publishedCollectionsCount;
  207.       long lessonCount;
  208.       long courseCount;
  209.       long topicsCount;
  210.       long questionsCount;
  211.       long answersCount;
  212.       long bestAnswersCount;
  213.       long followersCount;
  214.       long subscriptionsCount;
  215.       long collaborationsCount;
  216.       String error // This will be blank if it gets in here
  217.       */
  218.       Serial.println("------------------------");
  219.       uint16_t hi = stats.followersCount / 10000, // Value on left (high digits) display
  220.            lo = stats.followersCount % 10000; // Value on right (low digits) display
  221.       disp[4].seg7.print(hi, DEC);   // Write values to each display...
  222.       disp[5].seg7.print(lo, DEC);
  223.  
  224.       // print() does not zero-pad the displays; this may produce a gap
  225.       // between the high and low sections. Here we add zeros where needed...
  226.       if(hi) {
  227.         if(lo < 1000) {
  228.           disp[5].seg7.writeDigitNum(0, 0);
  229.           if(lo < 100) {
  230.             disp[5].seg7.writeDigitNum(1, 0);
  231.             if(lo < 10) {
  232.               disp[5].seg7.writeDigitNum(3, 0);
  233.             }
  234.           }
  235.         }
  236.        } else {
  237.          disp[4].seg7.clear(); // Clear 'hi' display
  238.         }
  239.        disp[4].seg7.writeDisplay(); // Push data to displays
  240.        disp[5].seg7.writeDisplay();
  241.     } else {
  242.       Serial.print("Error getting Author Stats: ");
  243.       Serial.println(stats.error);
  244.       disp[5].seg7.print(2029, DEC);
  245.       disp[5].seg7.writeDisplay();
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement