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

add option to reattempt connection in setup() for arduino examples #203

Closed 2bndy5 closed 2 years ago

2bndy5 commented 2 years ago

In testing the mesh examples on arduino, I found it kinda annoying to get up and reset the MCU when the examples don't connect during setup(). This is completely my fault since I added an infinite loop when mesh.begin() returned false (for unresponsive radio hardware reasons).

Proposal

For any non-master node examples, change

  // Connect to the mesh
  Serial.println(F("Connecting to the mesh..."));
  if (!mesh.begin()) {
    Serial.println(F("Radio hardware not responding or could not connect to network."));
    while (1) {
      // hold in an infinite loop
    }
  }

to

  // Connect to the mesh
  Serial.println(F("Connecting to the mesh..."));
  if (!mesh.begin()) {
    if (radio.isChipConnected()) {
      while (mesh.mesh_address == MESH_DEFAULT_ADDRESS) {
        Serial.println(F("Could not connect to network."));
        Serial.println(F("Press any key to reattempt a connection to the mesh."));
        while (!Serial.available()) {
          // wait for user input
        }
        Serial.read(); // dispose of first input char
        Serial.println(F("Connecting to the mesh..."));
        mesh.renewAddress();
      }
    }
    else {
      Serial.println(F("Radio hardware not responding."));
      while (1) {
        // hold in an infinite loop
      }
    }
  }
TMRh20 commented 2 years ago

I think this is a good idea, however I'm not sure we need to wait for serial input to try again, why not just loop the renew in automatically?

2bndy5 commented 2 years ago

good idea. I'll adjust it to do that.

  // Connect to the mesh
  Serial.println(F("Connecting to the mesh..."));
  if (!mesh.begin()) {
    if (radio.isChipConnected()) {
      while (mesh.renewAddress() == MESH_DEFAULT_ADDRESS) {
        // mesh.renewAddress() will return MESH_DEFAULT_ADDRESS on failure to connect
        Serial.println(F("Connecting to the mesh..."));
      }
    }
    else {
      Serial.println(F("Radio hardware not responding."));
      while (1) {
        // hold in an infinite loop
      }
    }
  }