Making something that’s been done so many times before!

//Making something that’s been done so many times before!
TUTORIAL


YouTube Subscriber Counter

-=Using the NodeMCU=-

HELP OUT

All my content is free.

If you feel that my videos are helping and you would like to contribute, you can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

Brainy-Bits Tip Jar Amount
OVERVIEW

Oh, the infamous YouTube Counter project!

I know there’s a lot of them out there, but I always wanted to build one for myself.

And since I told myself that I would try to film more stuff and put out more tutorial videos from now on, here’s my take on it!

This will be a two part tutorial:

In this first part we will have a look at:

  • How to setup the Google API for YouTube
    *This part can be confusing if you look at other tutorials on the internet, so I will try to make it easier ūüôā
  • Then we will look at the code to get the information from it, and make sure it works correctly

In the next part we will build it and make it look nice with all the bells and whistles!

So let’s get started!

THE NODEMCU

Now since we’re getting the YouTube information from the internet we need something that has at least a WiFi connection.

That’s why I chose to use the NodeMCU board which has WiFi built in.

The NodeMCU is built around the popular ESP8266 WiFi module.
Using the NodeMCU makes it easy to connect and use the capabilities of the ESP8266 as well as use the Arduino IDE we are familiar with to upload sketches.

If you’re not familiar with the NodeMCU board you can check these tutorials I’ve done previously:

SETTING UP THE GOOGLE YOUTUBE API

Like I said at the beginning, I feel the most confusing part of this project is “how do I setup the Google YouTube API?”

All the other tutorials I’ve seen on the subject don’t seem to go through all the steps in details, so I decided to go through it step by step to help with this part.

I will go through them here, but please don’t forget to watch the tutorial video below to see them in real life.

*note: this method works at the time this tutorial was created, so if Google makes changes in the future, these steps might be different.

Step 1: Go to the Google API webpage: https://console.developers.google.com  and make sure you are logged in to your gmail account.

Click on “CREATE” on the left to create a new project.

Give your project a Name, you don’t need to select an organization.

Then click CREATE to finish.

You should get a notification that the project was created and make sure it is selected in the upper left.

Now click on the “API library” Link next to the text that says “You don’t have any API’s available to use yet…..”

Now on this page type “YouTube” in the Search box.

Click on “YouTube Data API v3” to select the API.

Click on “ENABLE” to install the API.

Now that the API is installed and Enabled, click on “CREATE CREDENTIALS”.

Now on this page you are gonna click the link “API key”, I’ve highlighted it in green in the above picture.

Here you can give the API a name if you want, but before you click on Create, click on the “API restrictions” Tab.

Here, under the “Select API” drop down, select “YouTube Data API v3”

Once it shows up above the drop down box, then you can Click “Create” to finish.

And that’s it! ¬†You now have your API Key ready to go and test in the next step.

TESTING OUT THE YOUTUBE API

Now before we actually get to coding, we can test to see if the YouTube API is working just using a web browser.

For this you will need the API KEY we created in the prior step, as well as your YouTube ID.

You can find out your YouTube ID by going to YouTube main page and then clicking on Settings under your username:

Once there, click on Advanced settings, and there you will see your Channel ID.  Make a note of it for the next step.

Ok so now that we have the API key number and the YouTube ID we can open a browser (I use Google Chrome for this) and type in this URL:

https://www.googleapis.com/youtube/v3/channels?part=statistics&id=PUT YOUR YOUTUBE ID HERE&key=PUT YOUR API KEY HERE

Now of course, replace the “PUT YOUR….” text with the appropriate ID or KEY that you got from the prior steps.

If everything is working you should see this type of information after pressing enter:


If you see this type of information, yours will be different since it’s using a different YouTube Channel… ¬†Then you’re all set to move on to programming the NodeMCU to get that information itself.

CONNECTION DIAGRAM

The connections for this first part are pretty simple:

Pin D5, D7 and D8 of the NodeMCU are connected to CLK, DIN and CS of the LED Matrix

3.3V and GND of the NodeMCU are connected to VCC and GND of the LED Matrix

THE CODE

Ok so we confirmed that the YouTube API is working so now we can start the code that will retrieve this information using the NodeMCU.

We need a couple of libraries to make this work, the main one created by Brian Lough will make it easy to get the YouTube information we need.

There’s a list of the needed libraries with links at the bottom of this page.

As always, please watch the tutorial video for more information on how this code works.

/* YouTube Counter LED Display

Modified by Yvan / https://Brainy-Bits.com from example code of Brian Lough
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include <YoutubeApi.h>  // created by Brian Lough https://github.com/witnessmenow/arduino-youtube-api
#include <ArduinoJson.h> // *Version 5.13.5 needed* - created by bblanchon https://github.com/bblanchon/ArduinoJson
#include <ESP8266WiFi.h> // Library for WiFi communication (installed by default with NodeMCU board)

#include <MD_Parola.h>   // Library to control LED Matrix  https://github.com/MajicDesigns/MD_Parola
#include <MD_MAX72xx.h>  // Library needed for MD_Parola   https://github.com/MajicDesigns/MD_MAX72xx
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::ICSTATION_HW
/*  PAROLA_HW,    ///< Use the Parola style hardware modules.
    GENERIC_HW,   ///< Use 'generic' style hardware modules commonly available.
    ICSTATION_HW, ///< Use ICStation style hardware module.
    FC16_HW       ///< Use FC-16 style hardware module.
*/


#define MAX_DEVICES 1  // Number of modules connected
#define CLK_PIN   D5   // CLK pin of matrix connected to pin D5 on NodeMCU
#define DATA_PIN  D7   // DATA pin of matrix connected to pin D7 on NodeMCU
#define CS_PIN    D8   // CS pin of matrix connected to pin D8 on NodeMCU

MD_Parola MATRIX = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);  // Create instance of MD_Parola named MATRIX


#define wifi_name "HOME5GHZ"     // your network SSID (name)
#define wifi_password "cryogenics"  // your network key
#define google_api_key "AIzaSyB9JiEN5vXYtSpsD8OGEyLm0IP0XbyCAmg"  // your google API Key
#define youtube_channel_id "UCH2m9XX8jSyVMkZUXwETPTw" // your YouTube Channel ID


WiFiClientSecure client;  // create instance of WiFi named client

YoutubeApi get_from_youtube(google_api_key, client);  // Create instance of YoutubeAPI named get_from_youtube

unsigned long refresh_time = 6000; // Time in milliseconds that needs to pass before a refresh
unsigned long last_refresh;   // Last time a refresh of the info occured

long subs = 0;  // Variable to save number of Subscribers YouTube 
long total_views = 0;  // Variable to save number of total views on YouTube

int counterdigit=0;  // Variable to change number on led matrix

void setup() {

  MATRIX.begin();  
  MATRIX.setTextAlignment(PA_CENTER);
  
  Serial.begin(115200); 
  client.setInsecure(); // Needed to get information for the YouTube API on the NodeMcu

  Serial.print("Connecting Wifi: ");
  Serial.println(wifi_name);
  
  WiFi.begin(wifi_name, wifi_password);  // connect to the WiFi network
  
  while (WiFi.status() != WL_CONNECTED) {  // wait for a connection
    Serial.print(".");
    delay(500);
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);
  
  MATRIX.print(counterdigit);
  counterdigit++;
}

void loop() {

  if (millis() - last_refresh > refresh_time)  {  // if enough time has expired between refresh
    if(get_from_youtube.getChannelStatistics(youtube_channel_id))  // if info received from YouTube
    {
      Serial.println("---------Stats---------");
      Serial.print("Subscriber Count: ");
      Serial.println(get_from_youtube.channelStats.subscriberCount);
      Serial.print("View Count: ");
      Serial.println(get_from_youtube.channelStats.viewCount);
      Serial.print("Video Count: ");
      Serial.println(get_from_youtube.channelStats.videoCount);
      Serial.println("------------------------");
      MATRIX.print(counterdigit);
      counterdigit++;
      delay(10000);
    }
    
    last_refresh = millis();  // set last time info was refreshed
  }
}
CONCLUSION

So that will do it for Part 1 of the YouTube Subscriber Counter.

We confirmed that the NodeMCU can get the information we need from the YouTube API by viewing it on the Serial Monitor.

In the next part, we will build something more interesting to display all this information…

I’m still thinking about how to do this, so I hope to see you again when I do and see how it turns out!

Thanks for stopping by!

TUTORIAL VIDEO
DOWNLOAD

Copy and Paste the above code/sketch in your Arduino IDE software.

Link to the libraries used in this tutorial:


Arduino YouTube Library by Brian Lough https://github.com/witnessmenow/arduino-youtube-api

ArduinoJson library written by Beno√ģt Blanchon
– Install this library using the Arduino IDE Library manager
– You need to choose version 5.13.8 since the new versions will not work

If you’re not sure how to install libraries this way, check out this tutorial video:
https://www.brainy-bits.com/arduino-libraries-tutorial/

By | 2019-10-04T19:12:03-05:00 October 4th, 2019|Tutorials|

Leave A Comment