LED Panel – Youtube Counter + Google Analytics

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.

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

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.

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
//-------------------------------------

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.

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 install 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 '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 = $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.

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

The final result is not to 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.