Back to Parent

Servo clothesServo ;
int clothesServoPin = A4;
int servoPosition = -1;
int targetPosition = 0;

const int CONNECT_DELAY_SECS = 30;
const int SLEEP_DELAY_SECS = 2 * 60;
const int SLEEP_TIME_SECS = 60 * 60;

bool manualMode = false;
bool hookResponseRequested = false;
bool hookResponseReceived = false;
unsigned long lastResponseTime = 0;

//Assigns the temperature returned from the webhook to a weather pointer value
//Will be put into cloud
int setClothes(String value) {
  Serial.println("Setting clothes " + value);
  lastResponseTime = millis();
  //weatherPointer.pointTo(value);

  int newAngle = getClothesAngle( value );
  Serial.println( newAngle );

  if( newAngle != targetPosition ){
    Serial.print( "Target now: " );
    Serial.println( targetPosition );
    targetPosition = newAngle;
  }

  return 0;
}

int getClothesAngle( String value )
{
  if( value == "hat" )
    return 70 ;
  else if( value == "jacket" )
    return 175 ;
  else if( value == "sweater" )
    return 105 ;
  else if( value == "tshirt" )
    return 140;

  return 180;
}

// Returns any text found between a start and end string inside 'str'
// example: startfooend  -> returns foo
String tryExtractString(String str, const char* start, const char* end)
{
    if (str == NULL) {
        return NULL;
    }

    int idx = str.indexOf(start);
    if (idx < 0) {
        return NULL;
    }

    int endIdx = str.indexOf(end);
    if (endIdx < 0) {
        return NULL;
    }

    return str.substring(idx + strlen(start), endIdx);
}

//Calculates the time between responses, so that clock can be put to sleep
unsigned long timeSinceLastResponse() {
  return millis() - lastResponseTime;
}


//Map actual weather conditions to the clothes required
String findClothes(String temperature, String wind)
{
  //var precip = weather.precipitation;
  // if(wind.match(/rain/i)) {
  //   return "umbrella";
  // } else if(precip.match(/snow/i)) {
  //   return "shovel";
  // }
  int temp = temperature.toInt();
  Serial.print( "temp: " );
  Serial.print( temp );
  Serial.print( "wind: " );
  Serial.println( wind );
  //var t = weather.temperature;
  if(temp < 33) {
    return "hat";
  } else if(temp < 55) {
    return "jacket";
  } else if(temp < 70) {
    return "sweater";
  } else {
    return "tshirt";
  }
}

//Function that is called when hook returns data
void setDataFromHook(const char *event, const char *value)
{
  String weather = String(value);
  //Serial.println("Received what-to-wear response");

  String locStr = tryExtractString(weather, "<location>",
"</location>");
  String weathStr = tryExtractString(weather, "<weather>",
"</weather>");
  String tempStr = tryExtractString(weather, "<temp_f>", "</temp_f>");
  String windStr = tryExtractString(weather, "<wind_mph>",
"</wind_mph>");

  Serial.println("The weather data at "+locStr+" has been received!");
  hookResponseReceived = true;
  hookResponseRequested = false;
  lastResponseTime = millis();

  String clothes = findClothes(tempStr,windStr);
  setClothes(clothes);
}

//Interfaces with the hook to get weather data
void getDataFromHook() {
  hookResponseRequested = true;
  Serial.println("Published weather event");
  Particle.publish("weather_xml", PRIVATE);
}

//Main function of the program
void setup()
{
  Serial.begin(9600);
  delay(200);

  //powerSupply.setup();
  //pointerPersistence.setup();
  Serial.println("Woke from " + String(manualMode ? "pin" : "timer/reset"));

  Particle.function("clothes", setClothes);
  Particle.subscribe("hook-response/weather_xml", setDataFromHook, MY_DEVICES);

  clothesServo.attach( clothesServoPin );
  delay( 100 );

  if(!manualMode) {
    lastResponseTime = millis();
    getDataFromHook();
  }

}

void loop() {
  // if(readyToSleep()) {
  //   Serial.println("Done waiting => Go to sleep");
  //   delay(200);
  //   goToSleep();
  // }


  if(timeSinceLastResponse() > SLEEP_DELAY_SECS * 1000 and !hookResponseRequested ) {

     getDataFromHook();

  }

  if( targetPosition != servoPosition ){
    Serial.print( "Setting servo to ");
    Serial.println( targetPosition );
    servoPosition = targetPosition;
    clothesServo.write( targetPosition );
    delay( 200 );
    //clothesServo.detach();
  }

  delay( 100 );

}
Click to Expand

Content Rating

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

0