How to Query the Status of the 11 Internet Button LEDs

I’ve been successful in achieving my main objective in having the Internet Button trigger a php program using Web hooks. I’ve been looking for code that would allow me to query the state (on/off, color, brightness) of each of the 11 LEDs on the button. Haven’t been able to find it. Ideally, I’d like to be able to run a PHP program that would prompt the button to transmit the LED states.

Any direction from the community will be appreciated.

Currently, there is no way with the InternetButton library to get the colors of the LEDs. You either need to keep track of those colors when you set them with your own variables, or modify the InternetButton library to add a function to get the colors. That wouldn’t actually be difficult. You would need to add new .h and .cpp files to your project, and copy the InternetButton files into those, so you can modify the code. You would only need to add the following to the .h and .cpp files,

To the .cpp

uint32_t InternetButton::getPixelColor(uint16_t n) {
    return ring.getPixelColor(n);
}

To the .h,

uint32_t getPixelColor(uint16_t n);

To get all the values, you would have to loop through the 11 LEDs, convert the ints to strings, and combine them into one string variable (probably with some type of encoding like base85) that you could read from your php (via a Particle.variable). There already is a getBrightness() function that you can use. There is no on or off state, off is just having the rgb values for the color all set to 0.

1 Like

While that is possible, I question the need for that :wink:
Your code sets the LED states I’d assume, so your code should be able to keep track of the states it set and hence know at all times what the LEDs are supposed to show.

BTW, the easiest way to expose the actual LED states would be to expose the underlying NeoPixel object ring and use all the NeoPixel methods of it.

1 Like

Thanks. I’ll look into it!

It’s true that my code sets the colors, but it would be nice to be able to query the button directly rather than having to check the intermediate file I keep on my website. One fewer possible point of failure.

I’ll look up the ring object.

Easiest for you maybe :confused: . This is actually the first thing I tried, but there's something I'm clearly not understanding about C++. I tried to add a variable to the InternetButton.h file (Adafruit_NeoPixel ring; ), but it gives me this error,

no matching function for call to 'Adafruit_NeoPixel::Adafruit_NeoPixel()'

I don't understand why the compiler thinks a variable declaration is a call to a constructor (one that doesn't exist).

That is because that line would already call a default constructor (with empty parameter list).
You can’t declare an object variable without an underlying object - only object pointers can be uninitialised.
But Adafruit_NeoPixel lib does not expose a public default constructor.

But since InternetButton.cpp features this line

 Adafruit_NeoPixel ring = Adafruit_NeoPixel(PIXEL_COUNT, pin, PIXEL_TYPE);

where pin == 3 == D3
you could just comment that line out and put this into the class definition (InternetButton.h)

Adafruit_NeoPixel ring(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

and move these definitions from the bottom of InternetButton.h to the top

#define PIXEL_COUNT 11
#define PIXEL_PIN D3
#define PIXEL_TYPE WS2812B

You could either have that ring variable declared public, or better have it private and supply a public getter method to have “readonly” access to the object variable.

Ok, I think that is the point I missed. Most of my experience is with Objective-C and Swift, and in those languages the variables (for class objects) are object pointers; explicitly so in Objective-C (with the use of *), and behind the scenes in Swift. The pitfalls of being a self-taught coder; it's one thing to learn to code, and another to really understand all the intricacies.

2 Likes

The replies so far have been very helpful, but I’m still missing something very basic: how to connect to the Internet Button from PHP, either directly or by triggering an event. Again, any advice pointing me in the right direction will be much appreciated.

Check out this post about interacting with the Particle cloud through php. Using the code in that link will allow you to grab the value of a Particle Variable from a device.

Wow! That’s terrific! Thank you!