Back to Parent

# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
// 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;
int buttonPin1 = D0;
int buttonPin2 = D1;

 
void setup()
{

pinMode( buttonPin1, INPUT_PULLUP); // sets pin as input
pinMode( buttonPin2, INPUT_PULLUP);

Serial.begin(9600);
Serial1.begin(9600);

execute_CMD(0x3F, 0, 0); // Send request for initialization parameters

while (Serial1.available()<10) // Wait until initialization parameters are received
delay(30); // Pretty long delays between successive commands needed 
// Initialize sound to very low volume. Adapt according used speaker and wanted volume
execute_CMD(0x06, 0, 90); // Set the volume (0x00~0x30)

// Basically this will match any event that starts with "loveyou" or "hug" to figure out if our event comes from this device or another

Particle.subscribe(  "loveyou" , handleSharedEvent );
Particle.subscribe(  "hug" , handleSharedEvent );
}
void loop ()

{ 
    publishMyEvent();

}

void publishMyEvent()

{
    
    if( lastPublishedAt + publishAfter < millis()){

if( digitalRead( buttonPin1 ) == LOW )
{
    
      // System.deviceID() provides an easy way to extract the device
    //event name appended with device id to get a unique identifier for the device
    String eventName = "loveyou" + System.deviceID();
 
    Particle.publish(eventName,"button1");

}

else if( digitalRead( buttonPin2 ) == LOW )

{
    String eventName = "hug" + System.deviceID();
  
        // share it out and this will get shared out to all devices
}
    lastPublishedAt = millis();
 

}

}
void execute_CMD(byte CMD, byte Par1, byte Par2) // Excecute the command and parameters
{
   if( lastPublishedAt + publishAfter < millis() )


 
    lastPublishedAt = millis();

  // }
// Calculate the checksum (2 bytes)
int16_t checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);

// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, Par2, checksum >> 8, checksum & 0xFF, End_Byte};

//Send the command line to the module
for (byte k=0; k<10; k++)
{
Serial1.write( Command_line[k]);
}
 
}

void handleSharedEvent(const char *event, const char *data)
{
  String eventName = String( event ); // convert to a string object
  String dataStr = String( data ); // convert to a string
  String deviceID = System.deviceID();//get the device id

 
   if( eventName.indexOf(deviceID) != -1 )
   {
       // It Returns: The index of val within the String, or -1 if not found.
// if we get anything other than -1 the event came from this device so stop doing stuff
    return;
   }
   
 //else do stuff corresponding to the paired device
 
  if( dataStr.equals( "button1" ))
  {
      //play the first song
      execute_CMD(0x01,0,001);

  }
  else if( dataStr.equals( "button2" ))
  {     //play the second song
      execute_CMD(0x02,0,001);

  }
}
Click to Expand

Content Rating

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

-1