diff --git a/RF24Mesh.cpp b/RF24Mesh.cpp index f7bf09a..c9ec79c 100644 --- a/RF24Mesh.cpp +++ b/RF24Mesh.cpp @@ -159,12 +159,21 @@ template bool ESBMesh::checkConnection() { // getAddress() doesn't use auto-ack; do a double-check to manually retry 1 more time - if (getAddress(_nodeID) < 1) { - if (getAddress(_nodeID) < 1) { - return false; + for (uint8_t i = 0; i < MESH_CONNECTION_CHECK_ATTEMPTS; i++) { + + int16_t result = getAddress(_nodeID); + switch (result) { + case -2: return false; // Address not found in list or is default + case -1: continue; // Write failed or timed out + case 0: return false; // This is a master node + default: + if ((uint16_t)result == mesh_address) { // Successful address lookup if result == RF24Network address + return true; + } + break; } } - return true; + return false; } /*****************************************************/ diff --git a/RF24Mesh_config.h b/RF24Mesh_config.h index 64bd74a..0c0d1a7 100644 --- a/RF24Mesh_config.h +++ b/RF24Mesh_config.h @@ -54,6 +54,16 @@ #define MESH_MEM_ALLOC_SIZE 10 #endif // MESH_MEM_ALLOC_SIZE +/** + * @brief Number of attempts to verify a connection + * + * On child nodes, when calling `mesh.checkConnection();`, configure how many attempts will be made to contact the master node to verify connectivity + * Raising this number can result in a more stable mesh, since nodes can more easily verify that a connection is active + */ +#ifndef MESH_CONNECTION_CHECK_ATTEMPTS + #define MESH_CONNECTION_CHECK_ATTEMPTS 3 +#endif + /**************************/ /*** Debug ***/ //#define RF24MESH_DEBUG_MINIMAL /** Uncomment for the Master Node to print out address assignments as they are assigned */