-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoutubeCounter.ino
More file actions
590 lines (514 loc) · 16.2 KB
/
YoutubeCounter.ino
File metadata and controls
590 lines (514 loc) · 16.2 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include <Adafruit_NeoPixel.h> // Install with the library manager in the Arduino IDE - Tested with version 1.12.3
#include <HTTPClient.h> // Included with Espressif ESP32 Core - Tested with version 3.0.7
#include <ArduinoJson.h> // Install with the library manager in the Arduino IDE - Tested with version 7.2.1
#include <Preferences.h> // Included with Espressif ESP32 Core - Tested with version 3.0.7
#include <SimpleRotary.h> // Install with the library manager in the Arduino IDE - Tested with version 1.1.3
#include "counterWeb.h"
#include <TFT_eSPI.h> // Install with the library manager in the Arduino IDE - Tested with version 2.5.43 Driver to use : ILI9488
#include "mp3tf16p.h"
#include "scheduler.h"
#include "interface.h"
#include "darth_vader_helmet.h"
#include "font/Aurebesh_Bold80pt7b.h"
#include "font/Aurebesh_Bold30pt7b.h"
#include "font/hansolov355pt7b.h"
// Neopixels
#define PIXELSPIN 12
#define PIXELSCOUNT 8
uint16_t roundPixels[] = {1, 2, 3, 4, 5, 6};
const int roundPixelsCount = 6;
const int looseSubscriberPixel = 7;
const int gainSubscriberPixel = 0;
Adafruit_NeoPixel pixels(PIXELSCOUNT, PIXELSPIN, NEO_GRB + NEO_KHZ800);
// Display
TFT_eSPI tft = TFT_eSPI();
// MP3 Player
#define RXPIN 16
#define TXPIN 17
MP3Player mp3(RXPIN, TXPIN);
int currentVolume = 15;
int soundGainingSubscriber[] = {2, 4, 6, 11}; //Choosen randomly for more fun!
int darthVaderBreathingSound = 12;
int soundGainingSubscriberCount = 4;
int soundLoosingSubscriber = 3;
int soundTwoPlusSubscriber = 9;
int volumeChangeFeedback = 5;
// Rotary Encoder
#define ROTARY_PIN_A 27
#define ROTARY_PIN_B 26
#define ROTARY_PUSH_BUTTON 14
SimpleRotary rotary(ROTARY_PIN_A, ROTARY_PIN_B, ROTARY_PUSH_BUTTON);
// Fonts
const GFXfont *aurebeshCounter = &hansolov355pt7b;
const GFXfont *aurebeshText = &Aurebesh_Bold30pt7b;
// Colors
const uint32_t counterColor = TFT_WHITE;
// Youtube Statistics
int currentViewCount = -1;
int currentSubscriberCount = -1;
enum Subscriber_Status
{
NOCHANGE,
LOOSING,
GAINING,
};
Subscriber_Status currentSubscriberStatus = NOCHANGE;
// Interface activated by the Rotary Encoder
InterfaceMode interface;
// Task Scheduler called in the Loop function
TaskScheduler scheduler;
// Preferences to store values into non-volatile memory
Preferences prefs;
const char *volumePreference = "volume";
const char *channelIdPreference = "channel_id";
const char *apiKeyPreference = "api_key";
void setup()
{
Serial.begin(115200);
initPixels();
initDisplay();
mp3.initialize();
prefs.begin("youtube");
currentVolume = prefs.getInt(volumePreference, 15);
drawCenteredHorizontalText("Connect", 80, aurebeshText, TFT_DARKGREY);
drawCenteredHorizontalText("Wifi", 160, aurebeshText, TFT_DARKGREY);
initWebServer();
getYoutubeStatistics(currentSubscriberCount);
playDarthVadedBreathing();
// Scheduled tasks in milliseconds
scheduler.addTask(showRandomRoundPixels, 10000L);
scheduler.addTask(fetchSubscriberCount, 300000L);
scheduler.addTask(playDarthVadedBreathing, 3600000L);
scheduler.addTask(showFastRandomPixels, 950000L);
// Browser request will be handle by a separate task
xTaskCreatePinnedToCore(
handleBrowserCalls, /* Function to implement the task */
"handleBrowserCalls", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
NULL, /* Task handle. */
1); /* Core where the task should run */
}
void loop()
{
scheduler.runTasks();
readInterfaceThroughRotaryEncoder();
if (rotary.push() == 1)
{
// User has acknowledge the change in subscriber count
currentSubscriberStatus = NOCHANGE;
interface.setModeNormal();
showCurrentSubscriberStatus();
showSubscriberCount();
}
if (interface.checkReset())
{
showSubscriberCount();
}
}
// Generates a true random value between minVal (inclusive) and maxVal (exclusive) using ESP32's hardware RNG.
uint32_t espRandomInRange(uint32_t minVal, uint32_t maxVal) {
return (esp_random() % (maxVal - minVal)) + minVal;
}
// This runs on core 0 for responsiveness
void handleBrowserCalls(void * parameter)
{
for(;;)
{
server.handleClient();
delay(1); // allow other tasks to run
}
}
// Interface functions
//
void incrementVolume()
{
currentVolume++;
if (currentVolume > 30)
{
currentVolume = 0;
}
prefs.putInt(volumePreference, currentVolume);
}
void decrementVolume()
{
currentVolume--;
if (currentVolume < 0)
{
currentVolume = 30;
}
prefs.putInt(volumePreference, currentVolume);
}
// Show the interface when the Rotary encoder is used
void readInterfaceThroughRotaryEncoder(void)
{
int rotary_Value = rotary.rotate();
if (rotary_Value != 0)
{
switch (rotary_Value)
{
case 1:
interface.nextMode();
break;
case 2:
interface.prevMode();
break;
}
switch (interface.getMode())
{
case NORMAL:
showSubscriberCount();
break;
case VIEWCOUNT:
showViewCount();
break;
case VOLUME:
clearScreen();
drawTactical();
drawCenteredHorizontalText("Volume", 80, aurebeshText, TFT_DARKGREY);
drawCenteredHorizontalText(String(currentVolume), 160, aurebeshText, counterColor);
while (!interface.checkReset() && rotary.push() == 0)
{
rotary_Value = rotary.rotate();
switch (rotary_Value)
{
case 1:
drawCenteredHorizontalText(String(currentVolume), 160, aurebeshText, TFT_BLACK);
incrementVolume();
drawCenteredHorizontalText(String(currentVolume), 160, aurebeshText, counterColor);
mp3.playTrackNumber(volumeChangeFeedback, currentVolume, false);
interface.resetTime();
break;
case 2:
drawCenteredHorizontalText(String(currentVolume), 160, aurebeshText, TFT_BLACK);
decrementVolume();
drawCenteredHorizontalText(String(currentVolume), 160, aurebeshText, counterColor);
mp3.playTrackNumber(volumeChangeFeedback, currentVolume, false);
interface.resetTime();
break;
}
}
interface.setModeNormal();
showSubscriberCount();
break;
}
}
}
// Display Functions
//
void initDisplay(void)
{
tft.begin();
tft.setRotation(3);
clearScreen();
}
void drawTactical()
{
int lineSpacing = tft.width() / 7; // Divide by 7 to get 6 spaces
for (int i = 1; i <= 6; i++)
{
tft.drawLine(i * lineSpacing, 0, i * lineSpacing, tft.height(), TFT_DARKGREEN);
}
for (int i = 1; i <= 4; i++)
{
tft.drawCircle(50, tft.height() / 2, i * 50, TFT_DARKGREEN); // Radius is i * 75 to get a maximum radius of 300
}
tft.drawEllipse(tft.width() / 4, tft.height() / 2, tft.width(), tft.height() / 2, TFT_DARKGREEN);
tft.drawEllipse(tft.width() / 4, tft.height() / 2, tft.width() / 2, tft.height() / 4, TFT_DARKGREEN);
tft.drawEllipse(tft.width() / 4, tft.height() / 2, tft.width() / 3, tft.height() / 8, TFT_DARKGREEN);
}
void showViewCount(void)
{
clearScreen();
drawTactical();
drawCenteredHorizontalText("ViewCount", 80, aurebeshText, TFT_DARKGREY);
drawCenteredHorizontalText(String(currentViewCount), 160, aurebeshText, counterColor);
}
void drawHTTPIndicator(uint32_t color)
{
int lineSpacing = tft.width() / 7; // Divide by 7 to get 6 spaces
for (int i = 1; i <= 6; i++)
{
tft.fillRect((i - 1) * lineSpacing + 10, tft.height() - 10, espRandomInRange(15, 30), 10, color);
}
}
void drawCenteredScreenText(const String &text, const GFXfont *f, uint32_t color)
{
tft.setFreeFont(f);
Serial.println(text);
int displayWidth = tft.width();
int displayHeight = tft.height();
int textWidth = tft.textWidth(text);
int textHeight = 160; // tft.fontHeight();
int cursorX = (displayWidth - textWidth) / 2;
int cursorY = (displayHeight - textHeight) / 2;
tft.setTextColor(color);
tft.drawString(text, cursorX, cursorY);
}
void drawCenteredHorizontalText(const String &text, int line, const GFXfont *f, uint32_t color)
{
tft.setFreeFont(f);
Serial.println(text);
int displayWidth = tft.width();
int textWidth = tft.textWidth(text);
int cursorX = (displayWidth - textWidth) / 2;
tft.setTextColor(color);
tft.drawString(text, cursorX, line);
}
// Neopixels functions
//
void initPixels(void)
{
pixels.begin();
pixels.clear();
pixels.show();
}
void clearScreen(void)
{
tft.fillScreen(TFT_BLACK);
}
void showSubscriberCount(void)
{
clearScreen();
drawTactical();
String displayCount;
// Format subscriber count
if (currentSubscriberCount >= 10000) {
float formattedCount = currentSubscriberCount / 1000.0; // Convert to thousands
displayCount = String(formattedCount, 1) + "K"; // 1 decimal place + "K"
} else {
displayCount = String(currentSubscriberCount); // Use the plain number for smaller counts
}
drawCenteredScreenText(displayCount, aurebeshCounter, counterColor);
}
void showRainbowPixels(void)
{
for (int i = 1; i < 50; i++)
{
pixels.rainbow(0, i);
pixels.show();
delay(100);
}
pixels.clear();
pixels.show();
}
void showLoosingSubscriberPixels(void)
{
for (int i = 0; i < pixels.numPixels(); i++)
{
pixels.setPixelColor(i, pixels.Color(255, 165, 0));
}
pixels.show();
unsigned long c = millis();
while (millis() - c < 4000)
{
delay(50);
pixels.setBrightness(espRandomInRange(0,255));
pixels.show();
}
pixels.clear();
pixels.show();
}
void showFastRandomPixels(void)
{
unsigned long c = millis();
while (millis() - c < 10000)
{
showRandomRoundPixels();
delay(200);
}
}
void showRandomRoundPixels(void)
{
// Determine the number of pixels to change (up to 3)
int numPixelsToChange = min(3, roundPixelsCount);
// Clear all pixels in roundPixels
for (int i = 0; i < roundPixelsCount; i++)
{
pixels.setPixelColor(roundPixels[i], pixels.Color(0, 0, 0));
}
// Show the cleared pixels
pixels.show();
// Change color of up to 3 random pixels
for (int i = 0; i < numPixelsToChange; i++)
{
// Get a random index within the roundPixels array
int index = espRandomInRange(0,roundPixelsCount);
// Set the color of the pixel at the random index
pixels.setPixelColor(roundPixels[index], pixels.Color(0, 0, 32));
}
// Show the newly colored pixels
pixels.show();
showCurrentSubscriberStatus();
}
void setColorAllRoundPixels(uint32_t color)
{
for (int i = 0; i < roundPixelsCount; i++)
{
pixels.setPixelColor(roundPixels[i], color);
}
}
void showCurrentSubscriberStatus(void)
{
pixels.setPixelColor(gainSubscriberPixel, pixels.Color(0, 0, 0));
pixels.setPixelColor(looseSubscriberPixel, pixels.Color(0, 0, 0));
switch (currentSubscriberStatus)
{
case GAINING:
pixels.setPixelColor(gainSubscriberPixel, pixels.Color(0, 255, 0));
break;
case LOOSING:
pixels.setPixelColor(looseSubscriberPixel, pixels.Color(255, 0, 0));
/* code */
break;
}
pixels.show();
}
// MP3 Functions
//
void playDarthVadedBreathing(void)
{
uint32_t pause;
clearScreen();
tft.drawBitmap((tft.width() - 344) / 2, 0, darth_vader_helmet, 344, 320, TFT_RED);
mp3.playTrackNumber(darthVaderBreathingSound, currentVolume, false);
delay(500);
for (int i = 0; i < 2; i++)
{
// Inspiration
pause = 1096 / 255;
for (int i = 0; i <= 255; i++)
{
pixels.setBrightness(i);
setColorAllRoundPixels(pixels.Color(255, 0, 0));
pixels.setPixelColor(looseSubscriberPixel, pixels.Color(255, 0, 0));
pixels.setPixelColor(gainSubscriberPixel, pixels.Color(255, 0, 0));
pixels.show();
delay(pause);
}
// Expiration
pause = 1550 / 255;
for (int i = 255; i >= 0; i--)
{
pixels.setBrightness(i);
setColorAllRoundPixels(pixels.Color(255, 0, 0));
pixels.setPixelColor(looseSubscriberPixel, pixels.Color(255, 0, 0));
pixels.setPixelColor(gainSubscriberPixel, pixels.Color(255, 0, 0));
pixels.show();
delay(pause);
}
}
pixels.setBrightness(255);
pixels.clear();
pixels.show();
clearScreen();
showSubscriberCount();
}
// Youtube Statistics functions
//
void fetchSubscriberCount()
{
int subscriberCount;
if (getYoutubeStatistics(subscriberCount))
{
applyNewSubscriberCount(subscriberCount);
}
else
{
Serial.println("Failed to get subscriber count.");
}
}
void applyNewSubscriberCount(int newSubscriberCount)
{
int sound;
if (newSubscriberCount != currentSubscriberCount)
{
if (newSubscriberCount > currentSubscriberCount)
{
if (newSubscriberCount - currentSubscriberCount > 1 && currentSubscriberCount < 1000)
{
sound = soundTwoPlusSubscriber;
}
else
{
sound = soundGainingSubscriber[espRandomInRange(0,soundGainingSubscriberCount)];
}
currentSubscriberStatus = GAINING;
currentSubscriberCount = newSubscriberCount;
}
else
{
sound = soundLoosingSubscriber;
currentSubscriberStatus = LOOSING;
currentSubscriberCount = newSubscriberCount;
}
mp3.playTrackNumber(sound, currentVolume, false);
if (currentSubscriberStatus == GAINING)
{
showRainbowPixels();
}
else
{
showLoosingSubscriberPixels();
}
showSubscriberCount();
showCurrentSubscriberStatus();
}
}
// Google Youtube API calls
bool getYoutubeStatistics(int &subscriberCount)
{
HTTPClient http;
String url = "https://youtube.googleapis.com/youtube/v3/channels?part=statistics";
url += "&id=";
url += prefs.getString(channelIdPreference);
url += "&fields=items/statistics/subscriberCount,items/statistics/viewCount";
url += "&key=";
url += prefs.getString(apiKeyPreference);
http.begin(url);
int httpResponseCode = http.GET();
// If the request was successful, httpResponseCode will be a number > 0
if (httpResponseCode > 0)
{
String response = http.getString();
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, response);
// Check for errors in deserialization
if (error)
{
Serial.println(F("deserializeJson() failed: "));
Serial.println(error.c_str());
http.end();
drawHTTPIndicator(TFT_RED);
return false;
}
JsonObject root = doc.as<JsonObject>();
// Check if the JSON response contains the expected keys
if (root.containsKey("items") && root["items"].as<JsonArray>().size() > 0 &&
root["items"][0].as<JsonObject>().containsKey("statistics") &&
root["items"][0]["statistics"].as<JsonObject>().containsKey("subscriberCount"))
{
subscriberCount = root["items"][0]["statistics"]["subscriberCount"].as<int>();
currentViewCount = root["items"][0]["statistics"]["viewCount"].as<int>();
http.end();
drawHTTPIndicator(TFT_GREEN);
return true;
}
else
{
Serial.println("JSON response did not contain expected keys.");
http.end();
drawHTTPIndicator(TFT_RED);
return false;
}
}
else
{
Serial.print("Error on sending request: ");
Serial.println(httpResponseCode);
http.end();
drawHTTPIndicator(TFT_RED);
return false;
}
}