Read json from webhook

Hi guys. First of all I want to apologize for my English, I am a native speaker of Spanish.

Days ago I’m trying to read the data I get from a webhook that connects to a Firebase database. I just want to read two data sent in a Firebase json (temMax and tempMin).

This is my code to test that function (provided by the webhook that I created):

void setup() {
  Particle.subscribe("hook-response/seteoTemperatura", myHandler, MY_DEVICES);
}

void loop() {
  String data = String(10);
  
  Particle.publish("seteoTemperatura", data, PRIVATE);
  delay(6000);
}
      

void myHandler(const char *event, const char *data) {
  // In this function I do not know what to do.
}

The data contained in the json file are just these:
{"tempMax":15,"tempMin":10}

In the console I see this:

If you observe, Particle.publish (“set Temperature”, data, PRIVATE); print the value 10 on the console. If only I could read the other value (15) my problem would be serious, but I do not know what to do anymore.

I tried to follow the tutorial de de @rickkas7 but I was not lucky either … obviously the problem is me hahaha:
https://github.com/rickkas7/firebase_tutorial

I’m sorry for my bad English and for the duration of the publication!!.

I appreciate any help!! Thank you very much.
Regards!! :grin:

There are many ways to do this. One way is to use a JSON parser library; I like JsonParserGeneratorRK.

Add a global variable:

JsonParserStatic<512, 50> jsonParser;

Add this in myHandler:

jsonParser.addString(data);

if (jsonParser.parse()) {
    int tempMax;
    jsonParser.getOuterValueByKey("tempMax", tempMax);

    int tempMin;
    jsonParser.getOuterValueByKey("tempMin", tempMin);

    // Put code to do something with tempMin and tempMax here
}
1 Like

Thank you very much @rickkas7, I appreciate very much you have taken the trouble to answer me.

After your answer, my code remained this way

#include <JsonParserGeneratorRK.h>
JsonParserStatic<512, 50> jsonParser;

void loop() {  
  String data = String(10);  
  Particle.publish("setTemperature", data, PRIVATE);  
  delay(6000);
}
        

void setup() {
  Particle.subscribe("hook-response/setTemperature", myHandler, MY_DEVICES);
}

void myHandler(const char *event, const char *data) {
    jsonParser.addString(data);

    if (jsonParser.parse()) {
        String tempMax;
        jsonParser.getOuterValueByKey("tempMax", tempMax);
    
        String tempMin;
        jsonParser.getOuterValueByKey("tempMin", tempMin);
    
        Particle.publish("max", tempMax, PRIVATE);
        Particle.publish("min", tempMin, PRIVATE);
    }
}

In the console I see that the values are published null:
image

Will my error be found in the webhook configuration?

The weird thing is that the data is shown:

I feel like I’m asking stupid questions and I’m very embarrassed, but I reached the limit that I had no choice but to go and bother them with questions :flushed:

This should be

int tempMax;

The temperature is an integer, not a String. Same for tempMin.

JSON won't automatically convert between String and int. To publish it, use:

Particle.publish("max", String(tempMax), PRIVATE);
1 Like

I do not know what I thought when I did that. What I could not solve in days you did it in 5 minutes haha.

Thank you very much!! Now I can continue.
image

I am doing a project for a beer fermentation chamber, when I finish it I would like to share it with you.

Thank you for having so much patience @rickkas7
Regards!

1 Like