Connecting Neopixels on 2 particles

Made by ihans

Developing paired devices and networked interactions with Particle.publish

Created: November 30th, 2021

0

Intention: 

These are smaller exercises that build on each other to help get to the final prototype for team blossom (for the purpose of documentation and to trace steps back if needed at any point.)

0

Exercise 1: Connect two neopixels through Particle.publish and particle. subscribe

Result: The code worked fine except that I couldn't find the device id so haven't been able to test if the devices are paired.

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int r = 0;
int b = 0;
int g = 0;
long lastPublishedAt = 0;
int publishAfter = 100; //the initial delay
bool blueOn = false; //color for Lily = blue

void setup() {
    strip.begin();
    Particle.subscribe("diot2021blossomneopixel" , handleSharedEvent);

}

void loop() {
    // strip.show(); neopixel strip is off in the beginning
    uint16_t i;
    uint32_t c = strip.Color(0, 0, 255); // color Blue
    publishPixelon();
    if (blueOn == true){
        for( int i = 0; i < strip.numPixels(); i++ ){
            strip.setPixelColor(i, c); // each pixel turning on blue one by one
            strip.show();
            delay( 200 );
        }
        for ( int i = 15; i > 0; i-- ){
            strip.setPixelColor(i, c); // each pixel turning off one by one
            strip.show();
            delay( 200 );
        }
        
        blueOn = false;
    }    
    delay (100);
}

void publishPixelon(){
    if ( lastPublishedAt + publishAfter < millis() ){
        String pixelOn = "diot2021blossomneopixel";
        if(blueOn == false){
            Particle.publish(pixelOn);
        }
        lastPublishedAt = millis();
    }
}


void handleSharedEvent(const char *event, const char *data){
    String pixelOn = String(event); // this is reading the function as per particle.publish, convert to a string object first
    String deviceID = System.deviceID();
    // device id = 
    // event being triggered = "diot2021blossomneopixel"
    if( pixelOn.indexOf( deviceID) != -1 ){
        return;
    }
    else{
        blueOn = true;
    }
}
Click to Expand
0

Exercise 2: Turn on the neopixel through the console while retaining the particle.publish and particle.subscribe as in the earlier step

Result: I tested this through the console and the code works perfectly so far (minus testing of the pairing)

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int r = 0;
int b = 0;
int g = 0;
long lastPublishedAt = 0;
int publishAfter = 100; //the initial delay
bool blueOn = false; //color for Lily = blue

void setup() {
    strip.begin();
    Particle.subscribe("diot2021blossomblue" , handleSharedEvent);
    Particle.function("diot2021blossomnblue", BluePixelOn); //to turn on Lily's neopixel from the console

}

void loop() {
    // strip.show(); neopixel strip is off in the beginning
    publishPixelon(); //to give ddetails of the event to be published to all devices
}


int BluePixelOn(String b){ //functions for particle.function start with an int and take only string commands
  
        int bValue = b.toInt();  //first:convert string to number
        uint32_t blue = strip.Color(0, 0, bValue); //second: use the newly converted int to define blue

        uint16_t i;
        if (blueOn == true){
            for( int i = 0; i < strip.numPixels(); i++ ){
                strip.setPixelColor(i, blue); // each pixel to turn on blue one by one
                strip.show();
                delay( 200 );
            }
            blueOn = false;
        }
   
        delay(100);
    
        return 1;
}

void publishPixelon(){
    if ( lastPublishedAt + publishAfter < millis() ){
        String pixelOn = "diot2021blossomblue";
        if(blueOn == false){
            Particle.publish(pixelOn);
        }
        lastPublishedAt = millis();
    }
}


void handleSharedEvent(const char *event, const char *data){
    String pixelOn = String(event); // this is reading the function as per particle.publish, convert to a string object first
    String deviceID = System.deviceID();
    // device id = 
    // event being triggered = "diot2021blossomneopixel"
    if( pixelOn.indexOf( deviceID) != -1 ){
        return;
    }
    else{
        blueOn = true;
    }
}
Click to Expand
0

Exercise 3: 

Result: 

x
Share this Project

Courses

About

Developing paired devices and networked interactions with Particle.publish