Back to Parent

//Circuit #1
int buttonPiny = D2; 
int buttonPing = D3;
int ledPiny = D4;
int ledPing = D5;

int buttonStatey = LOW;
int buttonStateg = LOW;

long lastPublishedAt = 0;
 
int publishAfter = 1000;

void setup()
{
    pinMode( buttonPiny , INPUT_PULLUP);
    pinMode( buttonPing , INPUT_PULLUP);
    pinMode( ledPiny , OUTPUT );
    pinMode( ledPing , OUTPUT );
    Particle.variable("buttonG",buttonStateg);
    Particle.variable("buttonY",buttonStatey);
    Particle.subscribe(  "diot/2019/paired/" , handleSharedEvent );

}

void loop()
{
    buttonStatey = digitalRead( buttonPiny );
    buttonStateg = digitalRead( buttonPing );
    
    if (buttonStatey == 0 || buttonStateg == 0) 
    {
    publishMyEvent();

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

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


void publishMyEvent()
{

  if( lastPublishedAt + publishAfter < millis() )
  {
    String eventName = "diot/2019/paired/" + System.deviceID();
    Particle.publish( eventName, "data goes here" );
    lastPublishedAt = millis();
  }

}
void handleSharedEvent(const char *event, const char *data)
{
    String eventName = String( event ); // convert to a string object
    String deviceID = System.deviceID();

    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }
    else if ( eventName.indexOf( "e00fce68f98f1cc9288d63db" ) != -1){//melody's device
        digitalWrite( ledPing, HIGH);
        delay(1000);
        digitalWrite( ledPing, LOW);
        
    }
    else if ( eventName.indexOf( "e00fce68399c53fa4b94562a" ) != -1){//tala's device
        digitalWrite( ledPiny, HIGH);
        delay(1000);
        digitalWrite( ledPiny, LOW);
    }
    
}

//*****************************Code 1 end**************************************************



//**************************************************************
//Circuit #2
Servo myServo;
int servoPin = A3;
int servoPos = 0;
int newPos;
int showVolume = 0;
int waterVolume = 0;
int buttonPin = D2;
int buttonState=LOW;

long lastPublishedAt = 0;
//the time delay before publishing a new event from this device
int publishAfter = 10000;

void setup() {
    // attaches the servo on the A3 pin to the servo object
    myServo.attach( A3 );

    //Register Particle to control the servo
    Particle.function("servo", servoControl);
   
    //Keep cloud variables
    Particle.variable(  "servoPos" , &servoPos , INT );
    Particle.variable("button",buttonState);
    Particle.subscribe(  "diot/2019/smartShower" , handleSharedEvent );
    
    // sets button pin as input
    pinMode( buttonPin , INPUT_PULLUP);
}

void loop() {
    buttonState = digitalRead( buttonPin );
    //publish event when the button is pushed
    if( buttonState == 0 )
    {
        publishMyEvent();
    }else{}
}

int servoControl(String command)
{
    // Convert
    newPos = command.toInt();
    // Make sure it is in the right range
    //set the position
    servoPos = constrain( newPos, 0 , 180);

    // Set the servo
    myServo.write( servoPos );

    // done
    return 1;
}

void publishMyEvent()
{
  // check that it's been 10 secondds since the last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "diot/2019/smartShower" + System.deviceID();
      Particle.publish( eventName, "data goes here" );

      //capture publish time
      lastPublishedAt = millis();
  }
}

void handleSharedEvent(const char *event, const char *data)
{
    String eventName = String( event );
    // convert to a string object

    String waterVolumeIn = String(data);
    String deviceID = System.deviceID();

    if(eventName.indexOf( "e00fce68f3ec1a95fb53ad4c" ) != -1 ){
        waterVolume = waterVolumeIn.toInt();
        showVolume = map (waterVolume,0,6000,5,130);
        servoControl(String(showVolume));
    }
    else{
        return;
    }
}
Click to Expand

Content Rating

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

0