Monday, July 27, 2015

Cloud Connected NeoPixels Using The Particle Core

NeoPixels are a bright and colorful way to draw attention to wearable clothing, Arduino powered robots and more. As wonderful as all of those possibilities sound, there has been one thing I've wanted to try for a while. I've always wanted to control my NeoPixels via the cloud using a Particle Core (previously called a "Spark Core").

Particle Cores are microcontrollers that are quite similar to an Arduino Nano, however they have inbuilt Wi-Fi and come with a cloud based service that makes it easy to control your Core from afar. You can find out more on the Particle website. There are two newer versions of the device currently available for pre-order, the Particle Photon and the Particle Electron. The Photon has better reliability and is overall a faster and better upgrade to the Core. The Electron goes a step further and provides 2G/3G connectivity in over 100 countries, doing so via a global subscription service. It brings connectivity to a whole new and exciting level!

If you're looking to get started and you don't currently have a Particle device in your possession, try pre-ordering one of the newer options. At the time of writing, the Particle Cores are now all sold out! The demo below should still work on the newer devices, assuming that the NeoPixel library doesn't have any compatibility issues with the Photon. My Photon is still in the mail, so I can't check that just yet!

NeoPixels are a really neat brand of LED panel from Adafruit that can be hooked together in many different (and super colorful) ways. The NeoPixel grid I'll be using is an 8x8 NeoMatrix grid.

What We're Building

In this demo, we will be displaying a smiley face that will change depending on the mood sent to our Particle Core. If we send it a happy emotion, it will smile. If we send it a sad one, it will frown. We will send these emotions to it directly via very simple POST requests (you can replicate these by creating a form, AJAX site or server to initiate them, we will be keeping it simple, short and sweet by not defining one particular type of POST request method in this article).

The code and explanations for this demo assume you are familiar with your Particle Core, have set it up on your local Wi-Fi network and are familiar with how to flash code onto it via the Particle Build IDE. If you are new to the Particle Core and haven't tried anything with it yet, head to the Particle docs and read through the explanation on flashing apps first. Try flashing a simple LED blinking app to make sure you are all set up and ready to go. Assuming your Wi-Fi connectivity is pretty good, this should be a pretty smooth process.

The Demo

Want to skip ahead and see the final code? It is all available right here on GitHub.

The Sketch

Our sketch for our cloud connected, Particle-powered NeoPixel looks like so:

Particle and NeoPixels Sketch

A few important points when putting together the sketch above:

  • We have a 4700μF, 10V capacitor (Adafruit recommends at least 1000 μF, 6.3V or higher). The one I used was from AdaFruit. They very strongly recommend placing one of these in your set up before connecting the power.
  • There is also a 330 Ohm resistor between the data output pin and the input to the NeoPixel (Adafruit recommends somewhere between a 300 to 500 Ohm resistor). Whilst it might work without this, it is safest to include it!
  • Adafruit also strongly discourage connecting the NeoPixels to a live circuit. At the very least, always connect ground first, 5V and then data. When disconnecting it - disconnect in the reverse order.
  • We are powering the NeoPixels via a battery as the Particle Core does not provide the 5V power on its own that the NeoPixels need to light up. In this case, Adafruit recommends you power the pixels first, before powering the Particle Core.
  • If you don't have a battery pack but understand how to use a logic level shifter to get it up to 5V, you can do that instead. I don't have a logic shifter to try this out!
  • I am definitely not an electronics expert and strongly recommend reading up on NeoPixels via the NeoPixel Uberguide before putting the above sketch together. NeoPixels can be pretty easy to damage so be careful! All I can say with confidence is that the above sketch I made did not damage my own NeoPixels and theoretically should be okay. Make your own judgements first before trying it at home.

The Code

Everything we will be creating will be coded up within the Particle Build Web IDE that we use to flash code onto our Particle devices over Wi-Fi. It uses a similar simplifed version of C++ to Arduino along with the same software library called "Wiring" for common input/output functions. In other words, if you are an Arduino fan, you'll be coding in pretty much the exact same way.

We start by creating a new app within the Build IDE and adding a rather important library to it. To add pre-existing libraries to your Particle apps, select the Libraries option which looks a bit like a bookmark in the bottom left corner.

From the screen that appears, choose the "Neopixel" library underneath "Community Libraries", once it loads click the button that says "Include in App" to add it to the app we are creating.

That will add in the following lines to your app:

[code language="c"]
// This #include statement was automatically added by the Spark IDE.
#include "neopixel/neopixel.h"
[/code]

Notice it says something about "Spark"? I mentioned it briefly at the start of the article but thought I'd point this out once more to avoid confusion. The name "Spark" will appear throughout the code as it was the old name for "Particle". I'd say the name will fully change over soon but my demo still has the old name in it. I'm not quite sure if they've updated the inner workings of their API or not yet.

After that, we define three constants for our app. We define the pin the NeoPixel grid is connected to, the number of pixels in our grid and the type of NeoPixel we have.

[code language="c"]
#define PIXEL_PIN D7
#define PIXEL_COUNT 64
#define PIXEL_TYPE WS2812B
[/code]

We then define our Adafruit_NeoPixel object with those three constants.

[code language="c"]
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
[/code]

Continue reading %Cloud Connected NeoPixels Using The Particle Core%


by Patrick Catanzariti via SitePoint

No comments:

Post a Comment