Webhook & variable usage

Dear All,
first of all, I’m new to webhooks and not really good with it ;-(
I figured out how to get a GET webhook working. it is fetching to values from a DB via a PHP file and a JSON output
The webhook/webpage returns for example { "kCount_overall": 9586.080, "ReportCounter": 89 }
First of all I would like to use the ReportCounter value it should show my where logging stopped last time…

I defined the variable at the beginning of my code:
unsigned long ReportCounter = 1;

Then I created a function:

long ReportCounter_startup(const char *event, const char *data) {
  unsigned long ReportCounter = atof(data);
  return ReportCounter;
}

That really gets the value “89” :wink: (Tested with a publish in that function)

During void setup() I subscribe to the hook-response’s and then publish something to fire the webhook.
I can see at the dashboard the hook is working…

In my void loop() I would like to use that value to increment it evereytime something is writen back to db… but it never comes there… always starting with “1”

What did I forget…?
Thanks for your help in advance

Could you show us your entire code? Hard to debug something we can’t see :wink:

Actually… it seems as though you’re creating a new local variable inside your subscription handler, which has the same name as the global variable. Due to scoping, only the local one is being used, thus it never ‘leaves’ the handler.

Add to that the fact that your are mixing types: long, unsigned long and float which is what atof() returns!

2 Likes

I guess you don’t use this function as a subscribe handler direct either, do you?

Thanks to Moors & peekay123 I learned again much about variables in c++ and can now see that I redeclared the ReportCounter variable within the function local.... THANK YOU!

I changed the function to:

unsigned long ReportCounter_startup(const char *event, const char *data) {
  char * succ;
  ReportCounter = strtoul(data, &succ, 0);   
  return ReportCounter;
}

and for ScruffR: I use the following to call that function:

  Particle.subscribe("hook-response/Webhook1_fromDB", ReportCounter_startup, MY_DEVICES);
  Particle.publish("Webhook1_fromDB", "test_getfromDB", PRIVATE);

Hmm, that's odd, since the callback function for Particle.subscribe() should actually look like this (at least according to the docs)
https://docs.particle.io/reference/firmware/photon/#particle-subscribe-

It should not return anything but if it returns something who's going to catch the result?

I used this from the reference as template:
https://docs.particle.io/reference/webhooks/#receiving-complex-data

Yes, and where is the handler returning anything?

https://docs.particle.io/reference/webhooks/#receiving-complex-data

To pass data back from the event handler, you'd need to modify a variable which is available outside of it too (e.g. global)
Any return value would just be dropped.

I deleted “void” and replaced it with unsigned long like in post No.5… worked for me… :slight_smile:
ReportCounter is a global variable, and is altered after that and in the loop…

Yes, but the question stands - who would catch the return value and how?

Any return value that isn’t caught (by definition) is null and void, so why add the extra effort to push the result on the stack just to have it popped off unused immediately again?
The fact that it builds is no proof that it’s useful - it’s just not wrong.

hm, maybe I did not get the problem right… is there anything I should alter at my code? or I can do to do it better (there is of cause much) but for this case :wink:

Since this handler will (most likely) only be called from the system servicing the subscription, and this part of the system does not expect this function to return anything, you should stick with void and remove the return statement.
This will save some clock cycles and prevent any confusion as of why this function does not fit the definition of a normal event handler.

void ReportCounter_startup(const char *event, const char *data) {
  ReportCounter = strtoul(data, NULL, 0);   
}

But I’d probably add some checks whether the incoming data was valid and add some “flag” to indicate any potential error.

2 Likes

OK, thank you ScruffR - did change the code! Got that it was not necessary and probably a problem !