Colin O'Dell
Published © CC BY-NC-SA

Packagist Download Counter

A 7-segment display shows how many times an open-source PHP library has been downloaded by pulling data from packagist.org.

BeginnerFull instructions provided4 hours1,362
Packagist Download Counter

Things used in this project

Hardware components

Photon
Particle Photon
×1
MAX7219 8-Digit Red LED Display Module
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Yahoo Query Language
Packagist

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
Dremel 200 Series

Story

Read more

Custom parts and enclosures

Custom Enclosure

Custom project enclosure designed in Tinkercad.

Schematics

Schematic

Code

Firmware

C/C++
Firmware built using Particle Build online IDE and flashed to the Photon. Requires the LedControl-MAX7219-MAX7221 library to be included with the project.
// This #include statement was automatically added by the Particle IDE.
#include "LedControl-MAX7219-MAX7221/LedControl-MAX7219-MAX7221.h"

#include <math.h>

LedControl lc=LedControl(D6,D4,D5,1);

int count = 0;

void setup() {
    
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* and clear the display */
  lc.clearDisplay(0);
  
  Particle.subscribe("hook-response/commonmark-count", gotPackagistCount, MY_DEVICES);
  
  lc.setChar(0, 0, '-', true);
  lc.setChar(0, 1, '-', false);
  lc.setChar(0, 2, '-', false);
  lc.setChar(0, 3, '-', false);
  lc.setChar(0, 4, '-', false);
  lc.setChar(0, 5, '-', false);
  lc.setChar(0, 6, '-', false);
  lc.setChar(0, 7, '-', false);
  
  Particle.publish("commonmark-count");
}

void loop() { 
  // Fetch updates every 5 minutes
  delay(1000 * 60 * 5);

  // Set the last decimal point to indicate that we're fetching an update
  // To do this, we need to set the last digit as well
  lc.setDigit(0, 0, count % 10, true);

  Particle.publish("commonmark-count");
}

void gotPackagistCount(const char *name, const char *data) {
  char *key = "total\":\"";
  char *ptrTotal = strstr(data, key);
  if (ptrTotal == NULL) {
    return;
  }

  ptrTotal += strlen(key);

  int x = 0;
  char result[8];
  while (x < 8 && *ptrTotal != '"') {
      result[x++] = *ptrTotal++;
  }
  result[x] = '\0';
  count = strtol(result, NULL, 10);
    
  // Display the count
  displayCount();
}

void displayCount() {
  int i = 0;
  int n = numPlaces(count);
  do {
    int digit = count % 10;
    lc.setDigit(0, i, digit, false);
    i++;
    count=(int)(count/10);
  } while (i < n);

  while(i < 8) {
    lc.setChar(0, i, ' ', false);
      i++;
  }
}

int numPlaces (int n) {
  if (n == 0) return 1;

  return floor (log10 (abs (n))) + 1;
}

particle-cli command to create the webhook

Plain text
particle webhook GET commonmark-count "https://query.yahooapis.com/v1/public/yql?q=select%20downloads.total%20from%20json%20where%20url%3D'https%3A%2F%2Fpackagist.org%2Fpackages%2Fleague%2Fcommonmark.json'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"

Credits

Colin O'Dell

Colin O'Dell

1 project • 43 followers
Lead Web Developer at Unleashed Technologies. Author of league/commonmark. Conference speaker. Arduino enthusiast.

Comments