IMAG0532

With the power of the Arduino YÚN, I made an LCD sign that displays the number of subscribers of the famous YouTube user Pewdiepie’s channel! The led also glows blue if he’s put up a new video in the last hour! Awesome!

Sorry there haven’t been many posts up lately, I’ve been very busy and haven’t had any electronics time :(. But now that a four day weekend has rolled around (\o/) I can get back to what I love! Ok, to this Arduino business. It all started when I won a twitter competition put on by MCMElectronics over the Arduino Day weekend by retweeting the competition tweet and entering in a drawing for 3 Arduino UNOs and one grand prize YÚN. On the last day, I ended up winning the ~$85 WIFI connected linux board and after it arrived yesterday, I’ve been very busy with it.

Tutorial

Since this is so cool, and it’s a perfect way to explain how I did it, I’m going to do a how-to guide on how you can set this project up yourself! You can change the channel to any one you wish, and customize the message too!

With the YÚN comes an amazing partner service called Temboo. Temboo is a compiled programming environment that brings together the APIs of over one hundred different internet services in one place, and provides easy to use tools and code generators to help you reach out into the web using the YÚN to take advantage of these complicated services and make them easier for people to use. In this project, I took advantage of Google’s YouTube API which offers you many different things to do with YouTube videos, channels, statistics, playlists, trends, you name it. I selected the area closest to what I wanted to do, display Pewdie’s subscriber count, which ended up being “ListChannelsByID” which when queried, returns data from Google like this:

{
 "kind": "youtube#channelListResponse",
 "etag": "\"OilNCqKLXpFjeQ5CI-_BZqeMuCo/CGqUHGED6AbmUIcorPKAmWKo0UY\"",
 "pageInfo": {
 "totalResults": 1,
 "resultsPerPage": 1
 },
 "items": [
 {
 "kind": "youtube#channel",
 "etag": "\"OilNCqKLXpFjeQ5CI-_BZqeMuCo/UGWiEJkVlFup_RdbsaqBs09i4cQ\"",
 "id": "UC-lHJZR3Gqxm24_Vd_AJ5Yw",
 "statistics": {
 "viewCount": "4155762607",
 "commentCount": "289522",
 "subscriberCount": "26060466",
 "hiddenSubscriberCount": false,
 "videoCount": "1717"
    }
   }
  ]
 }

In essence, what my program does is it takes in this long message from Google, finds what position in the message the word “subscriberCountends and “hiddenSubscriberCount” begins, trims the message to just the number of subscribers, and then prints it on the LCD screen. The number of videos from “videoCount” is also read, and if the number changes in the span of one hour indicating a new video, the blue LED turns on! Cool!

Hardware

For this project you’ll need these supplies (beautiful pictures by Adafruit, YÚN photo by me!):

Arduino YÚNBlSw45HCUAADn8h

 

Breadboard and jumper wires

Standard LCD 16×2 + extras

 

LED (any kind really) 

 

Wiring

To wire up the circuit, attach the LCD in the standard fashion with the trimpot and the backlight connected as per this diagram:

 

Next, plug the LED’s positive leg into pin D13, and the shorter one into GND in the top left of the diagram.

Ok, with everything wired up you can plug the YÚN in and the screen should show a contrast calibration pattern to which you can dial the potentiometer to a setting you are comfortable with. If not, check those pesky connections, there are a lot of wires to get right!

Software

First thing’s first, in order to have YÚN compatibility, the Arduino IDE must be v1.5.6 or later, which can be downloaded from here.

After that, go ahead and make a Temboo account if you haven’t already, and get that all set up here. With that complete, we need authorization codes from Google in order to use the YouTube API. Just follow the setup instructions from Temboo about what codes you need from where, and how to generate them here. Follow all instructions from Google in the Developer’s Console and generate the codes needed in the setup instructions. KEEP THEM SAFE!

With those codes ready, we can start uploading some code!

Copy this program into the Arduino IDE:

/*
 John Wall
 WWW.WALLTECH.CC
 Arduino YUN YouTube Project
 4/18/14
*/
#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information, as described below
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int numRuns = 1; // Execution count, so this doesn't run forever
int maxRuns = 10; // Maximum number of times the Choreo should be executed
String message;
String subCount;
int vidCount;
int previousVidCount;
unsigned long previousMillis = 0; // will store last time the subscriber count was updated

// constants won't change :
const long interval = 1800000; // interval at which to check for new videos

void setup() {

 Bridge.begin();
 lcd.begin(16, 2);

 TembooChoreo ListChannelsByIDChoreo;

 // Invoke the Temboo client
 ListChannelsByIDChoreo.begin();

 // Set Temboo account credentials
 ListChannelsByIDChoreo.setAccountName(TEMBOO_ACCOUNT);
 ListChannelsByIDChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
 ListChannelsByIDChoreo.setAppKey(TEMBOO_APP_KEY);

 // Set saved inputs to use for execution
 //ListChannelsByIDChoreo.setSavedInputs("YoutubeCredentials");

 // Set Choreo inputs
 ListChannelsByIDChoreo.addInput("APIKey", "AIzaSyCFNxJUAel-2TWrriwsvkEQf8ygr94Zdzo");
 ListChannelsByIDChoreo.addInput("ChannelID", "UC-lHJZR3Gqxm24_Vd_AJ5Yw");
 ListChannelsByIDChoreo.addInput("Part", "statistics");

 // Identify the Choreo to run
 ListChannelsByIDChoreo.setChoreo("/Library/YouTube/Channels/ListChannelsByID");

 // Run the Choreo; when results are available, print them to serial
 ListChannelsByIDChoreo.run();

 while (ListChannelsByIDChoreo.available()) {
 char c = ListChannelsByIDChoreo.read();
 message += c;
 }

 int startVid = message.lastIndexOf("videoCount") + 14;
 int endVid = message.lastIndexOf('"');
 vidCount = message.substring(startVid, endVid).toInt();
 previousVidCount = vidCount;
 message = "";
 ListChannelsByIDChoreo.close();

}
void loop()
{
 if (numRuns <= maxRuns) {

 TembooChoreo ListChannelsByIDChoreo;

 // Invoke the Temboo client
 ListChannelsByIDChoreo.begin();

 // Set Temboo account credentials
 ListChannelsByIDChoreo.setAccountName(TEMBOO_ACCOUNT);
 ListChannelsByIDChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
 ListChannelsByIDChoreo.setAppKey(TEMBOO_APP_KEY);

 // Set saved inputs to use for execution
 //ListChannelsByIDChoreo.setSavedInputs("YoutubeCredentials");

 // Set Choreo inputs
 ListChannelsByIDChoreo.addInput("APIKey", "AIzaSyCFNxJUAel-2TWrriwsvkEQf8ygr94Zdzo");
 ListChannelsByIDChoreo.addInput("ChannelID", "UC-lHJZR3Gqxm24_Vd_AJ5Yw");
 ListChannelsByIDChoreo.addInput("Part", "statistics");

 // Identify the Choreo to run
 ListChannelsByIDChoreo.setChoreo("/Library/YouTube/Channels/ListChannelsByID");

 // Run the Choreo; when results are available, print them to serial
 ListChannelsByIDChoreo.run();

 while (ListChannelsByIDChoreo.available()) {
 char c = ListChannelsByIDChoreo.read();
 message += c;
 }

 int startSub = message.lastIndexOf("subscriberCount") + 19;
 int endSub = message.lastIndexOf("hiddenSubscriberCount") - 8;
 subCount = message.substring(startSub, endSub);
 lcd.clear();
 lcd.println("Pewdiepie's Bro ");
 lcd.setCursor(0, 1);
 lcd.print("Army: ");
 lcd.print(subCount);
 int startVid = message.lastIndexOf("videoCount") + 14;
 int endVid = message.lastIndexOf('"');
 vidCount = message.substring(startVid, endVid).toInt();
 Serial.println(vidCount);
 unsigned long currentMillis = millis();

 if (currentMillis - previousMillis >= interval) {
 // save the last time you blinked the LED
 previousMillis = currentMillis;

 previousVidCount = vidCount;
 // if the LED is off turn it on and vice-versa:
 digitalWrite(13, LOW);
 }

 if (vidCount != previousVidCount)
 {
 digitalWrite(13, HIGH);
 }

 else digitalWrite(13, LOW);

 subCount = "";
 message = "";
 ListChannelsByIDChoreo.close();

 }

 delay(3600000); // check every hour
}

In another Arduino tab that you’ll name “TembooAccount.h,” paste this code in, and fill in your Temboo account information in the right places.


/*
IMPORTANT NOTE about TembooAccount.h

TembooAccount.h contains your Temboo account information and must be included
alongside your sketch. To do so, make a new tab in Arduino, call it TembooAccount.h,
and copy this content into it. 
*/

#define TEMBOO_ACCOUNT "accountName"  // Your Temboo account name 
#define TEMBOO_APP_KEY_NAME "myFirstApp"  // Your Temboo app key name
#define TEMBOO_APP_KEY "abc123xxxxxxxxxxxxxx"  // Your Temboo app key

/* 
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.  
Keeping your account information in a separate file means you can share the 
main .ino file without worrying that you forgot to delete your credentials.
*/

With that complete, compile and check for errors. If you’re error free, upload it to the YÚN when it’s connected to the internet via ethernet or WIFI, and watch the numbers update live with Pewdie’s subscriber count! Awesome!

Customization

In order to customize this project to a different channel, simply go to this link, scroll to the bottom, under the Try it! heading, in the “id” entry, type “statistics,” and in the “forUsername” entry, type whoever you’d like to display. Hit execute, and in the response block you get back, find the channel id and exchange it with Pewdie’s in the program.

Screen Shot 2014-04-18 at 8.43.31 PM

 

To change the message, for the first line’s text, switch out “Pewdiepie’s Bro ” for something else, and likewise for “Army:”

Conclusion

If everything runs smoothly, you should now have a YouTube sign that indicates when new videos are uploaded by the channel, and displays the subscriber count in real time! I certainly had great fun building this, and as my first YÚN project, it’s opened the doors to so many awesome things from Temboo! Amazon has an API, USPS, Paypal, Instagram, and many other common services do too! The possibilities are endless!

-John

Arduino YÚN YouTube Sign! (First YÚN Project)