Comparing Strings and/or char arrays?? Please Help

Hi,

I am reading XML weather data off of Google's website: Google

My code is working and TextFinder returns a string I named "cond" (which is the forecast condition)

I need my code to activate a motor if "cond" is one of 5 phrases "Rain", "Showers", "Chance of Rain", "Chance of Showers", or "Thunderstorms"

I don't really understands how strings and char arrays work yet.

Any suggestions???

Here is the part of the code I am discussing:

if(finder.find("<condition data")){
      finder.getString("=", "/", cond, 30);
      Serial.print("Forecast is ");
      Serial.println(cond);

      lcd.setCursor(0,2);
      lcd.print(cond);
      delay(1);

      if (cond == "Chance of Showers" || "Chance of Rain" || "Showers" || "Rain" || "Thunderstorms" ){// THIS PART NEEDS HELP
        Serial.println("yes");
      }


    }

Here is the entire code for context:

//Sources
//Arduino Cookbook 15.5, 
//Google weather site http://www.google.com/ig/api?weather=Portland,OR
//wwww.bildr.org/?s=ethernet
//Arduino Forum, Topic: Getting Weather Data from XML with Ehernet Shield, zoomkat posted code from "someone else" based on SimpleClientGoogleWeatherDHCP
//DO NOT USE PIN 11 on ETHERNET SHEILD

#include <Ethernet.h>
#include <TextFinder.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(0);

byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0D, 0x14, 0xBF }; //Ethernet Sheild MAC address
byte server[] = { 
  209,85,229,104 }; //Google XML weather page 
EthernetClient client;
TextFinder finder(client);

int LedColdPin = 9;
int LedHotPin = 8;
int LedMedPin = 7;
int testcold = 0;
int testhot = 0;

const byte switchPin = 6; //digital pin
const byte RainMotorPin1 = 3; //digital pin
const byte RainMotorPin2 = 4; //digital pin
const byte SnowMotorPin3 = 16;
const byte SnowMotorPin4 = 17;
const byte enablePinRain = 5; //digital pin, powers that side of H bridge
const byte enablePinSnow = 15; //(on A1),digital pin, powers that side of H bridge

byte state = 0;

char cond[30]; // string for incoming serial data

void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac); 
  lcd.begin(20,4); 

  pinMode(LedColdPin,OUTPUT);
  pinMode(LedMedPin, OUTPUT);
  pinMode(LedHotPin,OUTPUT); 

  pinMode(switchPin, INPUT);

  pinMode(RainMotorPin1, OUTPUT);
  pinMode(RainMotorPin2, OUTPUT);
  pinMode(SnowMotorPin3, OUTPUT);
  pinMode(SnowMotorPin4, OUTPUT);

  pinMode(enablePinRain, OUTPUT);
  pinMode(enablePinSnow, OUTPUT);

}//void setup brace

void loop(){

  if (client.connect(server,80)) {
    client.println("GET /ig/api?weather=Portland,OR HTTP/1.0");
    client.println();
  }

  else{
    Serial.println("connection failed");
  }

  if (client.connected()){

    if(finder.find("<low data=")){
      int t_low = finder.getValue();
      Serial.print("Low Temp is ");
      Serial.println(t_low);

      lcd.setCursor(0,1);
      lcd.print("Low: ");
      lcd.setCursor(5,1);
      lcd.print(t_low);
      delay(1);
      lcd.setCursor(8,1);
      lcd.print((char)223);
      delay(1);


      if(t_low <= 45){
        digitalWrite(LedColdPin, HIGH);
        testcold=1;
      }
    }

    if(finder.find("<high data=")){
      int t_high = finder.getValue();
      Serial.print("High Temp is ");
      Serial.println(t_high);

      lcd.setCursor(0,0);
      lcd.print("High: ");
      lcd.setCursor(6,0);
      lcd.print(t_high);
      delay(1);
      lcd.setCursor(9,0);
      lcd.print((char)223);
      delay(1);

      if(t_high >= 75){
        digitalWrite(LedHotPin, HIGH);
        testhot=1;
      }
    }

    if(testcold == 0 && testhot == 0){
      digitalWrite(LedMedPin, HIGH);
    }

    if(finder.find("<condition data")){
      finder.getString("=", "/", cond, 30);
      Serial.print("Forecast is ");
      Serial.println(cond);

      lcd.setCursor(0,2);
      lcd.print(cond);
      delay(1);

      if (cond == "Chance of Showers" || "Chance of Rain" || "Showers" || "Rain" || "Thunderstorms" ){// THIS PART NEEDS HELP
        Serial.println("yes");
      }


    }//if finder find condition data brace

    else{
      Serial.print("Could not find condition field");
    }

  }//if client connect brace

    else{
    Serial.println("Disconnected");
  }

  client.stop();
  client.flush();
  delay(6000);  

}//void loop brace

Firstly, The normal syntax for such a comparison would be

if ((x == 1) || (x == 3) || (x == 4)) etc, you get the picture here.

Secondly, if cond is a nul terminated string, it needs to be big enough for the longest string plus the nul ('\0') character at the end.

Thridly, you need to call the strcmp function to compare the string to your "Template" string and check the ressult that comes back - how you do this is an exercise for you to look up :slight_smile:

Thank you!

So I looked up strcmp and found an example. I think I'm closer but it still doesn't work. I defined cond as an array 30 characters long, this should be longer than any weather forecast that could be read off the website.

Then I made Templates char ChanceShowers[] = "Chance of Showers";

What am I doing wrong now?

Here are the parts of the code I changed:

char cond[30]; // string for incoming serial data
char ChanceShowers[] = "Chance of Showers";
char ChanceRain[] = "Chance of Rain";
char Rain[] = "Rain";
char Showers[] = "Showers";
char Thunderstorm[] = "Thunderstorm";
.
.
.
if ((strcmp(ChanceShowers,cond) == 0) || (strcmp(ChanceRain,cond) == 0) || (strcmp(Showers,cond) == 0) || (strcmp(Rain,cond) == 0) || (strcmp(Thunderstorm,cond) == 0)){
     
        Serial.println("yes");
      }

Here is the whole code for reference:

//Sources
//Arduino Cookbook 15.5, 
//Google weather site http://www.google.com/ig/api?weather=Portland,OR
//wwww.bildr.org/?s=ethernet
//Arduino Forum, Topic: Getting Weather Data from XML with Ehernet Shield, zoomkat posted code from "someone else" based on SimpleCli
ntGoogleWeatherDHCP
//DO NOT USE PIN 11 on ETHERNET SHEILD

#include <Ethernet.h>
#include <TextFinder.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(0);

byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0D, 0x14, 0xBF }; //Ethernet Sheild MAC address
byte server[] = { 
  209,85,229,104 }; //Google XML weather page 
EthernetClient client;
TextFinder finder(client);

int LedColdPin = 9;
int LedHotPin = 8;
int LedMedPin = 7;
int testcold = 0;
int testhot = 0;


const byte switchPin = 6; //digital pin
const byte RainMotorPin1 = 3; //digital pin
const byte RainMotorPin2 = 4; //digital pin
const byte SnowMotorPin3 = 16;
const byte SnowMotorPin4 = 17;
const byte enablePinRain = 5; //digital pin, powers that side of H bridge
const byte enablePinSnow = 15; //(on A1),digital pin, powers that side of H bridge

byte state = 0;

char cond[30]; // string for incoming serial data
char ChanceShowers[] = "Chance of Showers";
char ChanceRain[] = "Chance of Rain";
char Rain[] = "Rain";
char Showers[] = "Showers";
char Thunderstorm[] = "Thunderstorm";


void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac); 
  lcd.begin(20,4); 

  pinMode(LedColdPin,OUTPUT);
  pinMode(LedMedPin, OUTPUT);
  pinMode(LedHotPin,OUTPUT); 

  pinMode(switchPin, INPUT);

  pinMode(RainMotorPin1, OUTPUT);
  pinMode(RainMotorPin2, OUTPUT);
  pinMode(SnowMotorPin3, OUTPUT);
  pinMode(SnowMotorPin4, OUTPUT);

  pinMode(enablePinRain, OUTPUT);
  pinMode(enablePinSnow, OUTPUT);

}//void setup brace

void loop(){

  if (client.connect(server,80)) {
    client.println("GET /ig/api?weather=Portland,OR HTTP/1.0");
    client.println();
  }

  else{
    Serial.println("connection failed");
  }

  if (client.connected()){

    if(finder.find("<low data=")){
      int t_low = finder.getValue();
      Serial.print("Low Temp is ");
      Serial.println(t_low);

      lcd.setCursor(0,1);
      lcd.print("Low: ");
      lcd.setCursor(5,1);
      lcd.print(t_low);
      delay(1);
      lcd.setCursor(8,1);
      lcd.print((char)223);
      delay(1);


      if(t_low <= 45){
        digitalWrite(LedColdPin, HIGH);
        testcold=1;
      }
    }

    if(finder.find("<high data=")){
      int t_high = finder.getValue();
      Serial.print("High Temp is ");
      Serial.println(t_high);

      lcd.setCursor(0,0);
      lcd.print("High: ");
      lcd.setCursor(6,0);
      lcd.print(t_high);
      delay(1);
      lcd.setCursor(9,0);
      lcd.print((char)223);
      delay(1);

      if(t_high >= 75){
        digitalWrite(LedHotPin, HIGH);
        testhot=1;
      }
    }

    if(testcold == 0 && testhot == 0){
      digitalWrite(LedMedPin, HIGH);
    }

    if(finder.find("<condition data")){
      finder.getString("=", "/", cond, 30);
      Serial.print("Forecast is ");
      Serial.println(cond);

      lcd.setCursor(0,2);
      lcd.print(cond);
      delay(1);

      if ((strcmp(ChanceShowers,cond) == 0) || (strcmp(ChanceRain,cond) == 0) || (strcmp(Showers,cond) == 0) || (strcmp(Rain,cond) == 0) || (strcmp(Thunderstorm,cond) == 0)){

        Serial.println("yes");
      }


    }//if finder find condition data brace

    else{
      Serial.print("Could not find condition field");
    }

  }//if client connect brace

  else{
    Serial.println("Disconnected");
  }

  client.stop();
  client.flush();
  delay(6000);  

}//void loop brace

I think I'm closer but it still doesn't work

What does this mean? What does not work?

The program is returning char cond as "Chance of Showers" today and is printing it to the serial monitor. But it is not printing "yes" to the serial monitor to verify that my template char ChanceShowers[] is equal to char cond.

If I change the statement to (strcmp(ChanceShowers,cond) != 0) then it does print "yes". So that means that char ChanceShowers[] is not equal to char cond. (I think)

Could it have something to do with the fact that it prints the " " quotation marks around the char cond that it returns (for example it prints: "Chance of Showers") to the serial monitor? Why is char ChanceShowers[] not equal to char cond ????

Thanks for your help!

Blueorchid:
Could it have something to do with the fact that it prints the " " quotation marks ("Chance of Showers") to the serial monitor? Why is char ChanceShowers[] not equal to char cond ????

Very likely. That file has:

<condition data="Chance of Showers"/>

So the quotes appear to be there. And "Chance of Showers" is not the same as Chance of Showers

I don't know if this will help, but you might use regular expressions to parse each line.

Then when you get:

<condition data="xxxx"/>

You could get a match, and then check what the "xxxx" is.

The file does have

Is there a way around this?

I tried changing: char ChanceShowers[] = "Chance of Showers";
to: char ChanceShowers[] = ""Chance of Showers"";

it does not like this.

It looks like your Regular Expressions library will work, but I am a total newbie (I have never taken a programing class) and I'm not sure I understand programing enough to combine it with my code.

char ChanceShowers[] = "\"Chance of Showers\"";

It works!! Thank you so much!!! :smiley: