Skip to content

Commit

Permalink
Enhance checkConnection() function (#240)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Remove breaks; from switch

- Per @2bndy5 in #240

* Update comment

---------

Co-authored-by: Brendan <[email protected]>
  • Loading branch information
TMRh20 and 2bndy5 authored Jun 16, 2024
1 parent 8f030b4 commit 7827e87
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
17 changes: 13 additions & 4 deletions RF24Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,21 @@ template<class network_t, class radio_t>
bool ESBMesh<network_t, radio_t>::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;
}

/*****************************************************/
Expand Down
10 changes: 10 additions & 0 deletions RF24Mesh_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 7827e87

Please sign in to comment.