FirebaseClient - Converting changed values to proper types question

I'm using the FirebaseClient with ESP32 trying to listen for database changes. I have everything working and outputting to the serial monitor so far but I'm getting hung up trying to convert the data into variables I can use in my program.

In my callback function, I have the code below. If the changed value is an integer then I want to figure out which one it is and then assign that to its corresponding variable in my program. For my test, I'm trying to compare the key of the changed data and if it matches the one I wanted, convert the data to integer format. I thought this would work but I know I'm missing something. The compare to "/ms" never works. I know the c_str() is /ms. I tried making a const char* test = "/ms" to use in the compare but that didn't work either.

I'm trying to learn my way through this library and how to use this but am stuck at the moment. Any pointers or help would be appreciated!

Serial Monitor (when I change the data in Firebase):

task: streamTask
event: put
path: /ms
etag:
data: 40000
type: 1
RTDB.dataPath(): /ms, Value: 40000
Updated value is: 0

Code:

if (RTDB.type() == 1){
        Serial.print("RTDB.dataPath(): ");
        Serial.print(RTDB.dataPath().c_str());
        Serial.print(", Value: ");
        Serial.println(RTDB.data().c_str());
        if (RTDB.dataPath().c_str() == "/ms") {tempInt = RTDB.to<int>();}
        Serial.print("Updated value is: ");
        Serial.println(tempInt);

We need to see ALL the code in code tags, a photo of a hand-drawn diagram of your actual wiring. If there is serial output, post it in code tags as well.

dataPath returns a String. String::c_str returns a const char *

But String also implements

  bool equals(const String &s) const;
  bool equals(const char *cstr) const;
  bool operator==(const String &rhs) const {
    return equals(rhs);
  }
  bool operator==(const char *cstr) const {
    return equals(cstr);
  }

While you can compare two String objects with the equals function, you can also use the == operator. And you can also compare a String with the C-string at char *. So

  if (RTDB.dataPath() == "/ms")

should work, comparing the content of the String with the the C-string. What you're doing before

is comparing two pointers for equality: do they contain the same address. ESP32 implements SSO: Small String Optimization. A short string like "/ms" is inside the 16-byte String object. So you are comparing the address of some object on the heap or stack with a the hard-coded string constant somewhere. They will not be equal.

Instead of == for two C-string pointers, use a function like strcmp, which is what String::equals is doing behind the scenes

bool String::equals(const char *cstr) const {
  if (len() == 0) {
    return (cstr == NULL || *cstr == 0);
  }
  if (cstr == NULL) {
    return buffer()[0] == 0;
  }
  return strcmp(buffer(), cstr) == 0;
}

print also knows how to print a String ➜ so long story short, you don't need to go to c-string

mind the scope of your variables

if (RTDB.type() == 1){
  Serial.print("RTDB.dataPath(): ");
  Serial.print(RTDB.dataPath());
  Serial.print(", Value: ");
  Serial.println(RTDB.data());

  if (RTDB.dataPath() == "/ms") {
    tempInt = RTDB.to<int>(); // YOU HAD A WRONG CLOSING BRACKET HERE
    Serial.print("Updated value is: ");
    Serial.println(tempInt);
  }
  // note that here tempInt does not exist anymore
  ...
}

Thank you for the detailed explanations. That was very helpful. I got it working by dropping the c_str(). I've seen that in examples and did it without thinking about what exactly it was doing! Little by little this starts to make more sense and my project gets farther and farther.