From 51f61b6f38f4ffd1b523884046bb4d2dbff55c37 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Sun, 16 Jun 2024 06:44:03 -0600 Subject: [PATCH] Enhance checkConnection() function (#240) * Modify checkConnection() - Define MESH_CONNECTION_CHECK_ATTEMPTS - Return from checkConnection() if -2 received, retry if -1, success if address matches, fail otherwise * Update RF24Mesh_config.h Co-authored-by: Brendan <2bndy5@gmail.com> * Remove breaks; from switch - Per @2bndy5 in #240 * Update comment --------- Co-authored-by: Brendan <2bndy5@gmail.com> --- RF24Mesh.cpp | 17 +++++++++++++---- RF24Mesh_config.h | 10 ++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/RF24Mesh.cpp b/RF24Mesh.cpp index 39c8dc5..44fa9ec 100644 --- a/RF24Mesh.cpp +++ b/RF24Mesh.cpp @@ -151,12 +151,21 @@ void RF24Mesh::setChild(bool allow) bool RF24Mesh::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 */