Back to Parent

#include <string.h>
#include <stdio.h>

String location=NULL;
String weather=NULL;
String temperature=NULL;
String wind=NULL;
String precip=NULL;
String fv;
String foreValue=NULL;
String foreString=NULL;
String weath_str=NULL;
String weathstr_in=NULL;
String weathstr_out=NULL;
String weath_cond=NULL;

int loc=0;
int weath=0;
int temp=0;
int win=0;
int prec=0;
int forecast_string=0;
int forecast_value=0;

bool cloudy;
bool sunny;
bool rainy;
bool snowy;

int servoPin1 = A4;
int servoPosition1 = 90;
Servo servo1; //Body Servo

int servoPin2 = A5;
int servoPosition2 = 90;
Servo servo2; //Head Servo

int servoPin3 = D0;
int servoPosition3 = 80;
Servo servo3; //Tail Servo

int vibratorPin = D6;
int vibratorspeed = 255;
int vibratortime = 5;

int swingtime = 5; //body movement
int swingrange1 = 75;
int swingrange2 = 105;

int shaketime = 5; // head movement
int shakerange1 = 70;
int shakerange2 = 120;

int tailtime = 5; // tail movement
int tailrange1 = 30;
int tailrange2 = 150;

int rain = 0;
int sun = 0;
int cold = 0;

void setup(){
  Serial.begin(9600);

  setupParticleData();

  servoAttach();

  servo1.write( servoPosition1 );
  servo2.write( servoPosition2 );
  servo3.write( servoPosition3 );
  pinMode(vibratorPin, OUTPUT);

  for(int i=0;i<7;i++)
  {
    publish_weather_event();
    if(location != NULL && temperature != NULL && wind != NULL && precip != NULL)
    {
      printData();
      break;
    }
    delay(5000);
  }

  for(int i=0;i<7;i++)
  {
    publish_weather_forecast();
    /*weath_str=foreValue;*/
    if(foreValue!=NULL)
    {
      weathstr_out=foreValue;
      break;
    }
  }
}

void setupParticleData(){
  Particle.subscribe("hook-response/weather_forecast",setForecastFromHook,MY_DEVICES);
  Particle.variable("temperature",&temperature,STRING);
  Particle.variable("wind",&wind,STRING);
  Particle.variable("location",&location,STRING);
  /*Particle.variable("fs",&foreString,STRING);*/
  Particle.variable("fv",&foreValue,STRING);
  Particle.variable("precip",&precip,STRING);
  Particle.variable("weather",&weath_str,STRING);
  Particle.variable("conditions",&weath_cond,STRING);

  Particle.function("vspeed", set_v_speed);
  Particle.function("vtime", set_v_time);
  Particle.function("Rain", setrain);
  Particle.function("Sun", setsun);
  Particle.function("Cold", setcold);
}

void servoAttach(){
  servo1.attach( servoPin1 );
  servo2.attach( servoPin2 );
  servo3.attach( servoPin3 );
}

String check_weather_conditions()
{
  cloudy = check_substring("cloud");
  if(cloudy)
    return "It's cloudy.";

  sunny = check_substring("sun");
  if(sunny)
    return "It's sunny.";

  snowy = check_substring("snow");
  if(snowy)
    return "It's snowy.";

  rainy = check_substring("rain");
  if(rainy)
    return "It's rainy.";
  /*if(check_substring("snowyday","snow"))
      weath_str="Substring is working!";*/

  /*check_substring("snow");*/
}

bool check_substring(String sub)
{
  if(foreValue==NULL)
  {
    weath_str="Forevalue is null!";
    return false;
  }
  else
  {
    String val = String(foreValue);
    std::string str (foreValue);
    std::string str2 (sub);

    weath_str="Inside substring!";

    size_t found = str.find(str2);
    if(found!=std::string::npos)
    {
      weath_str="Substring found!";
      return true;
    }

    return false;
  }

}

void publish_weather_forecast()
{
  Serial.println("Published weather forecast.");
  Particle.publish("weather_forecast","",PRIVATE);
}

// 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);
}

void setForecastFromHook(const char *event, const char *value)
{
  String weather = String(value);
  foreValue = tryExtractString(weather,"<icon>","</icon>");
  foreString = tryExtractString(weather,"<fcttext>","</fcttext>");

  if(foreValue!=NULL)
  {
    /*weath_str=foreValue;*/

    weath_cond = check_weather_conditions();

    Serial.println("Inside forecast str.");

  }
}

void printForecast()
{
    Serial.print("Following is what is expected today:");
    Serial.println(foreValue);

    Serial.print("The forecast conditions for the day are: ");
    Serial.println(foreString);
}

void printData()
{
    Serial.print("The weather data has been received for location: ");
    Serial.println(location);

    Serial.print("The temperature (in deg F) is: ");
    Serial.println(temperature);

    Serial.print("The wind speed (in mph) is: ");
    Serial.println(wind);

    Serial.print("The precip (in mm) is: ");
    Serial.println(precip);
}

//Function that is called when hook returns data
void setDataFromHook(const char *event, const char *value)
{
    String weather = String(value);

    /*Serial.println(weather);*/

    String locStr = tryExtractString(weather,"<city>","</city>");

    /*if(locStr==NULL)
      Serial.println("Loction string is null!");*/

    if(locStr!=NULL && loc==0)
    {
      location = locStr;
      loc++;
      /*Serial.println("Inside locstr.");*/
    }

    String tempStr = tryExtractString(weather,"<temp_f>","</temp_f>");

    if(tempStr!=NULL && temp==0)
    {
      temperature = tempStr;
      temp++;
      /*Serial.println("Inside tempstr.");*/
    }

    String windStr = tryExtractString(weather,"<wind_mph>","</wind_mph>");

    if(windStr!=NULL && win==0)
    {
      wind = windStr;
      win++;
      /*Serial.println("Inside windstr.");*/
    }

    String precStr = tryExtractString(weather,"<precip_today_string>","</precip_today_string>");

    if(precStr!=NULL && prec==0)
    {
      precip = precStr;
      prec++;

    }
}

void publish_weather_event()
{
  Serial.println("Published weather event.");
  Particle.publish("weather_xml","",PRIVATE);
}
void loop(){
  if (cold > 0){
    for( int i = 0; i < vibratortime ; i++ ){
      digitalWrite(vibratorPin, HIGH);
      delay(1000);
      digitalWrite(vibratorPin, LOW);
      delay(500);
    }
    cold = 0;
  }
  delay(1000);

  if (rain > 0){
    for (int iii = 0; iii<swingtime ; iii++){
      servo1.write(swingrange1);
      delay(300);
      servo1.write(swingrange2);
      delay(300);
      Serial.println("Shake Shake Shake");
    }
    servo1.write(servoPosition1);
    rain = 0;
  }
  delay(1000);
  if ( sun > 0){
    for (int iiii = 0; iiii< shaketime ; iiii++){
      servo2.write(shakerange1);
      servo3.write(tailrange1);
      delay(300);
      servo2.write(shakerange2);
      servo3.write(tailrange2);
      delay(300);
      Serial.println("Happy!");
    }
    servo2.write(servoPosition2);
    servo3.write(servoPosition3);
    sun = 0;
  }
  delay(1000);
}

int setPosition1( String command ){

  Serial.println( "setSpeed1: " + command );

  int value1 = command.toInt();
  if( value1 < 0 ) return -1;
  if( value1 > 180 ) return -1;

  servoPosition1 = constrain( value1, 0, 180 );
  servo1.write( servoPosition1 );
  return 1;
}

int setPosition2( String command ){

  Serial.println( "setSpeed2: " + command );

  int value2 = command.toInt();
  if( value2 < 0 ) return -1;
  if( value2 > 180 ) return -1;

  servoPosition2 = constrain( value2, 0, 180 );
  servo2.write( servoPosition2 );
  return 1;
}

int set_v_speed( String command ){
  int value3 = command.toInt();
  if( value3 < 0 ) return -1;
  if( value3 > 255 ) return -1;

  vibratorspeed = constrain( value3, 0, 255 );
  analogWrite(vibratorPin, vibratorspeed);
  return 1;
}

int set_v_time( String command ){
  int value4 = command.toInt();
  if( value4 < 0 ) return -1;
  if( value4 > 10 ) return -1;
  vibratortime = constrain( value4, 0, 10 );
  return 1;
}

int setrain( String command ){
  int value6 = command.toInt();
  if( value6 < 0 ) return -1;
  rain = value6;
  return 1;
}

int setsun( String command ){
  int value7 = command.toInt();
  if( value7 < 0 ) return -1;
  sun = value7;
  return 1;
  }

int setcold( String command ){
  int value8 = command.toInt();
  if( value8 < 0 ) return -1;
  cold = value8;
  return 1;
  }
Click to Expand

Content Rating

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

0