nRF24 / RF24Mesh

OSI Layer 7 Mesh Networking for RF24Network & nrf24L01+ & nrf52x devices
http://nrf24.github.io/RF24Mesh
GNU General Public License v2.0
421 stars 154 forks source link

improve examples' use of mesh.begin() and renewAddress() #205

Closed 2bndy5 closed 2 years ago

2bndy5 commented 2 years ago

This solves #203

While reviewing all the examples, I added some comments to the Linux C++ examples and ported the work from the mesh examples in pyrf24. I can revert the changes to the python examples if people are still using python2.7 for some reason, but I'm pretty sure boost.python has phased out maintenance support for python2.

TMRh20 commented 2 years ago

This commit just brought to light another issue with the examples.

        if(!mesh.renewAddress()){
          mesh.begin();
        }

I think should be?

        if(mesh.renewAddress() == MESH_DEFAULT_ADDRESS){
          mesh.begin();
        }

I guess when I wrote the examples, I was thinking it would return something else.

2bndy5 commented 2 years ago

I don't recall seeing that snippet anywhere. Why would you call begin() after renewAddress() fails?

But I would agree. Failure from renewAddress() is identified when it returns 04444.

2bndy5 commented 2 years ago

Wait, the docs don't mention what is returned when renewAddress() fails.

I suppose it is a good sign that users haven't noticed this oversight (or that no one reported it).

2bndy5 commented 2 years ago

Should I also add a while loop in the child node examples that demonstrate how to renewAddress() on write() failures?

        if (!mesh.write(&displayTimer, 'M', sizeof(displayTimer))) {
            // If a write fails, check connectivity to the mesh network
            if (!mesh.checkConnection()) {
                // The address could be refreshed per a specified timeframe or only when sequential writes fail, etc.
                printf("Renewing Address\n");
                mesh.renewAddress();
            }
            else {
                printf("Send fail, Test OK\n");
            }
        }
        else {
            printf("Send OK: %u\n", displayTimer);
        }

Or should we add a var isConnected that triggers a while (mesh.renewAddress() == MESH_DEFAULT_ADDRESS) in the loop()? This idea (to me) seems overly complex for a simple example.

TMRh20 commented 2 years ago

I don't recall seeing that snippet anywhere. Why would you call begin() after renewAddress() fails?

Its in all the client/child examples:

      // If a write fails, check connectivity to the mesh network
      if ( ! mesh.checkConnection() ) {
        //refresh the network address
        Serial.println("Renewing Address");
        if (!mesh.renewAddress()) {
          //If address renewal fails, reconfigure the radio and restart the mesh
          //This allows recovery from most if not all radio errors
          mesh.begin();
        }
      } else {
        Serial.println("Send fail, Test OK");
      }
    } else {
      Serial.print("Send OK: "); Serial.println(displayTimer);
    }
  }

It allows recovery from hardware failures etc. and makes the radios hot-swappable. I've swapped out faulty radios without having to reset the MCU...

Should I also add a while loop in the child node examples that demonstrate how to renewAddress() on write() failures?

Sure.

2bndy5 commented 2 years ago

I found that the RF24::isChipConnected() wasn't exposed in the old py wrapper. I fixed this on RF24 master branch.