Adafruit Webhook - Json Formatting error

I'm trying to follow this post but I'm missing something:
Webhooks limit of 20? - Cloud / Integrations - Particle

My ultimate goal is to update my Adafruit feed and attach a second value as metadata, but I can't even get one value to update in Adafruit.

My webhook fails with an error message {"status":400,"error":"The data sent could not be parsed as valid JSON"}

Thank you in advance for pointing out what I'm sure is a simple error.

John

Here is my code:

void sendCombinedMetrics() {
    // Send to Adafruit
    String celsiusString = "15";
    String feedID = "MYFEEDID";
    String feedName = "MYFEED";
    String feedData = "{ \"value\": \"" + celsiusString + "\", \"feedID\": \"" + feedID + "\"}";
    Particle.publish(feedName, feedData, 60, PRIVATE);
    hourlyMessageSent = 1;

}

The event data in the console is:

{
  "name": "MYFEED",
  "data": "{ \"value\": \"15\", \"feedID\": \"MYFEEDID\"}",
  "ttl": 60,
  "published_at": "2024-02-04T16:39:04.455Z",
  "coreid": "1c002b001947343438323536"
}

The webhook URL is:

https://io.adafruit.com/api/v2/webhooks/MYFEED/{{{feedID})

My JSON value in the webhook is:

{β€œvalue”: β€œ{{value}}”}

My header in the webhook is:

{
  "Content-Type": "application/json",
  "Accept": "application/json",
  "X-AIO-Key": "aio_XXXXXX"
}

You have three opening curly braces in your URL but only one closing curly and one closing parethesis - that's not valid IMO :wink:

Thank you - I got all excited that it was indeed a typo but looks like that was just a copy/paste error. I really appreciate your quick response though.

I poked around a little more and came up with this test program that seems to work. I added some more details in hopes it helps someone else set this up.

Thank you again!

#include <Particle.h>

int hourlyMessageSent = 0;


void sendCombinedMetrics() {
    // Send to Adafruit
    String runtimeString = "2.24";
    String cyclesString = "8";
    String feedID = "sump-pump";
    String feedName = "MYFEEDNAME";
    String feedData = "{ \"runtime\": \"" + runtimeString + "\", \"cycles\": \"" + cyclesString + "\", \"feedID\": \"" + feedID + "\"}";
    Particle.publish(feedName, feedData, 60, PRIVATE);
    hourlyMessageSent = 1;

}

void setup() {

}

void loop() {
    if (hourlyMessageSent == 0){
        sendCombinedMetrics();
    }

}

The webhook URL is:
https://io.adafruit.com/api/v2/MYFEEDNAME/feeds/{{{feedID}}}/data

The custom json is:

          
{
  "value": "{{{PARTICLE_EVENT_VALUE}}}"
}

I can subscribe to the Adafruit MQTT Topic and the incoming data looks like this:

{
  "runtime": "2.24",
  "cycles": "8",
  "feedID": "sump-pump"
}

I've ran into a lot of issues with JSON strings and the ParticlePublish command. I ended up just sending up a string and then using a decoder on my Dashboard to format it.

One option is to use a library to encode the JSON for you and then use particle publish to send it. arduinoJSON is one option.

@hansA ,

The formatting for a webhook should be fairly straightforward. A good rule of thumb for Particle programming is not to use Strings unless there is no other alternative. In this case, there is.

All you need to do is send the key value pairs:

void sendEvent() {
  char data[256];                                                     // Store the date in this character array - not global
  const char *feedIDStr = "sump-pump";
  snprintf(data, sizeof(data), "{\"runtime\":%4.2f, \"cycles\":%i,\"feedid\":%s }",runtime, cycles, feedIDStr);
  Particle.publish("MYFEEDNAME", data, PRIVATE | WITH_ACK);
  Log.info("Webhook: %s", data);                              // For monitoring via serial
}

Then you just set up your payload in the webhook using JSON

{
  "runtime": "{{runtime}}",
  "cycles": "{{cycles}}",
  "feedid": "{{feedid}}"
}

Give it a try and remember, Strings are bad (or so I am told).

Going forward, if you need to construct JSON objects and want help with the syntax (and double quotations) try the JSONParserGenerator_RK library in the Particle library system.

Thanks,

Chip

Thank you Chip, I will give it a try.

1 Like