Skills Dev V - Malika, Yang, Zhenfang

Made by malikak, yangb and Zhenfang Chen

Demonstrate networking with Particle devices.

Created: December 19th, 2020

0

Intention

The project is about building the connection between two devices. Once A presses a button, B's fan will run at the same time. 

0

Approach

We wanted to test a simple networking project where one device uses a button as a trigger to create a Particle event, which another device responds to using an event handler.

0
// PUBLISHING
bool fanMove = false;
long lastPublishTime = 0;
int publishAfter = 1000;
int buttonPin = D3;


void setup() {
    Particle.subscribe(  "diot/2020/paired/" , handleSharedEvent , ALL_DEVICES);
    pinMode( buttonPin , INPUT_PULLUP);
}

void loop() {
    int buttonState = digitalRead( buttonPin );
    if( buttonState == LOW ){
        fanMove = true;
        publishStatus();
    }
}

void publishStatus() {
    
    if (millis() > publishAfter+lastPublishTime) {
        String eventName = "diot/2020/paired/" + System.deviceID();
        String msg = "no activity";
        if (fanMove) {
          msg = "MOVE";
        }
        Particle.publish(eventName, msg);
        lastPublishTime = millis();
        // reset fanMove status
        fanMove = false;
    }
}

void handleSharedEvent(const char *event, const char *data) {
    String eventName = String( event );
    String dataStr = String(data);
    String deviceID = System.deviceID();
    // eventName contains our ID -> ignore
    if ( eventName.indexOf(deviceID) != -1) {
        return;
    }
}
Click to Expand
0
int solPin = D2;
bool shouldActivate = false;
long lastPublishTime = 0;
int publishAfter = 1000;

void setup()
{
  Particle.subscribe(  "diot/2020/paired/" , handleSharedEvent );
  
  Particle.function( "activate", activateSolenoid );
  
  pinMode(solPin, OUTPUT);
}

void loop()
{
  if( shouldActivate ){
    doSolenoid(  );
    shouldActivate = false;
  }
  delay( 100 );

}

void doSolenoid(  ){
  for( int i = 0; i < 5; i++ )
  {
    digitalWrite(solPin, HIGH);
    delay( 100 ) ;
    digitalWrite(solPin, LOW);
    delay( 100 );
  }
}


int activateSolenoid( String command ){
  shouldActivate = true;
  return 1;

}




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( lastPublishTime + 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 = "diot/2020/paired/" + 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, "no activity" );

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

      // we just pubished so capture this.
      lastPublishTime = 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
    //   shouldActivate = true;
      return;
    }

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

    //motorOn = true;
    String dataStr = String( data );
    if (dataStr == "MOVE") {
        shouldActivate = true;
    }
}
Click to Expand
0

Process

  • We wrote code for the device with a button to send a Particle event to all other devices that says "MOVE" when it is pressed. The code for the device with a fan handles shared events with the data "MOVE" by flagging the fan to turn on.
  • We ran into some problems with Zhenfang's device being stuck offline and refusing to go into safe mode :(

0

Reflection

  • From this project, we created the connection between two devices: the button and the fan. It's quite inspiring since we think it's a good start to really build the internet of things. During the process of coding, we understood more about the 'public-subscribe model' for Particle platform. Also, it's cool to figure out how to use a flag function and an executive function to manipulate the components. In a word, we can take this project as a good start to dig more about the connection between different devices and build more complex interactions.
  • We learned that it's a good idea to keep the publish interval quite short for the trigger device in order to make the experience feel more interactive.

0
skill dev5
x
Share this Project

Courses

About

Demonstrate networking with Particle devices.