Tarantula3DIYables
Published © GPL3+

DIY - YouTube Desktop Notifier

I have made this "YouTube Desktop Notifier" to keep me up-to-date with my channel subscriber and view counts.

BeginnerProtip2 hours723
DIY - YouTube Desktop Notifier

Things used in this project

Hardware components

Perfboard
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
220v AC to 5v DC Buck Step-Down Module
×1
Buzzer
DIYables Buzzer
×1
Resistor
DIYables Resistor
×1
SPDT switch
×1
4-digit 7-segment LED Display TM1637
DIYables 4-digit 7-segment LED Display TM1637
×4
LED
DIYables LED
×2
Jumper Wires
DIYables Jumper Wires
×1
A USB cable to upload the code
×1
Starter Kit
DIYables Starter Kit
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Pencil
Measuring Tape
Chop Saw
Hammer
Nails
Sanding Tool
Personal Protective Equipments (PPE) for woodcutting

Story

Read more

Schematics

Sketch

Code

Code

Arduino
/***********************************************************
 *  Read YouTube Channel statistics from the YouTube API   *
 *  By Brian Lough, Modified By Ashish Adhikari            *
 *  https://www.youtube.com/user/tarantula3                *
 ***********************************************************/

#include <YoutubeApi.h>       // A wrapper for the YouTube API for Arduino (works on ESP8266)
#include <ESP8266WiFi.h>      // The Wi-Fi library for ESP8266
#include <WiFiClientSecure.h> // Variant of WiFiClient with TLS support
#include <TM1637Display.h>    // This Library is to drive the TM1637 4 digits display
#include <ArduinoJson.h>      // This Sketch doesn't technically need this, but the library does so it must be installed.

// ------- Replace the following! ------ //
char ssid[]     = "XXXXXX";                                // your network SSID (name)
char password[] = "XXXXXXXX";                              // your network key
#define API_KEY "AIzaSyBNabcXXXXXXXXXXXXXXXRpxO3ySg_mNbY"  // your google apps API Token
#define CHANNEL_ID "UCR0wao7J3htf9DjuVJ2uzUw"              // makes up the url of channel
// ------------------------------------- //

WiFiClientSecure client;
YoutubeApi api(API_KEY, client);

unsigned long api_mtbs = 120000; // Mean time between api requests - Change this to 5 minutes 
unsigned long counter = 0;       // Counter that increments every 10 milliseconds

// Module connection pins (Digital Pins)
#define CLK1 D3
#define DIO1 D2
#define CLK2 D1
#define DIO2 D0
#define CLK3 D5
#define DIO3 D4
#define CLK4 D6
#define DIO4 D7
TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);
TM1637Display display3(CLK3, DIO3);
TM1637Display display4(CLK4, DIO4);
long viewsLeft  = 0;
long viewsRight = 0;
long subLeft    = 0;
long subRight   = 0;
long OLDsub     = 0;

// Setting up the buzzer
int frequency = 1000; // Specified in Hz
int buzzPin   = D8;
int timeOn    = 500; // Specified in milliseconds
int timeOff   = 500; // Specified in milliseconds

// Setting up the LEDs
int BlueLED  = D9;
int WhiteLED = D10;
int LEDblinker = 0;

void setup() {

  Serial.begin(115200);

  // Set the LEDs
  //pinMode(D9, OUTPUT);
  //pinMode(D10, OUTPUT);
  //digitalWrite(D9, HIGH);

  // Clear the display
  display1.setBrightness(0x0f);
  display1.showNumberDec(0, false, 4, 0);
  display2.setBrightness(0x0f);
  display2.clear();
  display3.setBrightness(0x0f);
  display3.showNumberDec(0, false, 4, 0);
  display4.setBrightness(0x0f);
  display4.clear();

  // Set WiFi to station mode and disconnect from an AP if it was Previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);
}

void loop() {

  counter = counter + 10;
  if (counter > api_mtbs)  {      // Should a request be made?
    if(api.getChannelStatistics(CHANNEL_ID))
    {
      Serial.println("---------Stats---------");
      Serial.print("Subscriber Count: ");
      Serial.println(api.channelStats.subscriberCount);
      Serial.print("View Count: ");
      Serial.println(api.channelStats.viewCount);
      Serial.print("Video Count: ");
      Serial.println(api.channelStats.videoCount);
      Serial.println("------------------------");

      // Display the view count
      viewsRight = api.channelStats.viewCount % 10000;                 // Value on the right
      viewsLeft  = (api.channelStats.viewCount - viewsRight) / 10000;  // Value on the left     
      if(viewsLeft > 0) {       // Zero-padding to avoid a gap between the Left and Right displays
        display1.showNumberDec(viewsRight, true, 4, 0);
        display2.showNumberDec(viewsLeft, false, 4, 0);
      } else {
        display1.showNumberDec(viewsRight, false, 4, 0);
        display2.clear();
      }

      // Display the subscribers count
      subRight = api.channelStats.subscriberCount % 10000;               // Value on the right
      subLeft  = (api.channelStats.subscriberCount - subRight) / 10000;  // Value on the left
      if(subLeft > 0) {      // Zero-padding to avoid a gap between the Left and Right displays
        display3.showNumberDec(subRight, true, 4, 0);
        display4.showNumberDec(subLeft, false, 4, 0);
      } else {
        display3.showNumberDec(subRight, false, 4, 0);
        display4.clear();
      }

      // Ring the buzzer if a new subscriber is gained
      if ( api.channelStats.subscriberCount > OLDsub ) { // New subscriber Gained
        tone(buzzPin, frequency);
        delay(timeOn);
        noTone(buzzPin);
        delay(timeOff);
        OLDsub = api.channelStats.subscriberCount;
        LEDblinker = 1;
      }

    }
    counter = 0; // Reset counter after displaying the data
  }

  // Blink the LEDs
  if ( LEDblinker == 1 ) {
    if( counter == 10 || counter == 400 || counter == 800 || counter == 1200 || counter == 1600 || counter == 2000 ){
      digitalWrite(BlueLED, LOW); // Turn off the Blue LED
      digitalWrite(WhiteLED, HIGH); // Turn on the White LED
    } else if ( counter == 200 || counter == 600 || counter == 1000 || counter == 1400 || counter == 1800 ){
      digitalWrite(BlueLED, HIGH); // Turn on the Blue LED
      digitalWrite(WhiteLED, LOW); // Turn off the White LED      
    } else if ( counter == 2200 ) {
      LEDblinker = 0;               // Reset the blinker counter      
    }
  } else {
    digitalWrite(BlueLED, HIGH); // Turn on the Blue LED
    digitalWrite(WhiteLED, LOW); // Turn off the White LED
  }
  delay(10);
}

Credits

Tarantula3
70 projects • 87 followers
There were 1000+ sperms but I was the fastest one..
DIYables
0 projects • 88 followers
I would like to invite you to join and add your projects to DIYables platform https://www.hackster.io/diyables

Comments