NRF24L01, how to check if signal is lost ?

Hello everyone,
thank you for reading and trying to help

Well i have seen another post asking this question but honestly i could not understand the solution and there was no answer from the guy who asked it at the end.

So I am trying to make a way to know that the signal from the NRF24L01 is lost so i can disable all other systems (LEDs, motors, etc).

first thing i thought of is using if(network.available()) instead of while( network.available() ) but it doesn’t work because it stays in the else block

So here is my successfully tested sketch for network:

Transmitter:

#include <SPI.h>
#include <RF24.h>
#include <RF24Network.h>

//1st byte is for throttle value from pot

int msgTX[2]; //Message to be transmitted, can contain up to 2 array elements, 2 bytes


//Defining radio object for the RF24 function
RF24 radio(9, 10);               // nRF24L01 (CE,CSN)
RF24Network network(radio);      // Include the radio in the network
const uint16_t this_node = 00;   // Address of this node in Octal format ( 04,031, etc)
const uint16_t node01 = 01; 


void setup() {
  Serial.begin(9600);
  SPI.begin();
  radio.begin();
  network.begin(90, this_node);  //(channel, node address)
  // this next line should be added for transmitter and receiver both or removed for both
  radio.setDataRate(RF24_2MBPS);
  
}
void loop() {
  
  network.update();
  msgTX[0] = 1;
  msgTX[1] = 4;
  //unsigned long value = 1;

  RF24NetworkHeader header(node01);     // (Address where the data is going)
  bool ok = network.write(header, &msgTX, sizeof(msgTX)); // Send the data
  
}

//DATA Receiving from the Receiver part, Acknowledgment Data
/*void AcknowledgmentDATA(){
    
    while ( radio.isAckPayloadAvailable() ){
        //Serial.println("Ack Available");
        radio.read(ackMessage, sizeof(ackMessage));
        int value = ackMessage[1];
        Serial.println(value);
   }
  
}*/

Receiver:

#include <SPI.h>
#include <RF24.h>
#include <RF24Network.h>

int msgRX[2]; //Message to be transmitted, can contain up to 2 array elements, 2 bytes

//Defining radio object for the RF24 function pin 9 and 10 of arduino
RF24 radio(9, 10);               // nRF24L01 (CE,CSN)
RF24Network network(radio);      // Include the radio in the network
const uint16_t base = 00;
const uint16_t this_node = 01;   // Address of our node in Octal format ( 04,031, etc)


void setup() {
  
  Serial.begin(9600);
  SPI.begin();
  radio.begin();
  network.begin(90, this_node); //(channel, node address)
  // this next line should be added for transmitter and receiver both or removed for both
  radio.setDataRate(RF24_2MBPS);
  
}
void loop() {
  
  network.update();
  while( network.available() ) {     // Is there any incoming data?
    RF24NetworkHeader header;
    //unsigned long incomingData;

    // on the receiver side, we must use msgRX to get all the data then we define the variable after reading
    network.read(header, &msgRX, sizeof(msgRX)); // Read the incoming data

    // here we define the variables after storing the incoming data into an array msgRX
    int value1 = msgRX[0];
    int value2 = msgRX[1];
    
    Serial.println("Successful connection -----");
    Serial.print("Value 1: ");
    Serial.println(value1);

    Serial.print("Value 2: ");
    Serial.println(value2);
    
  }
}

I am not an expert on NRF24L01 devices, but you need to be clear on what you mean by a 'lost' connection.

Is it 'lost' if there are no packets received within a certain time period or is it 'lost' if you send a packet and there is no acknowledge received within a certain time period.

TX: use the result of the sending operation, retry with software if needed/wished

RX: record the time of the last reception and after a given time without packets, you lost the connection

Connection is used in a very loose way here, there is no physical/real connection in a NRF24 setup.

Whandall:
TX: use the result of the sending operation, retry with software if needed/wished

RX: record the time of the last reception and after a given time without packets, you lost the connection

Connection is used in a very loose way here, there is no physical/real connection in a NRF24 setup.

Whandall, how can I do that ? what should i put in the if(millis()-timeOfLastReceived > 2000) ?

If "no reception for two seconds" means "connection lost" to you, absolutely.

ok I made it and it works, i added the receivedTime = millis() in the while loop
thank you for the help :smiley:

I'm glad I could help. Have a nice day.

Whandall:
I'm glad I could help. Have a nice day.

I came up with a solution for knowing which receiver of the 5 i have have been lost or turned off.
What I did is adding a signature for each receiver which means sendingData[0] = 1 for receiver 1 and sendingData[0] = 2 for receiver 2, these will be sent from the receivers to the transmitter
and on the transmitter side i made incomingData[0] == 1 then receiver 1 is on and same for other receivers, now for doing the sending and transmitting i have to take roles between the transmitting action and receiving on the receiver side but setting a boolean send = true or false and make it true when the data was sent so the receiving actions takes over then send = false so the sending actions takes over again and vice versa ! and it worked great after testing this ! It damn feels satisfying :smiley: !

thanks all for the help :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.