Back to Parent

// variables will change:
int pressureState = 0;         // variable for reading the pushbutton status
int pressurePlate = D3;    

// This value will store the last time we published an event
long lastPublishedAt = 0;
// this is the time delay before we should publish a new event from 
// this device
int publishAfter = 1000;

// DO NOT CHANGE THIS
String sharedEventName = "diot/2019/smartstudio/";
// device specific event name
String deviceEventName = "napTech/";

//Boolean 'opened' is used to store whether the blinds are:
//open (true) 
//or closed (false)
bool napping = false;

void setup()
{


  // initialize the pressure plate pin as an input:
  pinMode(pressurePlate,INPUT_PULLUP);  
  //initialize pressure state
  pressureState = digitalRead(pressurePlate);
   
}

void loop()
{

  // check if the pressure plate is pressed.
  // if it is, the pressureState is LOW:
  delay(2000);
  pressureState = digitalRead(pressurePlate);
  //if the pressure plate is pressed but no one was napping
  //if the state changes from what was previously shown, ie
  //this is to ensure that if someone shifts and the state 
  //changes it doesnt stop the nap because the person is still
  //napping.
  if (pressureState == LOW && !napping){    
    // publish an event to close the curtain:   
    publishMyEvent("close",(String)pressureState );
    // someone is now naping
    napping = true;
  }

  else if(pressureState == HIGH && napping){   
    // publish an event to close the curtain
    publishMyEvent("open",(String)pressureState );
    // no one is napping now
    napping = false;
    
  }

}


void publishMyEvent(String eventID, String data)
{
  // 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.
      // This will build 'diot/2019/smartstudio/myEvent/A13231245345A078'

      String eventName = sharedEventName + deviceEventName + eventID +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, data);

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

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

}
Click to Expand

Content Rating

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

0