Youngjoo, Hongyu, Ziyu - Skills Dev V: Working with Networking

Made by ziyuh and youngjol

Created: December 9th, 2021

0

Project: Happy Birthday Song Receiver / Sender 

Objective: Create a connected system with three Particle Argon devices such that each device can send a signal to play 'Happy Birthday' on the other two devices. 


Team: Youngjoo Lee, Ziyu Han, Hongyu Mao

0

Process

We began by writing a Happy Birthday song function to play on a Piezo. 

Once we tested that this played the song properly on our individual devices, we used Particle's Publish/Subscribe feature to connect the devices. Each device publishes an event with a unique name when the button on that device is pressed. Each device is also subscribed to the other two devices' events so that when one device's button is pressed, the two remaining devices starts to play the song. 

Issues

  • We ran into an issue with the subscribe feature initially. Although each device was subscribed to the other event names, it would not pick up when other devices published an event of that name. We tried to debug the code, but nothing seemed to be an issue there. We realized it was because we did not use the Echo Relay. 
  • Once we used it, it also did not work. Then, we realized our second mistake: we did not add the Access Token of our own devices when using the Echo Relay tool - we only added each others'. After we fixed this, the connected system worked as expected. 
  • Lastly, when we tested the audio, the volume from the piezo was too small. We crafted a handmade paper amplifier to increase the volume a bit. 
0
long lastPublishedAt = 0;
int publishAfter = 10000;


int speakerPin = D3;
int buttonPin = D2;
int val;

int handled = 0;

int C = 262;
int D = 294;
int E = 330;
int F = 349;
int G = 392;
int A = 440;
int B = 494;
int highC = 540;

int melody[] = {C, C, D, C, F, E, C, C, D, C, G, F, C, C, highC, A, F, E, D,
                B, B, A, F, G, F};
int noteDurations[] = {16/3, 16, 4, 4, 4, 2, 16/3, 16, 4, 4, 4, 2, 16/3, 16, 4, 4, 4, 4, 4, 16/3, 16,
                        4, 4, 4, 2}; //  4 = 1/4 note, 8 = 1/8 note, etc.
int numNotes = arraySize(melody);

void setup() {
    pinMode(buttonPin, INPUT_PULLUP); 
    Particle.subscribe("diotZiyu", handleSharedEvent);
    Particle.subscribe("diotHongyu", handleSharedEvent);
    Particle.variable("Handled", handled);
    
}

void loop() {
    
    val = digitalRead(buttonPin);
    
    if (val == LOW){ // doorbell is pushed
        publishEvent();
        // playHappyBirthday();
        delay(2500);
    }
    
    delay(100);
}

void publishEvent() {
    if(lastPublishedAt + publishAfter < millis() ){
        String eventName = "diotYJ";
        Particle.publish(eventName, "data");
        
        lastPublishedAt = millis();
    }
}

void handleSharedEvent(const char *event, const char *data){
    String eventName = String(event);
    String deviceID = System.deviceID();
    handled = 1;
    if (eventName.indexOf("diotYJ") != -1) { // i.e. it's a self-published event
        return;        
    } else{
        playHappyBirthday();
    }
    
}
Click to Expand
0

Figure 1. Wiring of piezo and button onto Argon

0
Figure 2. Two of Connected Devices (with Amplifier)
0

Video: Connected Devices Interaction

0
dev5
Youngjoo Lee - https://youtu.be/9zxljnB63SA
x