Skip to content

Commit 1610fb6

Browse files
committed
Added support for ESP32
1 parent e295021 commit 1610fb6

File tree

4 files changed

+101
-9
lines changed

4 files changed

+101
-9
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*******************************************************************
2+
* Read YouTube Channel statistics from the YouTube API using an
3+
* ESP32
4+
*
5+
* By Brian Lough
6+
* https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA
7+
*******************************************************************/
8+
9+
#include <YoutubeApi.h>
10+
#include <WiFi.h>
11+
#include <WiFiClientSecure.h>
12+
13+
#include <ArduinoJson.h> // This Sketch doesn't technically need this, but the library does so it must be installed.
14+
15+
//------- Replace the following! ------
16+
char ssid[] = "ssid"; // your network SSID (name)
17+
char password[] = "password"; // your network key
18+
#define API_KEY "ENTER_YOUR_API_KEY" // your google apps API Token
19+
#define CHANNEL_ID "UCezJOfu7OtqGzd5xrP3q6WA" // makes up the url of channel
20+
21+
22+
WiFiClientSecure client;
23+
YoutubeApi api(API_KEY, client);
24+
25+
unsigned long api_mtbs = 60000; //mean time between api requests
26+
unsigned long api_lasttime; //last time api request has been done
27+
28+
long subs = 0;
29+
30+
void setup() {
31+
32+
Serial.begin(115200);
33+
34+
// Attempt to connect to Wifi network:
35+
Serial.print("Connecting Wifi: ");
36+
Serial.println(ssid);
37+
38+
/* Explicitly set the ESP32 to be a WiFi-client, otherwise, it by default,
39+
would try to act as both a client and an access-point and could cause
40+
network-issues with your other WiFi-devices on your WiFi-network. */
41+
WiFi.mode(WIFI_STA);
42+
WiFi.begin(ssid, password);
43+
while (WiFi.status() != WL_CONNECTED) {
44+
Serial.print(".");
45+
delay(500);
46+
}
47+
Serial.println("");
48+
Serial.println("WiFi connected");
49+
Serial.println("IP address: ");
50+
IPAddress ip = WiFi.localIP();
51+
Serial.println(ip);
52+
53+
54+
}
55+
56+
void loop() {
57+
58+
if (millis() - api_lasttime > api_mtbs) {
59+
if(api.getChannelStatistics(CHANNEL_ID))
60+
{
61+
Serial.println("---------Stats---------");
62+
Serial.print("Subscriber Count: ");
63+
Serial.println(api.channelStats.subscriberCount);
64+
Serial.print("View Count: ");
65+
Serial.println(api.channelStats.viewCount);
66+
Serial.print("Comment Count: ");
67+
Serial.println(api.channelStats.commentCount);
68+
Serial.print("Video Count: ");
69+
Serial.println(api.channelStats.videoCount);
70+
// Probably not needed :)
71+
//Serial.print("hiddenSubscriberCount: ");
72+
//Serial.println(api.channelStats.hiddenSubscriberCount);
73+
Serial.println("------------------------");
74+
75+
}
76+
api_lasttime = millis();
77+
}
78+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=YoutubeApi
2-
version=1.0.0
2+
version=1.1.0
33
author=Brian Lough
44
maintainer=Brian Lough <[email protected]>
55
sentence=A wrapper for the YouTube API for Arduino (supports ESP8266 & WiFi101 boards)

src/YoutubeApi.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ String YoutubeApi::sendGetToYoutube(String command) {
3535
bool avail;
3636
// Connect with youtube api over ssl
3737
if (client->connect(YTAPI_HOST, YTAPI_SSL_PORT)) {
38-
// Serial.println(".... connected to server");
38+
if(_debug) { Serial.println(".... connected to server"); }
3939
String a="";
4040
char c;
4141
int ch_count=0;
42-
client->println("GET "+command+"&key="+_apiKey);
42+
client->println("GET " + command + "&key=" + _apiKey + " HTTP/1.1");
43+
client->print("HOST: ");
44+
client->println(YTAPI_HOST);
45+
client->println();
4346
now=millis();
4447
avail=false;
4548
while (millis() - now < YTAPI_TIMEOUT) {
@@ -72,20 +75,22 @@ String YoutubeApi::sendGetToYoutube(String command) {
7275
}
7376
}
7477
if (avail) {
75-
//Serial.println("Body:");
76-
//Serial.println(body);
77-
//Serial.println("END");
78+
if(_debug) {
79+
Serial.println("Body:");
80+
Serial.println(body);
81+
Serial.println("END");
82+
}
7883
break;
7984
}
8085
}
8186
}
82-
87+
closeClient();
8388
return body;
8489
}
8590

8691
bool YoutubeApi::getChannelStatistics(String channelId){
87-
String command="https://" YTAPI_HOST "/youtube/v3/channels?part=statistics&id="+channelId; //If you can't find it(for example if you have a custom url) look here: https://www.youtube.com/account_advanced
88-
//Serial.println(command);
92+
String command="/youtube/v3/channels?part=statistics&id="+channelId; //If you can't find it(for example if you have a custom url) look here: https://www.youtube.com/account_advanced
93+
if(_debug) { Serial.println(F("Closing client")); }
8994
String response = sendGetToYoutube(command); //recieve reply from youtube
9095
DynamicJsonBuffer jsonBuffer;
9196
JsonObject& root = jsonBuffer.parseObject(response);
@@ -109,3 +114,10 @@ bool YoutubeApi::getChannelStatistics(String channelId){
109114

110115
return false;
111116
}
117+
118+
void YoutubeApi::closeClient() {
119+
if(client->connected()) {
120+
if(_debug) { Serial.println(F("Closing client")); }
121+
client->stop();
122+
}
123+
}

src/YoutubeApi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ class YoutubeApi
4646
String sendGetToYoutube(String command);
4747
bool getChannelStatistics(String channelId);
4848
channelStatistics channelStats;
49+
bool _debug = false;
4950

5051
private:
5152
String _apiKey;
5253
Client *client;
5354
const int maxMessageLength = 1000;
5455
bool checkForOkResponse(String response);
56+
void closeClient();
5557
};
5658

5759
#endif

0 commit comments

Comments
 (0)