Skills Dev V: Working with Networking

Made by Kevin Chou

Practice developing paired devices and networked interactions with Particle.publish

Created: December 5th, 2022

0

Outcome

Pressing the button on either Argon will cause the LED of the other to light up. Both argons are able to communicate with each other via publishing and subscribing through webhooks on their particle consoles.

0
0
int ledPin = D2;
int buttonPin = D4;
bool doLED = false;

void setup() {

    pinMode( ledPin, OUTPUT);
    pinMode( buttonPin, INPUT_PULLUP);

    Particle.subscribe( "blinkLED", handleActivateLED );
    blinkLED( 3, ledPin );

}

void loop() {

    int buttonValue = digitalRead( buttonPin );
    if( buttonValue == LOW ){
        Particle.publish( "doPairedPublish" );
    }

    if( doLED == true ){
        blinkLED( 6, ledPin );
        doLED = false;
    }

    delay( 1000 );

}

void handleActivateLED( const char *event, const char *data)
{
   doLED = true;
}
void blinkLED( int times, int pin ){
    
    for( int i = 0; i < times ; i++ ){
        digitalWrite( pin, HIGH );
        delay( 500 );
        digitalWrite( pin, LOW );
        delay( 500 );
    }
    
}
Click to Expand
0

Process

To get the two microcontrollers to communicate with each other and trigger each other's functions, webhooks needed to be set up on the particle console for each board. These webhooks would listen(subscribe to) for a specific event published by the particle board with the specific access token set upon creation, and publish a string (a function name in this case) that the board is in turn listening for to trigger that function (LED blinking in this case). As such, setting up two boards to publish and subscribe to each other via webhooks required that their access tokens be traded, and you had to make sure that the events they were subscribed to were all spelled and published the same for communication to occur properly.

0

Reflection

Getting the particle webhooks to work was a little finicky- for some reason, the webhooks will fail to trigger the microcontroller's particle functions if the board was flashed locally through the VS Code Particle Workbench extension rather than over the air via the online editor. This took quite some time to debug, but is good to know for future reference.

x
Share this Project

Courses

About

Practice developing paired devices and networked interactions with Particle.publish