Back to Parent

Final Code
// Define a pin we'll place an LED on
int ledPin = D2;

// Pin locations on the Particle
int buttonPin = D0;
int flexPin = A0;
int fanPin = D1;

int flexReading = 0;
int flexValue = 0;

long lastPublishedAt = 0; //not sure what this is for?
// from this device
int publishAfter = 10000;

unsigned long duration;
unsigned long timeLength = 10000; // run blower for 10 seconds.

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin , INPUT_PULLUP);
  pinMode(flexPin, INPUT_PULLUP);
  pinMode(fanPin, OUTPUT);

  Particle.variable("flex", &flexReading, INT);
  Particle.variable("brightness", &flexValue, INT);

  Particle.subscribe(  "diot/2018/connected/bearinmind" , handleSharedEvent );
}

void loop()
{
    // find out if the button is pushed
    // or not by reading from it.
    int buttonState = digitalRead( buttonPin );
    flexReading = analogRead(flexPin);
    flexValue = map(flexReading, 3246, 3680, 0, 255);

    if( abs(flexValue) >= 175 )
    {
      int loopTime = millis();
      publishMyEvent();
      while(millis() <= loopTime + timeLength){
        digitalWrite( ledPin, HIGH);
        digitalWrite( fanPin, HIGH);
      }
    }else{
     digitalWrite( ledPin, LOW);
     digitalWrite( fanPin, LOW);
    }
}
void publishMyEvent()
{
  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "diot/2018/connected/bearinmind" + System.deviceID();
      // then we share it out
      Particle.publish( eventName, "Thinking of you..." );
      lastPublishedAt = millis();
  }

}

void handleSharedEvent(const char *event, const char *data)
{
    // Now we're getting ALL events published using "diot/2018/connected/bearinmind"
    // 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();

    if( eventName.indexOf( deviceID ) != -1 )
    {
      // if we get anything other than -1
      // the event came from this device.
      // so stop doing stuff
      return;
    }

    // otherwise do your stuff to respond to
    // the paired device here
    int loopTime = millis();
    duration = loopTime - millis();
    while(millis() <= loopTime + timeLength){
      digitalWrite( ledPin, HIGH);
      digitalWrite( fanPin, HIGH);
    }
}
Click to Expand

Content Rating

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

0