Issue with String.toLowerCase() on Particle

Hello, all!

I have recently gotten my Particle Photon, and as a simple first project, I’m building an ambient light weather indicator with a few RGB LEDs, frosted acrylic, and a Photon. Physically, it’s all good. No issues there. Program-wise, I have the Photon partnered with IFTTT to poll the weather and send a quick blurb of the weather to the a SparkFunction. The spark function searches the string for certain key weather words and changes the LEDs according. Below, you’ll see my code.

int red = 3;
int green = 2;
int blue = 1;

void setup() {

  Serial.begin(9600);

  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);

  Spark.function("weather", weatherLEDs);
  Serial.print("Hello World\n");
}

void loop() {}

int weatherLEDs(String weather){

  weather.toLowerCase();

  Serial.print(weather);


  if ((weather.indexOf("rain")>0) | (weather.indexOf("rainy")>0) | (weather.indexOf("showers")>0)) {

    digitalWrite(blue, HIGH);
    return 1;

  }

  if ((weather.indexOf("sunny")>0)) {

    digitalWrite(red, HIGH);
    digitalWrite(green, HIGH);
    return 1;

  }

  if ((weather.indexOf("storms")>0) | (weather.indexOf("thunderstorms")>0) | (weather.indexOf("lightning")>0)) {

    digitalWrite(blue, HIGH);
    digitalWrite(red, HIGH);
    return 1;

  }

  else {return 0;}

}

The issue now is that the weather.toLowerCase() returns the exact same string it was handed, often with uppercase letters. I’ve been running a serial monitor and using curl along with phrases which should be giving me corresponding colors (e.g. “Rain” or “Scattered Thunderstorms”), but the output string always has upper case letters. I’ve checked the Arduino reference (it seems to be nearly universally applicable to .ino files, but if I should be using some other reference, please let me know) on toLowerCase() and my usage above is correct, but the results just aren’t there. Any ideas or insight? I’m moving over from mainly arduino tinkering, so I may have made some silly error for Particle about which I am unaware.

Have you updated to 0.4.7 firmware?

Particle Device OS Updates Thread - #15 by mdma

https://github.com/spark/firmware/issues/665

@dchesson93,

I did something very similar on a project I finished a while back, but used C strings instead:

void gotWeather(const char *event, const char *data)  //  Clear~74.4~86%~NNW~0~70~  <<<<<<< return prototype
{
  char dataBuffer[125] = "";
  sprintf(dataBuffer, "%s", data);
  char* ptr = strtok(dataBuffer, "\"~");
  sprintf(weatherCondition, "%s", ptr);
  int i = 0;
  while(weatherCondition[i])
  {
    weatherCondition[i] = tolower(weatherCondition[i]);
    i++;
  }
  extTemp = atoi(strtok(NULL, "~"));
  celciusTemp = int((double(extTemp - 32) * (5.0/9.0)));
  if (strstr(weatherCondition, "snow") || strstr(weatherCondition, "ice") || strstr(weatherCondition, "freezing") || strstr(weatherCondition, "sleet"))
  {
    weatherColor = allBlue;
    weather_State = SNOW;
  }
  else if (strstr(weatherCondition, "drizzle") || strstr(weatherCondition, "rain"))
  {
    weatherColor = allBlue;
    weather_State = RAIN;
  }
  else if (strstr(weatherCondition, "hail") || strstr(weatherCondition, "thunderstorm"))
  {
    weatherColor = allBlue;
    weather_State = LIGHTNING;
  }
  else if (strstr(weatherCondition, "overcast"))
  {
    weatherColor = dullGray;
    weather_State = OVERCAST;
  }
  else if (strstr(weatherCondition, "sun") || strstr(weatherCondition, "clear"))
  {
    weatherColor = yellowSun;
    weather_State = SUNSHINE;
  }
  else
  {
    weatherColor = allBlue;
    weather_State = PARTLY_CLOUDY;
  }
}

a workaround that may work for you.

you could also just use toCharArray() to convert the String to a string.

char myCommand[125] = "";
data.toCharArray(myCommand,125);

Wow, that was quick! I’ll give the firmware update a shot and if it doesn’t fix the problem, I’ll try the C work around. I’ll report back. Thanks, guys!

The firmware update did wonders! Works like a charm now. Thanks a million, guys!

1 Like