|
| 1 | +/******************************************************************* |
| 2 | + * Read YouTube Channel statistics from the YouTube API * |
| 3 | + * This sketch uses the WiFiManager Library for configuraiton * |
| 4 | + * * |
| 5 | + * By Brian Lough * |
| 6 | + * https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA * |
| 7 | + *******************************************************************/ |
| 8 | + |
| 9 | +#include <YoutubeApi.h> |
| 10 | +#include <ESP8266WiFi.h> |
| 11 | +#include <WiFiClientSecure.h> |
| 12 | + |
| 13 | +// For storing configurations |
| 14 | +#include "FS.h" |
| 15 | +#include <ArduinoJson.h> |
| 16 | + |
| 17 | +// WiFiManager Libraries |
| 18 | +#include <DNSServer.h> //Local DNS Server used for redirecting all rs to the configuration portal |
| 19 | +#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal |
| 20 | +#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic |
| 21 | + |
| 22 | +const int resetConfigPin = D8; //When high will reset the wifi manager config |
| 23 | + |
| 24 | +char apiKey[45] = ""; |
| 25 | +char channelId[30] = "UCezJOfu7OtqGzd5xrP3q6WA"; |
| 26 | + |
| 27 | +WiFiClientSecure client; |
| 28 | +YoutubeApi *api; |
| 29 | + |
| 30 | +unsigned long api_mtbs = 60000; //mean time between api requests |
| 31 | +unsigned long api_lasttime; //last time api request has been done |
| 32 | + |
| 33 | +long subs = 0; |
| 34 | + |
| 35 | +// flag for saving data |
| 36 | +bool shouldSaveConfig = false; |
| 37 | + |
| 38 | +//callback notifying us of the need to save config |
| 39 | +void saveConfigCallback () { |
| 40 | + Serial.println("Should save config"); |
| 41 | + shouldSaveConfig = true; |
| 42 | +} |
| 43 | + |
| 44 | +void setup() { |
| 45 | + |
| 46 | + Serial.begin(115200); |
| 47 | + |
| 48 | + if (!SPIFFS.begin()) { |
| 49 | + Serial.println("Failed to mount FS"); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output |
| 54 | + digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level |
| 55 | + loadConfig(); |
| 56 | + |
| 57 | + WiFiManager wifiManager; |
| 58 | + wifiManager.setSaveConfigCallback(saveConfigCallback); |
| 59 | + |
| 60 | + // Adding an additional config on the WIFI manager webpage for the API Key and Channel ID |
| 61 | + WiFiManagerParameter customApiKey("apiKey", "API Key", apiKey, 50); |
| 62 | + WiFiManagerParameter customChannelId("channelId", "Channel ID", channelId, 35); |
| 63 | + wifiManager.addParameter(&customApiKey); |
| 64 | + wifiManager.addParameter(&customChannelId); |
| 65 | + |
| 66 | + // If it fails to connect it will create a YouTube-Counter access point |
| 67 | + wifiManager.autoConnect("YouTube-Counter", "supersecret"); |
| 68 | + |
| 69 | + strcpy(apiKey, customApiKey.getValue()); |
| 70 | + strcpy(channelId, customChannelId.getValue()); |
| 71 | + |
| 72 | + if (shouldSaveConfig) { |
| 73 | + saveConfig(); |
| 74 | + } |
| 75 | + |
| 76 | + digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH |
| 77 | + // Force Config mode if there is no API key |
| 78 | + if(strcmp(apiKey, "") > 0) { |
| 79 | + Serial.println("Init YouTube API"); |
| 80 | + api = new YoutubeApi(apiKey, client); |
| 81 | + } else { |
| 82 | + Serial.println("Forcing Config Mode"); |
| 83 | + forceConfigMode(); |
| 84 | + } |
| 85 | + api = new YoutubeApi(apiKey, client); |
| 86 | + Serial.println(""); |
| 87 | + Serial.println("WiFi connected"); |
| 88 | + Serial.println("IP address: "); |
| 89 | + IPAddress ip = WiFi.localIP(); |
| 90 | + Serial.println(ip); |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | +bool loadConfig() { |
| 95 | + File configFile = SPIFFS.open("/config.json", "r"); |
| 96 | + if (!configFile) { |
| 97 | + Serial.println("Failed to open config file"); |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + size_t size = configFile.size(); |
| 102 | + if (size > 1024) { |
| 103 | + Serial.println("Config file size is too large"); |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + // Allocate a buffer to store contents of the file. |
| 108 | + std::unique_ptr<char[]> buf(new char[size]); |
| 109 | + |
| 110 | + configFile.readBytes(buf.get(), size); |
| 111 | + |
| 112 | + StaticJsonBuffer<200> jsonBuffer; |
| 113 | + JsonObject& json = jsonBuffer.parseObject(buf.get()); |
| 114 | + |
| 115 | + if (!json.success()) { |
| 116 | + Serial.println("Failed to parse config file"); |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + strcpy(apiKey, json["apiKey"]); |
| 121 | + strcpy(channelId, json["channelId"]); |
| 122 | + return true; |
| 123 | +} |
| 124 | + |
| 125 | +bool saveConfig() { |
| 126 | + StaticJsonBuffer<200> jsonBuffer; |
| 127 | + JsonObject& json = jsonBuffer.createObject(); |
| 128 | + json["apiKey"] = apiKey; |
| 129 | + json["channelId"] = channelId; |
| 130 | + |
| 131 | + File configFile = SPIFFS.open("/config.json", "w"); |
| 132 | + if (!configFile) { |
| 133 | + Serial.println("Failed to open config file for writing"); |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + json.printTo(configFile); |
| 138 | + return true; |
| 139 | +} |
| 140 | + |
| 141 | +void forceConfigMode() { |
| 142 | + Serial.println("Reset"); |
| 143 | + WiFi.disconnect(); |
| 144 | + Serial.println("Dq"); |
| 145 | + delay(500); |
| 146 | + ESP.reset(); |
| 147 | + delay(5000); |
| 148 | +} |
| 149 | + |
| 150 | +void loop() { |
| 151 | + |
| 152 | + if (digitalRead(resetConfigPin) == HIGH) { |
| 153 | + forceConfigMode(); |
| 154 | + } |
| 155 | + if (millis() - api_lasttime > api_mtbs) { |
| 156 | + if(api->getChannelStatistics(channelId)) |
| 157 | + { |
| 158 | + Serial.println("---------Stats---------"); |
| 159 | + Serial.print("Subscriber Count: "); |
| 160 | + Serial.println(api->channelStats.subscriberCount); |
| 161 | + Serial.print("View Count: "); |
| 162 | + Serial.println(api->channelStats.viewCount); |
| 163 | + Serial.print("Comment Count: "); |
| 164 | + Serial.println(api->channelStats.commentCount); |
| 165 | + Serial.print("Video Count: "); |
| 166 | + Serial.println(api->channelStats.videoCount); |
| 167 | + // Probably not needed :) |
| 168 | + //Serial.print("hiddenSubscriberCount: "); |
| 169 | + //Serial.println(api->channelStats.hiddenSubscriberCount); |
| 170 | + Serial.println("------------------------"); |
| 171 | + |
| 172 | + } |
| 173 | + api_lasttime = millis(); |
| 174 | + } |
| 175 | +} |
0 commit comments