Back to Parent

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

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0