Data returned from Particle.subscribe

I am having trouble analysing the data returned from an event raised by another particle. I have Particle.subscribe(“Door”, DoorHandler, MY_DEVICES); and void DoorHandler(const char *event, const char *data). The other particle publishes a Door event, which is either “Open” or “Closed”. All works well and I can print the data to the serial screen. However, I am having some trouble referencing the data, I think it is a different data type to string. For example, I cannot use if (data == “Open”), and I have also tried if data.compareTo(“Open”) and neither works. Could someone please point me in the right direction?

data is a const char * not a String so you need to use C-style comparisons. For example:

if (strcmp(data, "Door") == 0)

Note that strcmp returns 0 if equal, so it’s kind of backwards (it returns -1 if less than or +1 if greater than). And it’s case-sensitive.


Thanks that worked!

1 Like