Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>


#define PIXEL_COUNT 1
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

// COLORS
int red = 0;
int blue = 0;
int green = 0;

// INPUT
int buttonPin = D3;

// PARTICLE CLOUD
long lastPublishedAt = 0;
int publishAfter = 3000;

// EVENT NAME
String event = "css19/diot/2019/paired/";

// TIME KEEPING
unsigned long pressTime = 0;

void setup() 
    {
         pinMode(buttonPin , INPUT_PULLUP);
         pinMode( PIXEL_PIN, OUTPUT );
         Particle.subscribe(  event , handleSharedEvent );
    }

void loop() 
    {
        int buttonState = digitalRead( buttonPin );
        if( buttonState == LOW)
            {
             // turn the LED On
            publishMyEvent();
            //doSolenoid();
            }
        else
            {
            // otherwise
            // turn the LED Off
            }
   
        
        if((millis()-pressTime) > 7000) 
            {
            setRGBColor(0,0,0);
            strip.setBrightness(255);
            } 
    
        unsigned long v = ((millis() - pressTime) / 7000.0);
        int k = int(255.0 * (1.0-v));
        strip.setBrightness(k);
    }

void setRGBColor( int r, int g, int b )
    {
        red = r;
        green = g;
        blue = b;
    
        strip.setPixelColor(0, red, green, blue);
        strip.show();
    }

void publishMyEvent()
    {
    // Remember that a device can publish at rate of about 1 event/sec,
    // with bursts of up to 4 allowed in 1 second.
    // Back to back burst of 4 messages will take 4 seconds to recover.
    // So we want to limit the amount of publish events that happen.

    // check that it’s been 10 secondds since our last publish
    if( lastPublishedAt + publishAfter < millis() )
        {
        // Remember our subscribe is matching  “db2018/paired/”
        // We’ll append the device id to get more specific
        // about where the event came from

        // System.deviceID() provides an easy way to extract the device
        // ID of your device. It returns a String object of the device ID,
        // which is used to identify your device.

     String eventName = event + System.deviceID();

     // now we have something like “diot/2019/paired/0123456789abcdef”
     // and that corresponds to this devices info

     // then we share it out
     Particle.publish( eventName, "Seema" );

     // And this will get shared out to all devices using this code

     // we just pubished so capture this.
     lastPublishedAt = millis();
         }

    }

void handleSharedEvent(const char *event, const char *data)
    {
     // Now we’re getting ALL events published using “db2018/paired/”
     // This includes events from this device.
     // So we need to ignore any events that we sent.

   // Let’s check the event name
   String eventName = String( event ); // convert to a string object
   // This gives us access to a bunch of built in methods
   // Like indexOf()
   // Locates a character or String within another String.
   // By default, searches from the beginning of the String,
   // but can also start from a given index,
   // allowing for the locating of all instances of the character or String.
   // It Returns: The index of val within the String, or -1 if not found.

   // We can use this to check if our event name contains the
   // id of this device

   String deviceID = System.deviceID();

   // device id = 0123456789abcdef
   // event = “diot/2019/paired/0123456789abcdef”

   if( eventName.indexOf( deviceID ) != -1 )
        {
        // if we get anything other than -1
        // the event came from this device.
        // so stop doing stuff
        pressTime = millis();
        blue = 255;
        strip.setBrightness(255);
        setRGBColor(red,0,blue);
        return;
        }

    // otherwise do your stuff to respond to
    // the paired device here

    //motorOn = true;
    pressTime = millis();
    red = 255;
    strip.setBrightness(255);
    setRGBColor(red,0,blue);

    }
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0