Minecraft Server Glowing Status Cube

I made this for the eldest boy as a Christmas present.

It is based of the following cube: https://www.thingiverse.com/thing:524925

I printed it at 150% size and added an ESP8266 with a LED strip containing 4x LEDS.

It works by connecting to my server which runs a node app. The node app periodically checks the number of players on my server:

setInterval(updatePlayers, 1000*15);
var numberOfMineCraftPlayers = 0;
url = "https://api.mcsrvstat.us/2/myserver.myserver.com";
async function updatePlayers() {
    https.get(url,(res) => {
        let body = "";

        res.on("data", (chunk) => {
            body += chunk;
        });

        res.on("end", () => {
            try {
                let json = JSON.parse(body);
                console.log(json);
                numberOfMineCraftPlayers = json.players.online;
                console.log(`Number of online players: ${numberOfMineCraftPlayers}`)
            } catch (error) {
                console.error(error.message);
            };
        });

    }).on("error", (error) => {
        console.error(error.message);
    });
}

This updates the number of players variable with serves a simple GET route:

app.get('/minecraft', function (req, res) {
    let jsonData = { "players": numberOfMineCraftPlayers};
    res.json(jsonData);
});

On the Arduino/ESP8266 side the program works like this:

  1. Connect to WiFi
  2. Get number of players
  3. Divide the color spectrum into 8 parts
  4. Depending on the player dedicate a chunk of that color spectrum
  5. Iterate through each “hue” and use a sine wave function to change brightness
  6. Loop again

Here is the code:

#include <FastLED.h>
#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

const char* ssid     = "";         // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "";     // The password of the Wi-Fi network

// How many leds in your strip?
#define NUM_LEDS 5
#define DEVISIONS 8
// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define DATA_PIN D1
#define CLOCK_PIN 13

// Define the array of leds
CRGBArray<NUM_LEDS> leds;

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical
  Serial.begin(9600);         // Start the Serial communication to send messages to the computer
  delay(10);
  Serial.println('\n');

  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++i); Serial.print(' ');
  }

  Serial.println('\n');
  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer

  leds[0] = CRGB::Red; // shows green
  leds[1] = CRGB::Green; // shows red
  leds[2] = CRGB::Blue;
  FastLED.show();
  delay(5000);
  leds[0] = CRGB::Black;
  leds[1] = CRGB::Black;
  leds[2] = CRGB::Black;
}

void loop() {

  WiFiClient client;  // or WiFiClientSecure for HTTPS
  HTTPClient http;

  // Send request
  //http.useHTTP10(true);
  http.begin(client, "http://yourclient.com");
  http.GET();

  // Print the response
  // Serial.print(http.getString());

  // Parse response
  DynamicJsonDocument doc(2048);
  deserializeJson(doc, http.getStream());

  int players = doc["players"];
  Serial.println(players);
  // Serial.println(doc["port"]);


  // Disconnect
  http.end();

  // Now turn the LED off, then pause
  static uint8_t hue;
  //players = 5;

  if (players > DEVISIONS) {
    players = DEVISIONS;
  }
  int minHue = (256 / DEVISIONS) * players;
  int maxHue = ((256 / DEVISIONS) * players) + (256 / DEVISIONS);

  for (hue = minHue; hue < maxHue; hue++) {
    for (int i = 0; i < NUM_LEDS; i++) {
      // fade everything out
      leds.fadeToBlackBy(40);

      // let's set an led value
      leds[i] = CHSV(hue, 255, 255);
      FastLED.delay(16);
      Serial.print("Hue: ");
      Serial.println(hue);
    }
    for (int r = 0; r < 360; r++) {
      float angle = radians(r);
      int brightness = (255 / 2) + (255 / 2) * sin(angle);
      FastLED.setBrightness(brightness);
      //Serial.print("Brightness: ");
      //Serial.println(brightness);
      FastLED.delay(16);
      FastLED.show();
    }

    FastLED.show();
  }
  //delay(1500);
}

I didn’t put a lot of time into this. But it works as intended.

Leave a comment

Your email address will not be published. Required fields are marked *