Youtube View Counter

About the project

Create your own YouTube view counter.

Project info

Difficulty: Easy

Platforms: Espruino

Estimated time: 1 day

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Hardware components

MAX7219 32x8 Pixel LED display MAX7219 32x8 Pixel LED display http://www.espruino.com/MAX7219 x 1
Espruino Wifi Espruino Wifi http://www.espruino.com/WiFi x 1

Software apps and online services

Espruino Web IDE Espruino Web IDE

Story


Wiring Up

Just connect as follows:

Note: Due to the ordering of the wires on the MAX7219 board and the colouring of the supplied ribbon cable, be really careful not to accidentally connect up the voltage backwards.

The MAX7219 board should run off of 3.3v or 5v. 5v will be brighter and will put less strain on the 3.3v line (also used by WiFi).

Since we're using software SPI here, you can use any available GPIO pins for the MAX7219.

Software

Use this software on the right-hand side of the Web IDE, making sure you change the WiFi, Video and API key details at the top. See the video for how to get a working API key.

  1. var WIFI_NAME = "espruino";
  2. var WIFI_OPTIONS = { password : "testtest" };
  3. var VIDEO_ID = "aZ4MKhqvz2w";
  4. var API_KEY = "";
  5.  
  6. var disp;
  7. var g = Graphics.createArrayBuffer(32, 8, 1);
  8. g.flip = function() { disp.raw(g.buffer); };
  9.  
  10. require("Font4x8Numeric").add(Graphics);
  11. g.setFont4x8Numeric();
  12.  
  13. var wifi;
  14.  
  15. var count = 0;
  16. var lastCount = 0;
  17. var interval;
  18.  
  19. function drawCount() {
  20. g.clear();
  21. var c = count.toString();
  22. g.drawString(c, g.getWidth()-g.stringWidth(c));
  23. g.flip();
  24. }
  25.  
  26. function updateCount(newCount) {
  27. if (interval) clearInterval(interval);
  28. interval = undefined;
  29. var diff = newCount - lastCount;
  30. if (lastCount===0 || diff<=0) {
  31. lastCount = newCount;
  32. count = newCount;
  33. drawCount();
  34. return;
  35. }
  36. lastCount = count;
  37. count = newCount;
  38. drawCount();
  39. interval = setInterval(function() {
  40. count++;
  41. drawCount();
  42. }, 60000/diff);
  43. }
  44.  
  45. function getPage() {
  46. require("http").get("https://www.googleapis.com/youtube/v3/videos?part=statistics&id="+VIDEO_ID+"&key="+API_KEY, function(res) {
  47. var data = "";
  48. res.on('data', function(d) { data += d; });
  49. res.on('close', function() {
  50. var json = JSON.parse(data);
  51. updateCount(json.items[0].statistics.viewCount);
  52. });
  53. });
  54. }
  55.  
  56. function onInit() {
  57. var spi = new SPI();
  58. spi.setup({ mosi:B3, sck:B5 });
  59. disp = require("MAX7219").connect(spi, B4, 4);
  60.  
  61. wifi = require("EspruinoWiFi");
  62. wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
  63. if (err) {
  64. console.log("Connection error: "+err);
  65. return;
  66. }
  67. console.log("Connected!");
  68. // Update immediately
  69. getPage();
  70. // Now update every 60 seconds
  71. setInterval(getPage, 60000);
  72. });
  73. }

Code

Source code

Credits

Photo of Espruino

Espruino

Espruino, Espruino Pico and Puck.js are low-power Microcontrollers that run JavaScript. Espruino is a JavaScript Interpreter for Microcontrollers that is designed to make development quick and easy. The Espruino interpreter is firmware that runs on a variety of different microcontrollers, but we also make Espruino Boards that come with the interpreter pre-installed and are the easiest devices to get started with. However Espruino itself isn't just the interpreter firmware or hardware - there's also the Web IDE, command-line tools, documentation, tutorials, and modules that form a complete solution for embedded software development.

   

Leave your feedback...