npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

websocket-neopixels

v0.0.2

Published

A simple Tessel script that demonstrates neopixel animation over a websocket

Downloads

4

Readme

video websocket-neopixels

This project demonstrates how to set up a server on Tessel that allows you to send arbitrary animations to Neopixels with a websocket. Neopixels is Adafruit's brand for strips of individually addressable RGB LEDs.

It uses the nodejs-websocket library to create the server and the neopixels library to write the animations.

I haven't tested it on animations greater than 10k bytes long but it worked well with my simple tests.

Install

npm install websocket-neopixels

Then, make sure you install the Neopixels firmware on your Tessel. Note that I have a PR open to merge Neopixels support into the official firmware build but it hasn't landed at the time of this writing.

Power your Neopixels(mine is just a small ring so I powered it with Tessel, PWR into VIN and GND into GND) and plug the data pin into G4 on Tessel.

Usage

server.js is a 30 line server that you'll run on Tessel. It simply opens a websocket server, parses any incoming streams, and routes the animation to the Neopixels.

Make sure your Tessel is connected to the same WiFi network as your PC (tessel wifi -n SSID -p PASS) before running the script. I didn't add any re/connect functionality to this example.

Once connected to wifi, run it with tessel run server.js.


var ws = require("nodejs-websocket"),
    Neopixels = require('neopixels'),
    neopixels = new Neopixels();
    tessel = require('tessel');
    port = 8000,

// Create the websocket server, provide connection callback
var server = ws.createServer(function (conn) {
  console.log("Accepted new connection...");

  // If get a binary stream is opened up
  conn.on("binary", function(stream) {
    // When we get data
    stream.on('data', function(data) {
      // Extract the number of LEDs to animate
      var numLEDs = data.readInt32BE(0);
      // Slice off that number
      var animation = data.slice(4);
      // Start the animation
      neopixels.animate(numLEDs, animation);
    });
  });

  conn.on("close", function (code, reason) {
      console.log("Connection closed")
  });
}).listen(port);

console.log('listening on port', port);

The client gets run with Node.js. It connects to the Tessel's server, then waits to receive "on" or "off" from the command line which will create an animation to either turn all the LEDs on or off.

Note that you need to change the address of the server to whatever your Tessel's IP Address is. You may also need to change the number of LEDs your animation. I was using a 24 pixel ring.

Wait until your Tessel reports that it's listening on port 8000, then run node client.js to connect to the Tessel. You can start typing 'on' and 'off' into the command line to animate your Neopixels.

var ws = require("nodejs-websocket");
var port = 8000;
// You may need to change this in your script
var numLEDs = 24;

// Set the binary fragmentation to 1 byte so it instantly sends
// anything we write to it
ws.setBinaryFragmentation(1);

var numLEDsBuffer = new Buffer(4);
numLEDsBuffer.writeUInt32BE(numLEDs, 0);

// Make a buffer to store the RGB values for each pixel
var onAnimation = new Buffer(numLEDs * 3);
// Make all the pixels white (equal R,G,B)
onAnimation.fill(0xff);

// Make another buffer to store animation for off buffer
var offAnimation = new Buffer(numLEDs * 3);
// Fill it with zero (R,G, and B are all off)
offAnimation.fill(0x00);

// When we get a connection (Put your Tessel's IP Address here!)
var connection = ws.connect('ws://192.168.8.110:' + port, function() {
  // Pipe the data to our server
  process.stdin.on('data', function(data) {
    // Check for off command
    if (data.toString() == 'off\n') {
      // Send the number of LEDs and the animation data
      connection.sendBinary(Buffer.concat([numLEDsBuffer, offAnimation]));
    }
    else if (data.toString() == 'on\n'){
      // Send the number of LEDs and the animation data
      connection.sendBinary(Buffer.concat([numLEDsBuffer, onAnimation]));
    }
  })
});