Need some help with Mesh.subscribe

Here’s an excerpt from my MarcoPolo heartbeat code which you can find in this thread. This code will subscribe to a Mesh.publish message called “Polo”. When it receives that publish, it runs the ProcessBeat() function which records the message in an array and the time when it reported.

//Before setup(), declare the variables.
#define MAX_MESH_NODES 10  //Maximum number of nodes expected on the mesh. Make small as possible to preserve memory space.

uint8_t reportingNodesCount = 0;
char reportingNodes[MAX_MESH_NODES][32];

void setup() {
    //Here we are subscribing to the event named "Polo".
    Mesh.subscribe("Polo", ProcessBeat);
}

void ProcessBeat(const char *name, const char *data) {
    //Record the device that is responding and exit fast. Don't do lengthy tasks here.

    //Here I'm just keeping track of the number of nodes that responded to this message so far.
    uint8_t count = reportingNodesCount++;

    //The sprintf command copies the contents of data into my repoortingNodes array.
    snprintf(reportingNodes[count], arraySize(reportingNodes[count])-1, data);

    //Here we record the time (in milliseconds since start) that the function ran.
    reportingNodesMillis[count] = millis();
}
2 Likes