Team MissU - Skills Dev V

Made by Yuchuaun Shan, Lulin Shan and Rahul Jha

As part of this exercise you’ll practice developing paired devices and networked interactions with Particle.publish

Created: December 6th, 2021

0

Project Goal

Share data between devices using Particle.publish and echo.

This is a prototype of our final project. There are two devices in the system. The switch button on one device controls the servo and neopixel on the paired device.

0
// set up neopixel: library, pixel COUNT, PIN and TYPE
#include "neopixel.h"
#define PIXEL_PIN A2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);


long lastPublishedAt = 0;
// time delay before we should publish a new event
int publishAfter = 5000;

// add switch
int switchPin = D3;
int buttonState;

// add servo
Servo myServo;
int servoPin = A3;
int servoPos = 0;

// variable to store availability
String availability = "unavailable";

void setup()
{
    pinMode(switchPin , INPUT_PULLUP);
    myServo.attach(A3);
    
    //******UPDATE EVENTNAME*******//
    Particle.subscribe(  "missU" , handleSharedEvent );
    

}

void loop(){
    
    publishMyEvent();
    
    // delay for a bit
    delay(100);
}


void publishMyEvent(){
    
    // check switch status
    buttonState = digitalRead(switchPin);
    
    // check that it's been 10 secondds since our last publish
    if(lastPublishedAt + publishAfter < millis()){
    
        //******UPDATE EVENTNAME*******//
        String eventName = "missU" + String(System.deviceID());
    
        // then we share it out
        // if switch is on, publish an event
        if(buttonState == LOW){
            availability = "available";
            Particle.publish(eventName, "available");
        }else{
            availability = "unavailable";
            Particle.publish(eventName, "unavailable");
        }
    
        // we just pubished so capture this.
        lastPublishedAt = millis();
    
    }

}


// Our event handle requires two bits of information
// 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){
    
    String eventName = String(event); 
    availability = data;

    String deviceID = System.deviceID();
    
    // device id = e00fce688578dddab57c1e9d
    // event = "missUe00fce688578dddab57c1e9d"
    
    if( eventName.indexOf( deviceID ) != -1 ){

      return;
    }
    
    turnOnAvailabilityLight(availability);
    servoControl(availability);

}

// function to turn on the window light based on availability(event data)
int turnOnAvailabilityLight(String command){

    if(command == "available"){
        for(int i=0; i< strip.numPixels(); i++){
            strip.setPixelColor(i, 255, 153, 51);
            strip.setBrightness(10);
            strip.show();
            delay(100);
        }
        return 1;
        
    }else{
        for(int i=0; i< strip.numPixels(); i++){
            strip.setPixelColor(i, 0, 0, 0);
            strip.show();
            delay(100);
        }
        return -1;
    }
}

// function to turn on servo based on availability(event data)
int servoControl(String command){
    
    if(command == "available"){
        myServo.write(100);
        return 1;
    }else{
        myServo.write(0);
        return -1;
    }

}
Click to Expand
0
Skills Dev V
Yu Chuan Shan - https://youtu.be/mZD2ktqlYd0
0

Reflection

We spent a lot of time pairing the devices. It took us a while to realize a few problems:

- When attaching deviceID in publishMyEvent(), the deviceID needs to be converted to String before being added to our custom event name (i.e. String eventName = "missU" + String(System.deviceID()))

- On the echo page (https://diotlabs.daraghbyrne.me/tools/echo/), the event name needs to be the full event name. At first we forgot to add the deviceID and it wouldn't relay the published data. 


x
Share this Project

Courses

About

As part of this exercise you’ll practice developing paired devices and networked interactions with Particle.publish