How can I store the mesh.subscribe received data into a local parameter


#1

Hi:
My codes use too much mesh.subscribe and particle.subscribe functions. In the past, I just store these returned values into global parameters. So, does these functions could be like normal designed function that I can store the returned value into local parameter.
For example, for this Mesh.subscribe(“try”, PublishEvent), if I let the PublishEvent is an int function.
Can I write something like this “int a = PublishEvent”. I know I can’t write it like this. I mean I just want to do something like a normal designed function that can return values, so that I can store this value into a local parameter.


#2

You may want to have a look in the reference docs that show how to use the functions first

https://docs.particle.io/reference/device-os/firmware/argon/#subscribe-

How would that solve your problem about using too many such functions?

If you could clarify why you think you’d need so many different subscribe handlers we may be able to suggest alternatives.

Or you search the forum for something like subscribe limit


#3

Sorry for my problem description. I think I should re-describe my problem.
For example, I design an function.

int test(int a) {
  int b =a;  
  return b; 
}

If I call this function, the function “test” will return a value, and then I can write c = test(5), so c=5. In this way, I assign the returned value to a local parameter c.
If I call the mesh handler, I have to claim a global paramter, like int c.

void myHandler(const char *event, const char *data)
{
   c = atoi(data);
}

So, the mesh call the handler function, and then assign value to c.
I want to do like the function before. So, is it impossible?
Just I dont want use global parameter.


#4

To get things straight first

You nor your code (normally) will ever call that function. The system does when a remote even is caught by the system and then the system passes in the data it received from the cloud.
You only tell the system how you want to handle such incoming data, but the actuall call comes from the system.

Next, the subscribe handler is (by definition without exception) a void function which will never ever put a return value on the stack for the caller (the system which wouldn’t care about any return value anyway) to collect.

Having that out the way the answer to this …

… is a clear: “Yes, that is impossible”.

However, there are other ways to circumvent the use of global variables.
You can always call functions and methods that manipulate non-global variables but you need some way of communication between your application code and the system that’s calling the handler (your code never does - usually).