# Call function in loop

**URL:** <https://community.particle.io/t/call-function-in-loop/43818>\
**Category:** Troubleshooting\
**Created:** [September 5, 2018, 1:30am UTC](https://community.particle.io/t/call-function-in-loop/43818 "2018-09-05T01:30:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Squanchy](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/squanchy/32/18146_2.png) [@Squanchy](https://community.particle.io/u/Squanchy)\
**Post date:** [September 5, 2018, 1:30am UTC](https://community.particle.io/t/call-function-in-loop/43818/1 "2018-09-05T01:30:05Z")

</div>

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!! 😁

```auto
#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);

  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()) {        
        jsonParser.getOuterValueByKey("tempMax", tempMax);
        jsonParser.getOuterValueByKey("tempMin", tempMin);
    }
}

```

---

<div class="post-metadata">

**Author:** ![seulater](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/seulater/32/280_2.png) [@seulater](https://community.particle.io/u/seulater)\
**Post date:** [September 5, 2018, 5:09am UTC](https://community.particle.io/t/call-function-in-loop/43818/2 "2018-09-05T05:09:07Z")

</div>

take a look into Software Timers. You can have them fire at certain time intervals.  
[https://docs.particle.io/reference/firmware/photon/#software-timers](https://docs.particle.io/reference/firmware/photon/#software-timers)

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [September 5, 2018, 5:26am UTC](https://community.particle.io/t/call-function-in-loop/43818/3 "2018-09-05T05:26:28Z")

</div>

> [@Squanchy](#):
>
> I want to call the “myHandler” function every 6 seconds in a loop to read if there is updated data.

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.

---

<div class="post-metadata">

**Author:** ![Squanchy](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/squanchy/32/18146_2.png) [@Squanchy](https://community.particle.io/u/Squanchy)\
**Post date:** [September 5, 2018, 3:12pm UTC](https://community.particle.io/t/call-function-in-loop/43818/4 "2018-09-05T15:12:30Z")

</div>

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

> [@nrobinson2000](#):
>
> Why do you want to call it every 6 seconds when it should only run in response to the webhook?

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.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 5, 2018, 3:16pm UTC](https://community.particle.io/t/call-function-in-loop/43818/5 "2018-09-05T15:16:20Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Squanchy](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/squanchy/32/18146_2.png) [@Squanchy](https://community.particle.io/u/Squanchy)\
**Post date:** [September 5, 2018, 3:25pm UTC](https://community.particle.io/t/call-function-in-loop/43818/6 "2018-09-05T15:25:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Markmand](https://avatars.discourse-cdn.com/v4/letter/m/b9bd4f/32.png) [@Markmand](https://community.particle.io/u/Markmand)\
**Post date:** [September 5, 2018, 4:52pm UTC](https://community.particle.io/t/call-function-in-loop/43818/7 "2018-09-05T16:52:56Z")

</div>

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 [https://suppsforlife.to/category/pct/](https://suppsforlife.to/category/pct/) as there something isn’t well going out. thanks

---

<div class="post-metadata">

**Author:** ![Squanchy](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/squanchy/32/18146_2.png) [@Squanchy](https://community.particle.io/u/Squanchy)\
**Post date:** [September 6, 2018, 3:12am UTC](https://community.particle.io/t/call-function-in-loop/43818/8 "2018-09-06T03:12:52Z")

</div>

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:**

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/2/1/21609a466cc537de0cf1bf85b66351120f635130.png)

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/d/9/d967ae5f285ae03333696834689dd61c3ef80fe9.png)

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

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 6, 2018, 5:57am UTC](https://community.particle.io/t/call-function-in-loop/43818/9 "2018-09-06T05:57:43Z")

</div>

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.  
e.g.

```auto
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");
    }
    else
      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.

---

<div class="post-metadata">

**Author:** ![Squanchy](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/squanchy/32/18146_2.png) [@Squanchy](https://community.particle.io/u/Squanchy)\
**Post date:** [September 6, 2018, 11:50pm UTC](https://community.particle.io/t/call-function-in-loop/43818/10 "2018-09-06T23:50:53Z")

</div>

@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!! 😀

> 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  
Regards!!
