How to pass an Integer variable to another particle?

Hello, I need to pass an Integer from one particle to another one. I know that there is the Particle.publish and Particle.subscribe system. This works with strings but not with variables like int or float.

Ok, I know that there is the possibility to to convert the int into a string, send it out by “publish”, fetch it with subscribe, re-convert it into an int and save it to a local variable. I have done that and this works fine. However, I wanted to know if there is a more elegant way.

If I expose a variable, as given for example in in the official docs :

int analogvalue = 0;

then in setup:

Particle.variable("analogvalue", analogvalue);

and in loop:

analogvalue = analogRead(A0);

Is there a way to read this variable from another particle? The doc provides explanations on how to read it via a request in a terminal. But I am wondering how I can code such a request in the code for another particle.

You cannot query a variable using the Particle API that runs on a Particle device. The much easier solution is to just turn your integer value into a string. The String class makes this easy:

String str = String(analogRead(A0));
int val = str.toInt();