Call function in loop

Hi guys. This code reads the information received from a webhook.
I want to call the “myHandler” function every 6 seconds in a loop to read if there is updated data. At the moment for some reason the function “myHandler” after being called about 5 times stops and does not return to consult the data.

I am new in this language, I hope I am not asking a very silly question.

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

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

void loop() {  
  String data = String(10);  
  Particle.publish("setTemperature", data, PRIVATE);
  Particle.publish("max", String(tempMax), PRIVATE);
  Particle.publish("min", String(tempMin), PRIVATE);


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

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

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

take a look into Software Timers. You can have them fire at certain time intervals.

1 Like

Right now myHandler is the callback for the webhook subscription. Why do you want to call it every 6 seconds when it should only run in response to the webhook?

You may need to rethink what you are trying to achieve.


Hi @nrobinson2000 thanks for your response. The truth, I feel a little lost with the functioning of webhooks.

What happens is that when I update the data from Firebase, the webhook immediately updates perfectly showing in console the new values. But the “myHandler” function that is responsible for interpreting the Json is not recalled, this causes the variables “tempMax” and “tempMin” to obtain the values of the initial call and then when the webhook is updated, these variables do not make.

I hope you understand what I’m trying to explain, I apologize for my bad English.

Try printing out the raw data and event name strings to see whether your webhook-response is what you expect.
Also check the console, whether the subscribe filter matches what your response even is.
Post screenshots of the console event log plus the raw data.


Thanks @ScruffR , at this moment I am in my work. But when I get home I do those tests and tell them how it goes.

1 Like

Squanchy please do, i’m really interested myself. thanks for all the help here! appreciating a lot. i need lots of help with my site as there something isn’t well going out. thanks

1 Like

Hi @nrobinson2000 @ScruffR !

I did the following tests, I printed in the console the variables tempMax, tempMin, data and webhook with the data that come from Firebase.

This is printed when the particle photon is put into operation:

Then I update the data in Firebase and I see that the webhook brings the data correctly ({“tempMax”: 20, “tempMin”: 12}). But what really interests me is that the variables “max” and “min” update their new values. Currently it remains with the first value obtained.

I think now I could explain better the difficulty I’m having. I appreciate any ideas to solve it :slightly_smiling_face:

This would suggest the jsonParser has some issues validating the incoming data.

In order to locate issues with your code one of the steps you always should take first is to add as many Serial.print() statements as you can to see what your code is internally doing and where it’s failing to do what you want.

void myHandler(const char *event, const char *data) {
    bool b;

    Serial.printlnf("%s: %s", event, data);

    b = jsonParser.addString(data);
    Serial.printlnf("addString() returned %s", b ? "true" : "false");
    Serial.printlnf("Buffer contains '%s' (%d)", jsonParser.getBuffer(), jsonParser.getBufferLen()); 

    if (jsonParser.parse()) {        
        b = jsonParser.getOuterValueByKey("tempMax", tempMax);
        Serial.printlnf("getOuterValueByKey(%s, %d) returned %s", "tempMax", tempMax, b ? "true" : "false");
        b = jsonParser.getOuterValueByKey("tempMin", tempMin);
        Serial.printlnf("getOuterValueByKey(%s, %d) returned %s", "tempMin", tempMin, b ? "true" : "false");
      Serial.println("parse() failed");

But what I think is happening is that you always add data to the end of the JSON buffer, but keep parsing from the beginning which still starts with the old data.
Try adding a jsonParser.clear() call before adding data.

1 Like

@ScruffR you gave me a double solution!! The “myHandler” method worked perfectly, but I also tried using jsonParser.clear () before calling data and it also worked perfect!

Thank you very much!! :grinning:

Squanchy please do, i’m really interested myself. thanks for all the help here! appreciating a lot.

@Markmand I hope the help I received has served you too.

Thanks to everyone who helped me, especially to @ScruffR

1 Like