-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHYS_YTSubsCounter.ino
135 lines (109 loc) · 2.82 KB
/
SHYS_YTSubsCounter.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <ArduinoJson.h> // !!! Version 5.13.4 !!!
#include "DigitLedDisplay.h"
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Wire.h>
#include <YoutubeApi.h>
// --------------------------------
// CONFIGURATION START
// --------------------------------
#define DISPLAY_DIN_PIN 16 // ESP PIN D0
#define DISPLAY_CS_PIN 4 // ESP PIN D1
#define DISPLAY_CLK_PIN 5 // ESP PIN D2
// WiFi SSID
char ssid[] = "YOUR_WIFI_SSID";
// WiFi Password
char password[] = "YOUR_WIFI_PASSWORD";
// Google-API key
#define API_KEY "YOUR_API_KEY"
// YouTube Channel ID
#define CHANNEL_ID "YOUR_YT_CHANNEL_ID"
// Delay between Counter-Update-Requests
unsigned long requestInterval = 60000;
// --------------------------------
// CONFIGURATION END
// --------------------------------
static unsigned long lastRequest;
int subsCount;
int viewCount;
int commentCount;
int videoCount;
WiFiClientSecure client;
YoutubeApi api(API_KEY, client);
DigitLedDisplay ld = DigitLedDisplay(DISPLAY_DIN_PIN, DISPLAY_CLK_PIN, DISPLAY_CS_PIN);
void setup() {
ld.setBright(10);
ld.setDigitLimit(8);
Serial.begin(115200);
reconnectWiFi();
}
void loop() {
if ( millis() - lastRequest > requestInterval ) {
// refresh Values from YouTube API
refreshSubscriber();
// Show new Subscriber Count on Display
ld.clear();
ld.printDigit(subsCount);
delay(100);
// remember last refresh time
lastRequest = millis();
}
}
/**
* reconnect to WiFi
*/
void reconnectWiFi(){
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
boolean zeroVisible = false;
while (WiFi.status() != WL_CONNECTED) {
ld.clear();
if(zeroVisible){
ld.printDigit(0);
}
zeroVisible = !zeroVisible;
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
ld.clear();
ld.printDigit(ip[0]);
delay(1000);
ld.clear();
ld.printDigit(ip[1]);
delay(1000);
ld.clear();
ld.printDigit(ip[2]);
delay(1000);
ld.clear();
ld.printDigit(ip[3]);
delay(1000);
ld.clear();
}
/**
* get current values from YouTube API into local variables
*/
void refreshSubscriber(){
if(api.getChannelStatistics(CHANNEL_ID)) {
commentCount = api.channelStats.commentCount;
subsCount = api.channelStats.subscriberCount;
videoCount = api.channelStats.videoCount;
viewCount = api.channelStats.viewCount;
Serial.print("Comments: ");
Serial.println(commentCount);
Serial.print("Subscriber: ");
Serial.println(subsCount);
Serial.print("Videos: ");
Serial.println(videoCount);
Serial.print("Views: ");
Serial.println(viewCount);
}
}