Calling/processing webhook causes connection to drop using the weather example code

I am trying to get temperature data for my area. I followed the weather example from here:
https://docs.particle.io/guide/tools-and-features/webhooks/#your-first-webhook

What I’ve done:
I created the webhook. I checked it was created using “particle webhook list”.

I subscribed to my webhooks using “particle subscribe mine” and then from another terminal called the webhook uisng “particle publish get_weather”.

I can see the webhook getting called every single time reliably with the weather data I expect.


I also see the same activity in the Dashboard. This gives me confidence that the webhook work well and works every time.

Then, I copied and pasted the code from the example and flashed it into my core. I connected to the serial port to see activity.
The issue is that periodically, the core will lose connection and start blinking cyan just before calling the webhook. This keeps happening more often than not. I have see it blink red every now and then…

My wifi connection is fine; I’ve never had issues with the core on my network. I’ve had a core running continuously for a long time without issue displaying indoor temperature. As soon as I add a call for this webhook to the code, it starts to disconnect over and over. I was someone suggest to rename the webhook to something other than get_weather but that didn’t help.

This is from the Dashboard. You can see how it disconnects after or before every call. The computer displaying the dashboard is on the same wifi network so there is no doubt that wifi works.

This is the code running on the core:

    void setup() {

    Serial.begin(115200);
    Spark.subscribe("hook-response/get_weather", gotWeatherData, MY_DEVICES);

    for(int i=0;i<10;i++) {
        Serial.println("waiting " + String(10-i) + " seconds before we publish");
        delay(1000);
    }
    }

    void loop() {

    Serial.println("Requesting Weather!");
    Spark.publish("get_weather");
    delay(60000);
    }

    void gotWeatherData(const char *name, const char *data) {

    String str = String(data);
    String tempStr = tryExtractString(str, "temp", "pressure");

    if (tempStr != NULL) {
        Serial.println("The temp is: " + tempStr + String(" *F"));
    }

    String tryExtractString(String str, const char* start, const char* end) {
    if (str == NULL) {
        return NULL;
    }

    int idx = str.indexOf(start);
    if (idx < 0) {
        return NULL;
    }

    int endIdx = str.indexOf(end);
    if (endIdx < 0) {
        return NULL;
    }

    return str.substring(idx + strlen(start), endIdx);
    }

Has anyone experienced this behavior? Is there anything I’m clearly doing wrong here? I’m kind of stuck on this issue so any help would be appreciated. Thanks.

I’m by far no expert on webhooks, but I’d try a few modifications to the code.

Try to remove the MY_DEVICES from the subscribtion and replace the delay(60000) with this

uint32_t msDelay;

void loop()
{
  if (millis() - msDelay > 60000)
  { 
    msDelay = millis();
    // your other code
    ...
  }
}

And just for testing the webhook alone, only do Serial.println(data); inside the handler, before you get to the String manipulation (I would not touch String with a stick unless I had to :stuck_out_tongue_closed_eyes:)

Thanks for the suggestions. I removed MY_DEVICE and that stops the core from losing connection.
It also stopped the subscription from receiving any data so the gotWeatherData function never gets called. So it seems that without MY_DEVICES the whole thing won’t work :frowning:

OK, then MY_DEVICES is needed :blush:
How about the other suggestions?
Especially the avoid String thing, since you are allocating rather big chunks of heap each time.

I got it working by changing the json response template to only pass the data that I want.

I followed this tutorial: Tutorial: Webhooks and Responses with Parsing (JSON, Mustache, Tokens)

I suspect the root of the problem is with using too much memory when importing the entire json response into the core as the official particle example suggests to do. Using response template in json is much more efficient.

1 Like