Use case for subscribe the same event for multiple device and know him


#1

Hi

Several Xenon transmit a temperature on the mesh network and one Argon is programmed to do a subscribe. All work great with this code :

ex:
void myHandler(const char *event, const char *data)
{
Serial.printlnf(“event=%s data=%s”, event, data ? data : “NULL”);
//ex: printed event=temp data=23.2

}

void setup() {
Mesh.subscribe(“temp”, myHandler);
}

If I want to know which Xenon send me the payload using only one subscribe for my temperature ?
I look for similar like :

device=Xenon1 event=temp data=23.2

How you can do that ?


#2

On the Xenons, make the event id something like temp-deviceID where you concatenate the actual device id onto the end of the even name. The Argon subscribe can stay the same. In the subscribe handler you can pull the device id out of the event name using any method you choose (strtok, etc.).


#3

Really ?

On the Argon, like that ??

Mesh.subscribe(“temp-1”, myHandler);
Mesh.subscribe(“temp-2”, myHandler);
Mesh.subscribe(“temp-3”, myHandler);
Mesh.subscribe(“temp-4”, myHandler);
Mesh.subscribe(“temp-5”, myHandler);


#4

You do not need multiple lines. The first string is not an exact match. It’s only a prefix. So a single line of

Mesh.subscribe("temp", myHandler);

should match all devices - whatever comes after “temp”. Then in the event handler you can extract the device number from the passed in event name.


#5

Thanks I was sure that event name needed to be exact match. Thanks


#6

That’s where reading the docs beats assumption :wink:
https://docs.particle.io/reference/device-os/firmware/argon/#particle-subscribe-


#7

Thanks to refer me to the doc, always doc reference are bookmarked in my browser but for that I asked question be cause I wanted to learn the best use case.

Like MQTT topics I’ll look to use like that :

place/xenonid/temperature/

Thanks