These are the sections in the official reference docs that should explain everything
https://docs.particle.io/reference/device-os/firmware/xenon/#publish-
https://docs.particle.io/reference/device-os/firmware/xenon/#subscribe-
The samples won't get any simpler than shown in the docs
// receiver side - needs to setup once and will be called on demand by the system
void myHandler(const char *event, const char *data)
{
Serial.printlnf("event=%s data=%s", event, data ? data : "NULL");
}
void setup()
{
Serial.begin(9600);
Mesh.subscribe("motion-sensor", myHandler);
}
// sender side (anywhere in your code - e.g. inside `loop()`
...
Mesh.publish("motion-sensor", "living room");
...
You setup your receiver and have it sit there minding its own busines till any sender publishes an event which will be delivered to all the subscribers where the subscription callback will be invoked and do its job.
So I think your main question is not about how to send/receive data inside your mesh but rather how to send your data - irrespectivce of scope (e.g. intra mesh vs. cloud), right?
If so, I think you shouldn't limit your search to mesh transport but rather investigate the fundamentals of wrapping your data into a publishable string.
In this regard this thread might be helpful
(make sure to read the entire thread or any other thread that explains the use of snprintf()
)
Once you have composed the event data string, the next step would be parsing the received data. For that you could either use sscanf()
for any format string or a dedicated parser library (e.g. for JSON JsonParserGeneratorRK
is a good one - this can also be used on the sender side to generate the data string)