Back to Parent

Recording and Sending Circuit
//For Receiving and Sending Circuit

int RedPin = D4; //To identify the state of the RED Button
int GreenPin = D5; //To identify the state of the GREEN Button

int RedButton = A5; //For recording the Message
int GreenButton = A0; //For sending the Message

long lastPublishedAt = 0; //The last time an event was published
int publishAfter = 10000;// Time delay before publishing a new event

//Initial state of the button
int RedButtonState=1;
int GreenButtonState=1;

int PreviousRBS = 1;

bool MessageExists = false;
   
void setup()
{
    // Set Button pins as input
    pinMode( RedButton , INPUT_PULLUP ); 
    pinMode( GreenButton , INPUT_PULLUP); 

    Particle.variable("Message",RedButtonState); //to verify the state of the red button used for recording message
    
    
    // Set LED pins as output
    pinMode( RedPin , OUTPUT ); 
    pinMode( GreenPin , OUTPUT ); 
    
    //Subscribe to Langley's event to get feedback if my published event has been received
    Particle.subscribe(  "langley/2019/iot/receivedenchantedBookMessage/" , handleSharedEvent );
}

void loop()
{
    //Read the state of the button
    RedButtonState = digitalRead( RedButton );
    GreenButtonState = digitalRead( GreenButton );
    
    //Recording the Message
    if( PreviousRBS == HIGH && RedButtonState == LOW)
    {
        //Start recording 
        digitalWrite( RedPin, HIGH); // turn the LED On
        //message = "This is a Sample Message"; //Sample text to simulate voice recording
        MessageExists = true;
    }
    else if(PreviousRBS == LOW && RedButtonState == HIGH)
    {
        //Stop recording 
        digitalWrite( RedPin, LOW); // turn the LED Off
    }
    
    //Sending the Message
    if( GreenButtonState == LOW && MessageExists == true)
    {
        publishMyEvent(); 
        delay(100);
        digitalWrite(GreenPin, HIGH);
        MessageExists = false;
    }
    else
    {
        digitalWrite(GreenPin, LOW);
    }
    
    //saving the message
    
    PreviousRBS = RedButtonState; 
}


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 = "swapnil/2019/enchantedBook/" + 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, "Swapnil_Message" );

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

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

}

// Our event handlde requires two bits of information
// This gives us:
// A character array that consists of the event name
// A character array that contains the data published in the event we're responding to.
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
      return;
    }

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

    digitalWrite( GreenPin, HIGH);
    delay(500);
    digitalWrite( GreenPin, LOW); 
    delay(500);
    digitalWrite( GreenPin, HIGH);
    delay(500);
    digitalWrite( GreenPin, LOW);
    delay(500);
}
Click to Expand

Content Rating

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

0