LED – Youtube Counter + Google Analytics

Intro

Had a LED Matrix panel laying around for a while and finally got around to make something useful with it. Decided to build a Youtube subscriber and a Google Analytics realtime user counter.

An updated post of this project can be found here

Parts

The items i used for this project are:

  • LED P4 Matrix – link
  • ESP32 D1 Mini – link
  • ESP32 Matrix Shield – link
  • 5v 3A Power Supply – link

Total cost of components approximately 42 euro’s

ESP32 Matrix Shield

First of, we need to solder up the brilliant little board from Brian Lough. You can wire the LED Matrix up without it, but that will result in a rats nest of wires at the back of the panel. Using his board just gives a much cleaner look.

Arduino

Next step is to upload the Arduino code to the board. For this i used the following libraries:

  • PxMatrix – link
  • YoutubeApi – link
  • ArduinoJson
  • HTTPClient

PxMatrix is used for controlling the LED Matrix panel. YoutubeApi (from the maker of the ESP32 Shield) is used to pull subscriber statistics of of Youtube (depends on ArduinoJson) and HTTPClient to pull Google Analytics realtime user count of a external webserver.

For the Youtube API to work you need to create a Youtube API Key. Head over to Google API’s and create your own.

Now for the Arduino code.

First we need to set up wifi and load in the Youtube API key and Channel ID

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PxMatrix.h>
#include <YoutubeApi.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
//------- Replace this !!!!! ----------
const char* ssid = "your-ssid";       // your network SSID (name)
const char* password = "your-password";  // your network key
#define API_KEY "your-api-key"  // your google apps API Token (see readme of YT API library)
#define CHANNEL_ID "your-channel-id" // makes up the url of channel
//-------------------------------------

Images

Next we need to load in the two images (Youtube and Google Analytics logo) . I used this site to convert images to hex. Keep in mind that the resolution of your images is equal to the amount of pixels on the matrix you use. So the maximum pixels on my panel are 64 pixels wide and 32 pixels high.

For example this Youtube image is 21 pixel wide and 14 high. Two images equals 28 pixels. Two lines between the two logo’s and one line at the top and bottom of the panel. 28+2+1+1=32. Adjust your image accordingly.

This image converts to the following hex code

uint16_t static youtube_icon[]={
0x0000, 0x0000, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0010 (16) pixels
0xF800, 0xF800, 0xF800, 0x0000, 0x0000, 0x0000, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0020 (32) pixels
0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0x0000, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0030 (48) pixels
0xF800, 0xF800, 0xFFFF, 0xFFFF, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0040 (64) pixels
0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xFFFF, 0xFFFF, 0xFFFF, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0050 (80) pixels
0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,   // 0x0060 (96) pixels
0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0070 (112) pixels
0xF800, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0080 (128) pixels
0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0090 (144) pixels
0xF800, 0xF800, 0xF800, 0xF820, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,   // 0x00A0 (160) pixels
0x0000, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x00B0 (176) pixels
0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x00C0 (192) pixels
0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x00D0 (208) pixels
0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xFFFF, 0xFFFF, 0x0000, 0xF800, 0xF800, 0xF800,   // 0x00E0 (224) pixels
0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0x0000,   // 0x00F0 (240) pixels
0x0000, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0x0000, 0xF800, 0xF800, 0xF800,   // 0x0100 (256) pixels
0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0110 (272) pixels
0x0000, 0x0000, 0x0000, 0xF861, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800, 0xF800,   // 0x0120 (288) pixels
0xF800, 0xF800, 0xF800, 0xF820, 0x0000, 0x0000,
};

Do the same for any additional images you want to add to the panel. I’ve added the Google Analytics logo as well.

PHP

Since there is no Google Analytics API for Arduino that i know of, i used a PHP script to retrieve realtime user statistics. Then used HTTPClient in Arduino to trigger this script and display the number.

First you need to download Google API for PHP here and unzip it on a PHP enabled webserver obviously.

Then you need to create a JSON Service Key for Google Analytics again at Google API’s and get your View Profile ID of the website you want to monitor from Google Analytics itself.

Created this PHP script.

<?php                                                                                                                                                                                    
                                                                                                                                                                                         
require 'google-api-php-client/vendor/autoload.php';                                                                                                                                     
function initializeAnalytics(){                                                                                                                                                          
  $client = new Google_Client();                                                                                                                                                         
  $client->setApplicationName("matrix");                                                                                                                                                 
  $client->setAuthConfig(__DIR__ . '/service-key.json');                                                                                                                                 
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);                                                                                                            
  $analytics = new Google_Service_Analytics($client);                                                                                                                                    
  return $analytics;                                                                                                                                                                     
}                                                                                                                                                                                        
$analytics = initializeAnalytics();                                                                                                                                                      
$results = $analytics->data_realtime->get('YOUR_VIEW_ID', 'rt:activeUsers', ['fields' => 'rows']);                                                                                       
$activeVisitors = isset($results->rows[0][0]);                                                                                                                                           
if ($activeVisitors < "1") {                                                                                                                                                             
        echo "0";                                                                                                                                                                        
} else {                                                                                                                                                                                 
        echo $activeVisitors;                                                                                                                                                            
}                                                                                                                                                                                        
                                                                                                                                                                                         
?>

Drop the JSON Service Key in the same directory as the script itself. Also change “YOUR_VIEW_ID”. The script spits out a number which HTTPClient then retrieves.

Fusion360

Finally i needed a stand for the display and designed one in Fusion360. Download it here.

Final result

The final result is not too shabby. Unfortunately don’t have much subscribers and real time visitors yet. But hey, the matrix panel works.

Full Arduino code can be downloaded here.

PayPal

If you like my work, please consider donating