WebHook with more then one value

Hi everyone,

I am very new to this I am on day 5 as a photon owner. Here is what i am trying to do and what I could use a little advice about.

I need to post more then one value from my photon to a web API. Sending it as a Get is also an option but again its more then one value that must be sent.

Here is what i have come up with so far.

Lets say Light level and devices IP address. I have been reading about this for a while. Here is what i have come up with.

IPAddress myAddr = WiFi.localIP();
byte oct1 = myAddr[0];
byte oct2 = myAddr[1];
byte oct3 = myAddr[2];
byte oct4 = myAddr[3];
char s[16];  
sprintf(s, "%d.%d.%d.%d", oct1, oct2, oct3, oct4);
    
char crap[1000] ;
sprintf(crap,"{ \"DeviceLocation\": \"LivingRoom\",\"DeviceIP\": \"%s\" ,\"LightLevel\": \"%u\"}" , s,analogvalue);
Particle.publish("CurrentData", crap,  PRIVATE);

This should if i understand it correctly send a JSon string to the event log.

I have been looking at web hooks and i cant figure out if its even possible to get the web hook to read the events json string and then post that to the server location i have set in the web hook. everything i have found was for using forms and {{PARTICLE_EVENT_VALUE}} I don’t think i can use that because i need to send more then one value from the device.

Unfortunately my device is offline for some reason and i cant test it. But I want to be sure i am on the right track with this before i start going off in the wrong direction.

Yes, that should work. Instead of using {{PARTICLE_EVENT_VALUE}}, which includes the entirety of the data, in your web hook definition, you can use the individual variables: {{DeviceLocation}}, {{DeviceIP}}, and {{LightLevel}} that are automatically parsed from the event value, when the event value is JSON.

1 Like

Awesome! I cant wait to get home and try it out. Thanks for verifying that I am on the right track.

I am having a lot of fun with this but its going to take some time to learn.

2 Likes

Also, if you want, you can convert an IPAddress to a const char * suitable for passing to a %s argument to sprintf by using myAddr.toString().c_str(). Or even WiFi.localIP().toString().c_str(). Both will return the 4-octet dotted decimal format IP address string like the one you manually generated.

2 Likes

unfortunately this isn’t working at all.

Creating a web hook that posts and adding the json

{
  "Location": "{{DeviceLocation}}",
  "Ip": "{{DeviceIP}}"
}

appears to be sending something along the lines of Http:\site…com{json here} yes I am sure i put post but its like its sending it as a HTTP GET. I wonder if its just not setting the content type to application/json so the webservice doesn’t know to accept json.

Also it appears that the values in {{DeviceLocation}} are empty even thought i can see that the event hit has values.

{“data”:"{ “DeviceLocation”: “LivingRoom1”,“DeviceIP”: “192.168.1.221” }"“ttl”:“60”,“published_at”:“2016-06-15T16:26:02.667Z”,“coreid”:“280024000347343337373738”,“name”:“SendData”}

I am not sure what i have done to it now because its not even hitting the webhook on the event anymore :frowning:

Very frustrated why isn’t there any documentation or examples on how to use webhooks with posting json is there is an option to do it.

Why cant i edit a webhook???

Hey @jeiden, want to chime in here?

I’m not sure why it’s not working for you. Here’s what I did that works; maybe an example will help.

Photon code:

#include "Particle.h"

const unsigned long PUBLISH_PERIOD_MS = 30000;

unsigned long lastPublish = 0;
int lightLevel = 1;

void setup() {

}

void loop() {
	if (millis() - lastPublish >= PUBLISH_PERIOD_MS) {
		lastPublish = millis();

		char buf[1000];
		snprintf(buf, sizeof(buf), "{ \"DeviceLocation\":\"LivingRoom\",\"DeviceIP\":\"%s\",\"LightLevel\":\"%u\"}",
				WiFi.localIP().toString().c_str(), lightLevel);
		Particle.publish("LightLevel", buf, 60, PRIVATE);
	}
}

Hook definition (hook.json):

{
    "event": "LightLevel",
    "url": "http://requestb.in/s5jm3ms5",
    "requestType": "POST",
    "json": {
    	"DeviceLocation":"{{DeviceLocation}}",
    	"DeviceIP":"{{DeviceIP}}",
    	"LightLevel":"{{LightLevel}}"
    },
    "mydevices": true,
    "noDefaults":true
}

By the way, if you have the Particle CLI install you just edit the hook.json file, delete any old hook, and then upload the new one from the command line.

particle webhook create hook.json 

And here’s what it looks like on RequestBin:

1 Like

I am actually doing this all though the web site. I haven’t spent the time trying to get Particle CLI working. Website has a few bugs i have found. Oops it says didn’t create the web hook when in fact it did create it. oops couldn’t delete it when in fact it did delete it.

I think part of my problem is there appears to be some kind of lag. So I create a web hook it doesn’t work i delete it create a new one and it takes the log system a few minutes to pick up the new hook. I am used to working with a faster response time i have had to slow myself down to ensure that its actually running the one i am currently trying to test. It actually yelled at me at one point, to many errors detected stopping for a while. :confused:

I think i have it almost working using SEND CUSTOM DATA : None almost because if i hard code each of the parameters it appears to work correctly but when i try and give a parameter value as one of the values sent by the event its not adding the data value to the request. {{DeviceLocation}}

I think i am very close to getting this to work. I can see why doing it like this will be much smarter then sending a raw HTTP post directly from the particle board. Feel a little sorry for my board I have it sending events every second for me to test with hardware abuse.

I am going to give it another hour of this if i cant get it to work i will try and CLI to work tomorrow.

Status: One web hook is working the other is not. I cant for the life of me figure out the difference beyond the fact that they have different web endpoints.

Hi there!

Sorry for the trouble here. One factor that may have been impacting the webhook creation and deletion was the fact that we’ve been dealing with some sporadic outages of the webhook service over the past few days. Our engineering team will be working hard over the next sprint to improve reliability of this service.

In addition, we will also be rolling out a “webhooks audit log” shortly, championed by @bryce. This will help give you detailed information about why a webhook may be failing by giving you access to HTTP requests and responses.

Hope this is helpful!

Jeff

Sadly i must admit that it was not all your fault. The json i was sending to the event was invalid so the webhook couldn’t parse it that’s why the values where null.

As for the issues with delete and creating of web-hooks along with the slow response that’s all on you guys.

Keep up the good work. :slight_smile:

Anything in particular you did wrong with your json format? I’ve followed this thread through and I’m struggling! I think it may be due to the length of data I’m sending with the webhook! (too long)

@joearkay, if you are sending more than 255 chars in the webhook then yes, it is too long. If you provide more details, we can support you better :grinning:

Not checking to be 100% sure that the server in question accepted data in Json format which it turns out it didn’t.

Must learn to check things with curl first and not just assume.

2 Likes