0

Value of pointer in IF statement (subscribing)
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Value of pointer in IF statement (subscribing)

by UberPsych on Thu Dec 28, 2017 9:20 am

I am subscribing to a feed called button04. The pointer value when printed is either ON or OFF.
I tried the IF ELSE statement I get an error.

if (*data->value() == "ON") {
// Do something
}
else
{
// OFF
}

I suspect that that I need to copy the pointer value into a char[3] and then compare but all my attempts have failed.

UberPsych
 
Posts: 4
Joined: Sun Aug 26, 2012 10:52 pm

Re: Value of pointer in IF statement (subscribing)

by abachman on Fri Dec 29, 2017 3:14 pm

Hey UberPsych,


You could use the toString method on the data object to compare. In the case of your example code, assuming it's inside an Adafruit IO message handler, that would look like this:
Code: Select all | TOGGLE FULL SIZE
void handleMessage(AdafruitIO_Data *data) {
  Serial.print("GOT DATA ");
  Serial.println(data->value());

  if (data->toString() == "ON") {
    doTheThing();
  }
}

The Adafruit IO Arduino library has a handful of data conversion methods[/url] that are useful in cases like this: https://github.com/adafruit/Adafruit_IO_Arduino/blob/master/src/AdafruitIO_Data.h#L47-L70


- adam

abachman
 
Posts: 131
Joined: Mon Feb 01, 2010 12:48 pm

Re: Value of pointer in IF statement (subscribing)

by UberPsych on Fri Dec 29, 2017 11:01 pm

Thanks Adam
That fixed my problem.

UberPsych
 
Posts: 4
Joined: Sun Aug 26, 2012 10:52 pm

Please be positive and constructive with your questions and comments.