I can't seem to compare what I believe are strings.
My function looks like the following:
void handleMessage(AdafruitIO_Data *data) {
Serial.printf("\nreceived <- %s", data->value());
if (data->value() == "OPEN") {
Serial.printf("\nIt worked!");
}
}
When printed, data->value()
prints what I expect it to, but when I compare it like this data->value() == "OPEN"
it doesn't work. What is the right way to do this, and why isn't the above working?
I have attempted to use strcmp()
as suggested by How do I properly compare strings?
void handleMessage(AdafruitIO_Data *data) {
Serial.printf("\nreceived <- %s", data->value());
if (strcmp(data->value() == "OPEN")) {
Serial.printf("\nIt worked!");
}
}
However I get:
FileName:48: error: cannot convert 'bool' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
It isn't a boolean when it is printed. From my example it prints: received <- OPEN
When printed, data->value() prints what I expect it to, but when I compare it like this data->value() == "OPEN" it doesn't work. What is the right way to do this, and why isn't the above working?
strcmp takes two arguments both are char * (pointer to a char), you are supplying it with a boolean expression that boils down to a bool
Reference for strcmp can be found here
Assuming that AdafruitIO_Data
is as defined here and that you've included string.h
void handleMessage(AdafruitIO_Data *data) {
Serial.printf("\nreceived <- %s", data->value());
if (!strcmp(data->value(), "OPEN")) {
Serial.printf("\nIt worked!");
}
}