Skills Dev V - Networking

Made by jmadala, Gabriela Suazo and Rachel Arre

We will be developing paired devices and networked interactions using Particle.publish. The devices will have a tilt sensor and a vibration motor. When the tilt sensor is tilted, the other device will vibrate.

Created: December 19th, 2020

0

Intention

Our goal was to pair a tilt sensor with a vibration motor to see if the tilt could start the vibration motor once tilted more than 30 degrees and then stop once it was back upright. We made this connection as part of our exploration work to see which two components were "sturdiest" and reliable in generating a notification on another device.

0

Approach

We broke this down into two key steps -- first, to correctky set up the tilt and vibrate relationship we were seeking followed by networking it through a Particle shared account

0

Step 1: Confirm tilt + vibrate setup

We started with the tilt and vibrate on one device to make sure we got that part of the setup correct.

0
int tiltPin = D3;
int motorPin = D2;
void setup()
{
  pinMode( tiltPin , INPUT_PULLUP ); 
  
  pinMode(motorPin, OUTPUT);
  
}
void loop()
{
    
    int tiltState = digitalRead( tiltPin );
    int speed = 100;
    
    if( tiltState == HIGH ) {
        for( int i = 0; i < 20; i++ ){
          analogWrite(motorPin, speed);
          delay(1000);
        }
    
    } else {
        analogWrite(motorPin, 0);
        delay(1000);
    }
}
Click to Expand
0
IMG 0811
Jody Techy - https://youtu.be/2IwuHWvEaZk
0

Step 2: Network the devices

Once we confirmed the tilt and vibrate worked on one device, we connected all three devices by moving all our devices onto one new Particle account and updated the code as below.

0
networked
Jody Techy - https://youtu.be/VZQTwZfOGhI
0
int motorPin = D2;
int tiltPin = D3;

// 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;

bool motorTriggeredByOtherDevice = false;

void setup()
{

    // setup the tilt switch
    pinMode( tiltPin , INPUT_PULLUP ); 
    pinMode(motorPin, OUTPUT);

  // We'll want to subscribe to an event thats fairly unique

  // From the Particle Docs
  // A subscription works like a prefix filter.
  // If you subscribe to "foo", you will receive any event
  // whose name begins with "foo", including "foo", "fool",
  // "foobar", and "food/indian/sweet-curry-beans".

  // Basically this will match any event that starts with 'db2018/paired/'
  // This is a feature we'll useto figure out if our event comes from
  // this device or another (see publishMyEvent below)

  Particle.subscribe(  "teampotato/" , handleSharedEvent );

}

int lastTiltState = LOW;

void loop()
{
    // publish my event
    // you'll want some more complex stuff here
    
    int tiltState = digitalRead( tiltPin );
    int speed = 250;
    

    
    if( lastTiltState != tiltState ){
        publishMyEvent();
    }
    
    lastTiltState = tiltState;
    
    
    if( motorTriggeredByOtherDevice ){
        
        for( int i = 0; i < 3 ; i++ ){
          analogWrite(motorPin, speed);
        }
        
        delay( 2000);
        
        analogWrite(motorPin, 0);
    
        motorTriggeredByOtherDevice = false;
    }
    

    // delay for a bit
    delay(100);
}



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 = "teampotato/" + 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, "data goes here" );

      // 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

    motorTriggeredByOtherDevice = true;

}
Click to Expand
0

Reflection

Setting up the two working components on one breadboard was manageable; however, getting the networking to work was much more challenging and we needed to reach out to Daragh for help understanding how to structure the code blocks and to use Particle.subscribe and millis ( ) correctly for what we hoped to accomplish. 

x
Share this Project

Courses

About

We will be developing paired devices and networked interactions using Particle.publish. The devices will have a tilt sensor and a vibration motor. When the tilt sensor is tilted, the other device will vibrate.