Using event data for mathematical comparison


#1

I am working on a project for school and simply want to use the “data” value from a published event for mathematical comparison. I have subscribed to the event, but can not figure out what to do to the code to be able to use that value.


#2

To help you further, you may want to tell us:

Where are you trying to do the calculation? On the particle device? Where does the data come from… do you have a URL or something? If not, what format is the data and what is the datatype? What code have you tried already, what were the expected results and what were the actual results?


#3

Yes I am attempting to do the calculation on the particle device. The data comes from the particle event. Essentially I would like to take the number from the “data” column of the event and compare it to a number to enable an output. For example “if the value is greater than 70, turn on an LED”


#4

This compiles… not sure if it works with your data. I had to search this forum on how to convert the const char* data into a float variable. This is basically taken directly from the Particle.subscribe() documentation and example but substituted for your event name.

float myValueFromPubSub = 0;
float myValueToCompare = 70.0;
int LED = D7;                         // Built-in LED is connected to D7
int i = 0;

void myHandler(const char *event, const char *data)
{
  i++;
  Serial.print(i);
  Serial.print(event);
  Serial.print(", data: ");
  if (data) {
    Serial.println(data);
    myValueFromPubSub = atof(data);
    if (myValueFromPubSub > myValueToCompare) {
      digitalWrite(LED, HIGH);          // sets the LED on
    } else {
      digitalWrite(LED, LOW);          // sets the LED off
    }
  }
  else {
    Serial.println("NULL");
  }
}

void setup()
{
  pinMode(LED, OUTPUT);               // sets pin as output
  Particle.subscribe("temperature_swanger", myHandler, MY_DEVICES);
  Serial.begin(9600);
}

void loop() 
{
}

#5

Where are those events coming from? Is it the same device?


#6

The events are coming from an IFTTT applet.


#7

In that case, @ninjatill’s suggestion is the way to go.