Help with subscribe - reading .publish from other photon

I am trying to grab a part of data from my other photon to display on a led connected to another photon.

This code works

void myHandler(const char *event, const char *data)
{
i++;
Serial.print(i);
Serial.print(event);
Serial.print(", data: ");
if (data)
Serial.println(data);
else
Serial.println(“NULL”);
}

void setup()
{
Particle.subscribe(“temperature”, myHandler, MY_DEVICES);
Serial.begin(9600);
}

And my data from the other photon looks like this:

{data:"{“humidity”: 25.687870, “temp”: 20.197059, “amb_temp”: -2.600000}"

How can i grab only the “amb_temp” part of the data.

Create a char buffer of adequate size to fit the data
copy data to a char array
parse the data using strtok()

examples

or (since your return is a JSON object) you can look into the JSON library on Build

1 Like

Thanks!. I’ll try that out tomorrow

I have a project that uses Neopixels to display temperature.

Mine is a bit more involved at this point but broken down it is…

void setup(){
    Particle.subscribe("TempCpub", myHandler, MY_DEVICES);
}

Then the data string in…

void myHandler(const char *event, const char *data)
{
    tmp = atof(data);  // convert the incoming string literal to float and put the value in the global variable
    gotMessage = true; // a global semaphore to set true when you get a new light value from master Particle.subscribe() function
    Particle.publish("GotIt", String(tmp));

}

It has it’s own Particle.publish to verify it has grabbed the data

Then the loop itself calling the logic for the Neopixels and basing the variable published on our string of data in the myHandler function.

void loop() 
{
  static int messageTimer = 0;
  if(gotMessage)
  {
    setLeds(tmp);
    gotMessage = false;
    messageTimer = millis();  // each time the message is received, reset the timeout clock to millis()
  }
  if(millis() - messageTimer > TIMEOUT_PERIOD)  // check to see if your timeout has expired
  {
    chase(strip.Color(255, 0, 0)); // Red
  }
}
1 Like

thanks for that too. I’m here at work now so Ill get to tinkering and report back.

I cant seem to get it, when i do this:

Serial.print(event);
Serial.print(", data: ");
Serial.println(str);

I get this

data:{“humidity”: 24.993797, “temp”: 20.458456, “amb_temp”: -4.000000}

I still cant figure out how to get the “amb_temp” part only

I have this code so far:

void myHandler(const char *event, const char *datascrt)
{

String str = String(datascrt);
char strBuffer[256] = “”;
str.toCharArray(strBuffer, 256);
int forecastday1 = atoi(strtok(strBuffer,","));
int maxtempday1 = atoi(strtok(NULL, “,”));

Serial.print(event);
Serial.print(", data: ");

Serial.println(str);
Serial.println(forecastday1);
Serial.println(maxtempday1);

}

it just spits out zeros. Any help is appreciated

your string above may also have spaces following the “;” character ( i had to escape the string for my example…

char myData[] = "data:{\"humidity\": 24.993797, \"temp\": 20.458456, \"amb_temp\": -4.000000}";


void setup() 
{
  Serial.begin(9600);
  char* ptr = strtok(myData, ": ");
  int idx = 0;
  while(ptr)
  {
    Serial.print(idx++);
    Serial.print(": ");
    Serial.println(atof(ptr));
    ptr = strtok(NULL, ": ");  // note a space inside the quotes, too
  }
}

void loop(){}

It would be easier to parse if you just send the 3 numbers separated by commas, if you have control over that. If the data were like this,

“24.993797,20.458456, -4.000000”

Then, you could separate the strings with strtok,

void myHandler(const char *event, const char *data) {
    char *args = (char*)data;
    char *first = strtok(args, ",");
    char *second = strtok(NULL, ",");
    char *third = strtok(NULL, ",");
    float ambientTemp = atof(third);
}
1 Like

ill try this out thanks.