$10 ESP8266 Live Youtube Subscriber Display With OTA Support

October 30th, 2016





After watching Andreas Spiess’ video in which he demoed his live sub counter, I set off to build one of my own. For this I used:

2x MAX7219 based 8×8 red LED matrices

1x Wemos d1 mini(ESP8266 based microcontroller)

5x male to female jumper cables

The first thing I began with was getting an API key from https://console.developers.google.com/

Once you do that, you are ready to begin using the Arduino Youtube API Library.

Download it and install in Arduino by going to Sketch->Include Library->Add a .ZIP library, and open the file you downloaded from the above link.

You will also need to download the ArduinoJSON Library, and the MAX7219LedMatrix Library.

Install it by searching for it in the library manager or by downloading it from the link above.

Add a .ZIP library to arduino

Now that you have installed your library, run the example sketch located at

Examples->YoutubeApi->ESP8266->ChannelStatstics

Channel Statistics Example Sketch

Run this sketch, replacing the token with your token, and the wifi details with yours.

If it works, you will get a serial output similar to this:
687474703a2f2f692e696d6775722e636f6d2f466d58795734452e706e67

If you’re able to successfully get that output, you’re ready to go on.

Now load the following sketch over USB:

Download or

Copy and paste:

//By Joey Babcock
//Find diagram and more here: https://www.joeybabcock.me/blog/projects/arduino-esp8266-live-subscriber-display/

#include <YoutubeApi.h>
#include <SPI.h>
#include "LedMatrix.h"
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#define NUMBER_OF_DEVICES 2
#define CS_PIN D4
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);

char ssid[] = "SSID_HERE"; // your network SSID (name)
char password[] = "PASSWORD"; // your network key
#define API_KEY "find on https://console.developers.google.com/" // your google apps API Token
#define CHANNEL_ID "UC7SHEJBjzSz7Xm9M4d93mOQ" // makes up the url of channel(Find yours here: https://www.youtube.com/account_advanced)
WiFiClientSecure client;
YoutubeApi api(API_KEY, client);
int api_mtbs = 60000; //mean time between api requests
long api_lasttime; //last time api request has been done
long subs = 0;

void setup() {

        Serial.begin(115200);
        Serial.println();
        Serial.println();
        Serial.print("Connecting to ");
        Serial.println(ssid);

        WiFi.begin(ssid, password);

        while (WiFi.status() != WL_CONNECTED) {
                delay(500);
                Serial.print(".");
        }
        ArduinoOTA.setPassword((const char *)"123");
        ArduinoOTA.onStart([]() {
                Serial.println("Start");
        });
        ArduinoOTA.onEnd([]() {
                Serial.println("\nEnd");
        });
        ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
                Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
        });
        ArduinoOTA.onError([](ota_error_t error) {
                Serial.printf("Error[%u]: ", error);
                if (error == OTA_AUTH_ERROR)
                         Serial.println("Auth Failed");
                else if (error == OTA_BEGIN_ERROR)
                        Serial.println("Begin Failed");
                else if (error == OTA_CONNECT_ERROR)
                        Serial.println("Connect Failed");
                else if (error == OTA_RECEIVE_ERROR)
                        Serial.println("Receive Failed");
                else if (error == OTA_END_ERROR)
                         Serial.println("End Failed");
        });
        ArduinoOTA.begin();
        Serial.println("");
        Serial.println("WiFi connected");
        Serial.println("IP address: ");
        IPAddress ip = WiFi.localIP();
        Serial.println(ip);
        ledMatrix.init();
        ledMatrix.setIntensity(10); // range is 0-15
        getStats();
}

void loop() {
        scroll();
        ArduinoOTA.handle();
        if (millis() > api_lasttime + api_mtbs) {
               if(api.getChannelStatistics(CHANNEL_ID))
                {
                        getStats();
                }
               api_lasttime = millis();
        }
}

void scroll()
{
        ledMatrix.clear();
        ledMatrix.scrollTextLeft();
        ledMatrix.drawText();
        ledMatrix.commit();
        delay(100);
}

void getStats() {
        ledMatrix.setText("Subs: "+String(api.channelStats.subscriberCount));
}

On the last part, you could also do:

ledMatrix.setText("Subs: "+String(api.channelStats.subscriberCount) + "Views: " + String(api.channelStats.viewCount) + "Videos: " + String(api.channelStats.videoCount));

Which would also display view count and video count.

Now comes the connections. According to the Max7219Matrix library, we need to connect:

GND->GND

VCC->3v3(on wemos d1 mini)

DIN->D7

CS->D4

CLK->D5

20161108_17041020161108_17041720161108_17040320161108_170335

 


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *