-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266_YouTube.ino
147 lines (106 loc) · 3.3 KB
/
ESP8266_YouTube.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
136
137
138
139
140
141
142
143
144
145
146
/**
* Display number of youtube subscribers on a 4 digit LED Segment Display using TM1637 on NodeMCU Dev Board (ESP8266)
*
* 09/2017
*/
#include <TM1637Display.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include "credentials.h"
// ---- pins
const int CLK = D6;
const int DIO = D5;
// ---- wifi
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
// ---- request infos from google api v3
const char* host = "www.googleapis.com";
const int httpPort = 443; // when using https we use WiFiClientSecure instead of WiFiClient
const char* url = "/youtube/v3/channels?part=statistics&id=" YOUTUBE_CHANNEL_ID "&key=" GOOGLE_API_KEY;
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
/**
*
*/
void setup()
{
display.setBrightness(0x0a); //set the diplay to maximum brightness
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/**
---------------------------------------------------------------
*/
void loop()
{
WiFiClientSecure client; // WiFiClient client;
Serial.print("connecting to ");
Serial.println(host);
// ---- make request
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// ---- Read response (skip headers)
bool bHeaderReceived = false;
String jsonResponse = "";
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r" && !bHeaderReceived) {
Serial.println("headers received");
bHeaderReceived = true;
} else if (bHeaderReceived) {
jsonResponse += line;
}
}
// ---- parse json
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(jsonResponse);
if (!root.success()) {
Serial.println(jsonResponse);
Serial.println("Parsing JSON failed");
display.showNumberDec(0);
delay(30 * 1000);
return;
} else {
Serial.println( "Parsed JSON successfully");
}
JsonObject& statistics = root["items"][0]["statistics"];
// const char* statistics_viewCount = statistics["viewCount"];
const char* statistics_subscriberCount = statistics["subscriberCount"];
display.showNumberDec(atoi(statistics_subscriberCount));
Serial.println();
Serial.println("closing connection");
delay(60 * 10 * 1000); // sleep 10 minutes
}