Skip to content

Commit a17d89e

Browse files
committed
Updating ESP8266 Channel Stats example. not working though
1 parent c6d9c7e commit a17d89e

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

examples/ESP8266/ChannelStatistics/ChannelStatistics.ino

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
/*******************************************************************
2-
* Read YouTube Channel statistics from the YouTube API *
3-
* *
4-
* By Brian Lough *
5-
* https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA *
2+
Read YouTube Channel statistics from the YouTube API
3+
4+
Parts:
5+
D1 Mini ESP8266 (or any ESP8266) * - http://s.click.aliexpress.com/e/uzFUnIe
6+
* = Affilate
7+
8+
If you find what I do useful and would like to support me,
9+
please consider becoming a sponsor on Github
10+
https://github.com/sponsors/witnessmenow/
11+
12+
Written by Brian Lough
13+
YouTube: https://www.youtube.com/brianlough
14+
Tindie: https://www.tindie.com/stores/brianlough/
15+
Twitter: https://twitter.com/witnessmenow
616
*******************************************************************/
717

8-
#include <YoutubeApi.h>
18+
// ----------------------------
19+
// Standard Libraries
20+
// ----------------------------
21+
922
#include <ESP8266WiFi.h>
1023
#include <WiFiClientSecure.h>
1124

12-
#include <ArduinoJson.h> // This Sketch doesn't technically need this, but the library does so it must be installed.
25+
// ----------------------------
26+
// Additional Libraries - each one of these will need to be installed.
27+
// ----------------------------
28+
29+
#include <YoutubeApi.h>
30+
// Library for connecting to the Youtube API
31+
32+
// Search for "youtube" in the Arduino Library Manager
33+
// https://github.com/witnessmenow/arduino-youtube-api
34+
35+
#include <ArduinoJson.h>
36+
// Library used for parsing Json from the API responses
37+
38+
// Search for "Arduino Json" in the Arduino Library manager
39+
// https://github.com/bblanchon/ArduinoJson
1340

1441
//------- Replace the following! ------
1542
char ssid[] = "xxx"; // your network SSID (name)
1643
char password[] = "yyyy"; // your network key
1744
#define API_KEY "zzzz" // your google apps API Token
1845
#define CHANNEL_ID "UCezJOfu7OtqGzd5xrP3q6WA" // makes up the url of channel
19-
46+
//------- ---------------------- ------
2047

2148
WiFiClientSecure client;
2249
YoutubeApi api(API_KEY, client);
2350

24-
unsigned long api_mtbs = 60000; //mean time between api requests
25-
unsigned long api_lasttime; //last time api request has been done
51+
unsigned long timeBetweenRequests = 60000;
52+
unsigned long nextRunTime;
2653

2754
long subs = 0;
2855

@@ -50,12 +77,13 @@ void setup() {
5077
IPAddress ip = WiFi.localIP();
5178
Serial.println(ip);
5279

53-
80+
// Required if you are using ESP8266 V2.5 or above
81+
client.setInsecure();
5482
}
5583

5684
void loop() {
5785

58-
if (millis() - api_lasttime > api_mtbs) {
86+
if (millis() > nextRunTime) {
5987
if(api.getChannelStatistics(CHANNEL_ID))
6088
{
6189
Serial.println("---------Stats---------");
@@ -73,6 +101,6 @@ void loop() {
73101
Serial.println("------------------------");
74102

75103
}
76-
api_lasttime = millis();
104+
nextRunTime = millis() + timeBetweenRequests;
77105
}
78106
}

src/YoutubeApi.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ int YoutubeApi::sendGetToYoutube(char *command) {
7171

7272
bool YoutubeApi::getChannelStatistics(String channelId){
7373

74+
Serial.println("Deprecated, user a char* rather than String");
7475
int strLen = channelId.length() + 1;
7576
char tempStr[strLen];
7677
channelId.toCharArray(tempStr, strLen);
@@ -82,6 +83,8 @@ bool YoutubeApi::getChannelStatistics(char *channelId){
8283
char command[150] = YTAPI_CHANNEL_ENDPOINT;
8384
strcat(command, "?part=statistics&id=%s");
8485
sprintf(command, command, channelId);
86+
strcat(command, "&key=%s");
87+
sprintf(command, command, _apiKey);
8588

8689
if (_debug)
8790
{
@@ -96,7 +99,9 @@ bool YoutubeApi::getChannelStatistics(char *channelId){
9699
+ 2*JSON_OBJECT_SIZE(4)
97100
+ JSON_OBJECT_SIZE(5);
98101

99-
if (sendGetToYoutube(command) == 200)
102+
int httpStatus = sendGetToYoutube(command);
103+
104+
if (httpStatus == 200)
100105
{
101106
// Allocate DynamicJsonDocument
102107
DynamicJsonDocument doc(bufferSize);
@@ -120,6 +125,9 @@ bool YoutubeApi::getChannelStatistics(char *channelId){
120125
Serial.print(F("deserializeJson() failed with code "));
121126
Serial.println(error.c_str());
122127
}
128+
} else {
129+
Serial.print("Unexpected HTTP Status Code: ");
130+
Serial.println(httpStatus);
123131
}
124132
closeClient();
125133

src/YoutubeApi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class YoutubeApi
5353
{
5454
public:
5555
YoutubeApi (char *apiKey, Client &client);
56-
DEPRECATED YoutubeApi (String apiKey, Client &client);
56+
YoutubeApi (String apiKey, Client &client);
5757
int sendGetToYoutube(char *command);
5858
bool getChannelStatistics(char *channelId);
59-
DEPRECATED bool getChannelStatistics(String channelId);
59+
bool getChannelStatistics(String channelId);
6060
channelStatistics channelStats;
6161
bool _debug = false;
6262

0 commit comments

Comments
 (0)