Summer Camp: Internet of Things.

 

Youtube Subscriber Counter Guide

In this tutorial, you will be taught how to set up the code and hardware to make a live subscriber counter that displays the number of youtube subscribers you have in the form of text. The counter updates every minute.

  1. Open Arduino IDE and paste the provided code into it. Then select WeMos D1 R2 and Mini from Tools, Boards.
  2. #include <Wire.h>
    #include <ESP8266WiFi.h>
    #include <ArduinoJson.h>
    
    const char* ssid     = "bingo";       // SSID of local network
    const char* password = "D7D9C4DA07";   // Password on network
    String apiKey = "AIzaSyDQw3c9m0I7LoT5R4HuwsRPO_slP7l4FeQ";       //API KEY
    String channelId = "UCNbHnuQMqV90ThphAEaWi6A";   // YouTube channel id
    
    const char *host = "www.googleapis.com";
    long subscribers,subscribersBefore = 0;
    
    void setup() {
    Serial.begin(9600);
    Serial.println("Connecting");
      WiFi.begin(ssid, password);
       while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println(".");
      }
    }
    
    void loop() {
       int length;
      String subscribersString = String(getSubscribers());
      if(subscribers != subscribersBefore)
      {
        
        length = subscribersString.length();
        Serial.println(subscribersString);
        subscribersBefore = subscribers;
      }  
      delay(60000);
    
    }
    
    int getSubscribers()
    {
      WiFiClientSecure client;
      Serial.print("connecting to "); 
      Serial.println(host);
      if (!client.connect(host, 443)) {
        Serial.println("connection failed");
        return -1;
      }
      String cmd = String("GET /youtube/v3/channels?part=statistics&id=") + channelId + "&key=" + apiKey+ " HTTP/1.1\r\n" +
                    "Host: " + host + "\r\nUser-Agent: ESP8266/1.1\r\nConnection: close\r\n\r\n";
      client.print(cmd);
    
      int repeatCounter = 10;
      while (!client.available() && repeatCounter--) {
        delay(500);
      }
      String line,buf="";
      int startJson=0;
      
      while (client.connected() && client.available()) {
        line = client.readStringUntil('\n');
        if(line[0]=='{') startJson=1;
        if(startJson) 
        {
          for(int i=0;i<line.length();i++)
            if(line[i]=='[' || line[i]==']') line[i]=' ';
          buf+=line+"\n";
        }
      }
      client.stop();
    
      DynamicJsonBuffer jsonBuf;
      JsonObject &root = jsonBuf.parseObject(buf);
      if (!root.success()) {
        Serial.println("parseObject() failed");
        delay(10);
        return -1;
      }
      
      subscribers = root["items"]["statistics"]["subscriberCount"];
      Serial.println(subscribers);
      return subscribers;
    }
    
  3. Connect the WeMos Board to your computer with a usb cable.
  4. In the Arduino IDE go to tools, port and select the port that shows up.
  5. In the code, where it says const char ssid, type in your network name. Where it says const char password, type in your network password.
  6. Go to https://console.developers.google.com. Log in with your google account and click create a new project. In the new project, enable youtube API. Then click on the credentials tab and create a new API key. Paste the API key where it says String apikey in the code. Then paste your youtube channel ID where it says string channelId.
  7. Upload the code to the wemos and open serial monitor. You should see the number of subscribers to your channel pop up every minute.

Now you should have a Wemos board running the code you uploaded, and a serial monitor on the ARduino IDE which displays the number of subscribers you have. The WeMos is connected to google and that is how it get the number of subscribers you have.

Reference links: More Detailed Guide

« Previous