Back to Parent

Final Code
const int buttonPin1 = D1;
const int buttonPin2 = D2;

String weatherIcon = "";
double temperature = 0;
double precipProbability = 0;
double precipIntensity = 0;

long lastPublishedAt = 0;
// from this device
int publishAfter = 10000;

void setup(){
  pinMode(buttonPin1, OUTPUT);
  pinMode(buttonPin2, OUTPUT);
  //digitalWrite(buttonPin1, HIGH);
  //digitalWrite(buttonPin2, HIGH);

  pressButton1();
  delay(1000);
  pressButton1();

  pressButton2();
  delay(1000);
  pressButton2();

  Particle.subscribe("hook-response/forecast", handleForecastReceived, MY_DEVICES);
  Particle.subscribe(  "diot/2018/connected/atHome" , handleSharedEvent );
  getData();
}

void loop(){
  //pressButton1(); //turn humidifier on
  // getData();
  // delay(2000);
  // if (weatherIcon == "rain"){
  //   pressButton1();
  //   delay(500);
  //   pressButton1();
  // }else if(weatherIcon != "rain"){
  //   pressButton2();
  //   delay(5000);
  //   pressButton2();
  // }

}

void pressButton1(){
  digitalWrite(buttonPin1, LOW);
  delay(250);
  digitalWrite(buttonPin1, HIGH);
}

void pressButton2(){
  digitalWrite(buttonPin2, LOW);
  delay(250);
  digitalWrite(buttonPin2, HIGH);
}

void getData()
{
    // Publish an event to trigger the webhook
 Particle.publish("forecast", "19.0896,72.8656", PRIVATE);
}

void publishMyEvent()
{
  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "diot/2018/connected/atHome" + System.deviceID();
      // then we share it out
      Particle.publish( eventName, "scentActive" );
      lastPublishedAt = millis();
  }

}

void handleSharedEvent(const char *event, const char *data)
{
    // Now we're getting ALL events published using "diot/2018/connected/atHome"
    // 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();
    weatherIcon = String(data);
    //Particle.publish("motion activated");
    //Particle.publish(eventName , weatherIcon);

    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

    //getData();
    delay(250);
    if (weatherIcon == "rain"){
      pressButton1();
      delay(8000);
      pressButton1();
    }else if(weatherIcon != "rain"){
      pressButton2();
      delay(8000);
      pressButton2();
    }

}

void handleForecastReceived(const char *event, const char *data) {
 // Handle the integration response

 String receivedStr =  String( data );
 int loc1 = 0;
 int loc2 = 0;
 int loc3 = 0;
 int loc4 = 0;

 loc1 = receivedStr.indexOf("~");

 weatherIcon = receivedStr.substring(0,loc1);

 loc2 = receivedStr.indexOf("~",loc1+1);
 temperature = (double) String(receivedStr.substring(loc1+1,loc2)).toFloat();

 loc3 = receivedStr.indexOf("~",loc2+1);
 precipProbability = (double) String(receivedStr.substring(loc2+1,loc3)).toFloat();

 loc4 = receivedStr.indexOf("~",loc3+1);
 precipIntensity = (double) String(receivedStr.indexOf(loc3+1)).toFloat();

}
Click to Expand

Content Rating

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

0