help with code

Hi all

i have some code that while it does work, by searching a string value on an XML feed in an if statement the tag search displays the correct string but not for the data part, can someone take a look please and tell me what iam doing wrong

THis is the part that i am having an issue with, the second IF statement won’t display the dataStr no matter what, but if i remove the IF statement the dataStr will print

  if (matchTag("<eventState>")){
      Serial.print(", Event State: ");
        if (matchData("inactive"))
          Serial.print(dataStr);

Below is the full code, if someone can help me out that would be great

THanks

#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
#include <Wire.h>
#include <string.h>
#include <SoftwareSerial.h>

// Define Constants
// Max string length may have to be adjusted depending on data to be extracted
#define MAX_STRING_LEN  30

// Setup vars
char tagStr[MAX_STRING_LEN] = "";
char dataStr[MAX_STRING_LEN] = "";
char tmpStr[MAX_STRING_LEN] = "";
char endTag[3] = {'<', '/', '\0'};
int len;

// Flags to differentiate XML tags from document elements (ie. data)
boolean tagFlag = false;
boolean dataFlag = false;

// Ethernet vars
byte ip[] = { 192,168,1,10 }; 
byte server[] = {192,168,1,64}; //hikvision camera
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xE3, 0x0F };



// Start ethernet client
EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Serial.println("Hikvision IP Interface");
  Serial.println("V1.0 -- 2017");
  Ethernet.begin(mac, ip);
  delay(1000);

  if (client.connect(server,80)) {
    Serial.println("Communication with Camera Started");
     client.print("GET /ISAPI/Event/notification/alertStream");
    client.println(" HTTP/1.1");
    client.println("Host: http://192.168.1.64");
    client.println("Authorization: Basic ********************");
    client.println(); 
    Serial.println("Connected to Camera Stream");    
    client.println();
    delay(2000);
  } else {
    Serial.println("connection failed");
  }  
}

void loop() {

  // Read serial data in from cam:
  while (client.available()) {
    serialEvent(); 
  }

  if (!client.connected()) {
    //Serial.println();
    //Serial.println("Disconnected");
    client.stop();

    // Time until next update
    //Serial.println("Waiting");
    for (int t = 1; t <= 15; t++) {
      delay(2000); // 1 minute
    }

    if (client.connect(server,80)) {
      //Serial.println("Reconnected");
      client.println("Connected to Camera");    
      client.println();
      delay(2000);
    } else {
      Serial.println("Reconnect failed");
    }       
  }
}

// Process each char from hikvision camera stream
void serialEvent() {

   // Read a char
      char inChar = client.read();
   //Serial.print(".");
  
   if (inChar == '<') {
      addChar(inChar, tmpStr);
      tagFlag = true;
      dataFlag = false;

   } else if (inChar == '>') {
      addChar(inChar, tmpStr);

      if (tagFlag) {      
         strncpy(tagStr, tmpStr, strlen(tmpStr)+1);
      }

      // Clear tmp
      clearStr(tmpStr);

      tagFlag = false;
      dataFlag = true;      
      
   } else if (inChar != 10) {
      if (tagFlag) {
         // Add tag char to string
         addChar(inChar, tmpStr);

         // Check for </XML> end tag, ignore it
         if ( tagFlag && strcmp(tmpStr, endTag) == 0 ) {
            clearStr(tmpStr);
            tagFlag = false;
            dataFlag = false;
         }
      }
      
      if (dataFlag) {
         // Add data char to string
         addChar(inChar, dataStr);
      }
   }  
  
   // If a LF, process the line
   if (inChar == 10 ) {

/*
      Serial.print("tagStr: ");
      Serial.println(tagStr);
      Serial.print("dataStr: ");
      Serial.println(dataStr);
*/

      // Find specific tags and print data 
      if (matchTag("<eventType>")) {       
            Serial.print("Event Type: ");
         Serial.print(dataStr);
      
      }
      if (matchTag("<eventState>")){
      Serial.print(", Event State: ");
        if (matchData("inactive"))
          Serial.print(dataStr);
      }
      if (matchTag("<eventDescription>")){
            Serial.print(", Description = ");
         Serial.print(dataStr);
         Serial.println("");
      }

      // Clear all strings
      clearStr(tmpStr);
      clearStr(tagStr);
      clearStr(dataStr);

      // Clear Flags
      tagFlag = false;
      dataFlag = false;
   }
}

/////////////////////
// Other Functions //
/////////////////////

// Function to clear a string
void clearStr (char* str) {
   int len = strlen(str);
   for (int c = 0; c < len; c++) {
      str[c] = 0;
   }
}


//Function to add a char to a string and check its length
void addChar (char ch, char* str) {
   char *tagMsg  = "<TRUNCATED_TAG>";
   char *dataMsg = "-TRUNCATED_DATA-";

   // Check the max size of the string to make sure it doesn't grow too
   // big.  If string is beyond MAX_STRING_LEN assume it is unimportant
   // and replace it with a warning message.
   if (strlen(str) > MAX_STRING_LEN - 2) {
      if (tagFlag) {
         clearStr(tagStr);
         strcpy(tagStr,tagMsg);
      }
      if (dataFlag) {
         clearStr(dataStr);
         strcpy(dataStr,dataMsg);
      }

      // Clear the temp buffer and flags to stop current processing 
      clearStr(tmpStr);
      tagFlag = false;
      dataFlag = false;

   } else {
      // Add char to string
      str[strlen(str)] = ch;
   }
}

// Function to check the current tag for a specific string
boolean matchTag (char* searchTag) {
   if ( strcmp(tagStr, searchTag) == 0 ) {
      return true;
   } else {
      return false;
   }
}

boolean matchData (char searchData) {
  if (strcmp (dataStr, searchData) == 0) {
    return true;
  }else {
    return false;
  }
}

Perhaps this:

boolean matchData (char searchData) {

should be this:

boolean matchData (char* searchData) {

similar to your matchTag function.

Hi Thanks for the response, i have changed that but still does not work

// Add char to string
      str[strlen(str)] = ch;

I'm concerned about some of your string handling, particularly WRT terminators.

still does not work

I hate that phrase

If you still think the problem is in the second IF statement then prove it to yourself by putting more debug serial prints inside matchData or before calling it. Make sure the data getting to matchData is good.

In general, if you need to parse XML then get an XML parser. That is - google "arduino xml parser library". Someone will certainly have written one that works - it's a far better option than trying to write your own.

Ditto parsing JSON.

Thanks for the reply guys, still having problems. if the second if statement is removed then the dataStr prints out, but I want to compare whats in the dataStr before it prints, I have tried the tmpStr also

Incentive:
Thanks for the reply guys, still having problems. if the second if statement is removed then the dataStr prints out, but I want to compare whats in the dataStr before it prints, I have tried the tmpStr also

You still insist on going this alone instead of using a debugged library? ? ?

For more info on parsing XML, read this post on using reegular expressions to parse XML.