NRF24L01 struggling with code

Hello Gents,

I’m struggling with some code for the NRF24L01. My transmitter and receiver work like a charm so no worries there.
But i want to code something for the times when my transmitter returns NaN or when my receiver does not receive anything. For these times my display should return a message after 30 or so seconds.

I’ve looked through tutorials and examples but i can’t find stuf that notifies when something does go wrong.
And I find this library difficult to work with as a novice.
Hopefully someone can point me in the right direction.

I’ve attached the full file for my project as reference.

#include "Shared.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 8);
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

class THsensor {
private:  
unsigned long startTime = 0;
unsigned long prevTime = 0;
unsigned long currentTime = 0;
unsigned int sampleTime = 30000;
    
public:  


void THsetup(){
  startTime = millis();
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();
  }

void getData(){
  if (radio.available()){
    float dataToReceive[2];
    radio.read(&dataToReceive, sizeof(dataToReceive));
    temperatureValue = dataToReceive[0];
    humidityValue = dataToReceive[1];
    noSignal = 0;

    else if (/*Something when the return is NaN or when there is no signal*/){      
    currentTime = millis() - startTijd;
    if (currentTime - prevTime >= sampleTime){
        prevTime = currentTime;
        noSignal = 1;
        }
    }    
 }
}
};
THsensor TH;

WTW_bypass6.zip (1.61 KB)

I don't think the code in your Original Post will compile and I don't understand why you are using a Class.

In any case this piece of code is structured incorrectly

void getData(){
  if (radio.available()){
    float dataToReceive[2];
    radio.read(&dataToReceive, sizeof(dataToReceive));
    temperatureValue = dataToReceive[0];
    humidityValue = dataToReceive[1];
    noSignal = 0;

    else if (/*Something when the return is NaN or when there is no signal*/){      
    currentTime = millis() - startTijd;
    if (currentTime - prevTime >= sampleTime){
        prevTime = currentTime;
        noSignal = 1;
        }
    }    
 }
}

You can't just chuck an ELSE IF anywhere you like. It must the second part of an IF and you don't seem to have the first part.

Maybe you need something like this

if (radio.available()){
    float dataToReceive[2];
    radio.read(&dataToReceive, sizeof(dataToReceive));
    if (dataToReceived[0] ......  // whatever test is appropriate for a valid value
        temperatureValue = dataToReceive[0];
        humidityValue = dataToReceive[1];
        noSignal = 0;
    }
    else {
       // code for incorrect data
    }

Another thought is that it may be easier to deal with the invalid data on the TX program so that only valid data is transmitted.

...R
Edit to add the missing DON'T on the first line - sorry for any confusion

Yes it might be better to handle invalid data on the TX end.

This code was working for me before i tried to implement the other stuf. and i see now why ELSE IF does not work.

void getData(){
  if (radio.available()){
    float dataToReceive[2];
    radio.read(&dataToReceive, sizeof(dataToReceive));
    temperatureValue = dataToReceive[0];
    humidityValue = dataToReceive[1];
    }    
 }

The data on the TX side is being send every 2 seconds, if the connection is lost the RX the program keeps the last known values. But I would want the program to act when after about 30 seconds the RX has not received anything. By doing this i can put "connection lost" on the display.
I can't find anything that brings me closer to my goal.

U5ED:
But I would want the program to act when after about 30 seconds the RX has not received anything. By doing this i can put "connection lost" on the display.
I can't find anything that brings me closer to my goal.

That should be straight forward.

Save the value of millis() every time a message is received - something like

lastMessageTime = millis();

and then you can have a test in loop() like this

if (millis() - lastMessageTime >= 30000) {
   // there have been no messages for 30 secs
   // do something
}

...R

I figured that i have to do something like this.

Robin2:

lastMessageTime = millis();
if (millis() - lastMessageTime >= 30000) {

// there have been no messages for 30 secs
  // do something
}

But what tells me when the last message has been received and how to use such a thing? I think i need a trigger that i can use in order to tell me when a message is received.
for example: that the RX light on the arduino will blink when something is received or a function within the NRF that tells me as such. :confused:

U5ED:
But what tells me when the last message has been received

Every message is the last message until another message arrives. I was not using the name lastMessage to mean the final message.

Maybe mostRecentMessage would be a better name for it.

...R

Ah i understand now! thank you lots!