RF24 library

Hello. Im looking for precision concerning radio.available()
In a doc, it says that “Check whether there are bytes available to be read”.

On the program I am currently trying, radio.available is always “false”, making communication impossible.

// SlaveSwapRoles

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


#define CE_PIN   9
#define CSN_PIN 10

const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataReceived[10]; // must match dataToSend in master
int replyData[2] = {109, -4000}; // the two values to be sent to the master
bool newData = false;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second


void setup() {

    Serial.begin(9600);

    Serial.println("SlaveSwapRoles Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
    radio.openReadingPipe(1, slaveAddress);

    radio.setRetries(3,5); // delay, count
    radio.startListening();

}

//====================

void loop() {
    getData();
    showData();
    send();
}

//====================

void send() {
    if (newData == true) {
        radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyData, sizeof(replyData) );
        radio.startListening();

        Serial.print("Reply Sent ");
        Serial.print(replyData[0]);
        Serial.print(", ");
        Serial.println(replyData[1]);

        if (rslt) {
            Serial.println("Acknowledge Received");
            updateReplyData();
        }
        else {
            Serial.println("Tx failed");
        }
        Serial.println();
        newData = false;
    }
}

//================

void getData() {
    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
}

//================

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
    }
}

//================

void updateReplyData() {
    replyData[0] -= 1;
    replyData[1] -= 1;
    if (replyData[0] < 100) {
        replyData[0] = 109;
    }
    if (replyData[1] < -4009) {
        replyData[1] = -4000;
    }
}

Try the example programs in Simple nRF24L01+ 2.4GHz transceiver demo which are known to work. Take particular note of the library used

Im literally using robin's program already, what you are seeing is his "SlaveSwapRoles.ino"

What about MasterSwapRoles ?

Do the SimpleRx and SimpleTx programmes work for you ?

yeah, I asked for help with it on another post, and robin helped me make simplerx and simpletx work.

here is the master

// MasterSwapRoles

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


#define CE_PIN   9
#define CSN_PIN 10

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
const byte masterAddress[5] = {'T', 'X', 'a', 'a', 'a'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataToSend[10] = "Message 0";
char txNum = '0';
int dataReceived[2]; // to hold the data from the slave - must match replyData[] in the slave
bool newData = false;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

//============

void setup() {

  Serial.begin(9600);

  Serial.println("MasterSwapRoles Starting");

  radio.begin();
  radio.setDataRate( RF24_250KBPS );

  radio.openWritingPipe(slaveAddress);
  radio.openReadingPipe(1, masterAddress);

  radio.setRetries(3, 5); // delay, count
  send(); // to get things started
  prevMillis = millis(); // set clock
}

//=============

void loop() {
  currentMillis = millis();
  if (currentMillis - prevMillis >= txIntervalMillis) {
    send();
    prevMillis = millis();
  }
  getData();
  showData();
}

//====================

void send() {

  radio.stopListening();
  bool rslt;
  rslt = radio.write( &dataToSend, sizeof(dataToSend) );
  radio.startListening();
  Serial.print("Data Sent ");
  Serial.print(dataToSend);
  if (rslt) {
    Serial.println("  Acknowledge received");
    updateMessage();
  }
  else {
    Serial.println("  Tx failed");
  }
}

//================

void getData() {

  if ( radio.available() ) {
    radio.read( &dataReceived, sizeof(dataReceived) );
    newData = true;
  }
}

//================

void showData() {
  if (newData == true) {
    Serial.print("Data received ");
    Serial.print(dataReceived[0]);
    Serial.print(", ");
    Serial.println(dataReceived[1]);
    Serial.println();
    newData = false;
  }
}

//================

void updateMessage() {
  // so you can see that new data is being sent
  txNum += 1;
  if (txNum > '9') {
    txNum = '0';
  }
  dataToSend[8] = txNum;
}

If the simple programs worked and you have not changed anything then we know that the hardware and wiring is OK but I have never tried the SwapRoles programs

As you will be aware, Robin is very active here and will be able to offer more advice

Ok, I worked a lot to understand how work simple transmission of a variable. I hope switching role isn't that hard XD

LunaticCat:
On the program I am currently trying, radio.available is always "false", making communication impossible.

I realise the following may sound unhelpful, but it is not intended to be ...

How do you know that it is always false?

In case you have not tried, it is a good idea to de-power and re-power the Arduino and its nRF24 immediately after uploading a program. De-powering is required to get the nRF24 to reset.

...R

In the program there is a IF condition that check if radio.available is true (like this one below). I just put a serial.print in it to see if the program was getting there, but it never did.

  if ( radio.available() ) {
    radio.read( &dataReceived, sizeof(dataReceived) );
    newData = true;
    Serial.println("check");
  }

LunaticCat:
I just put a serial.print in it to see if the program was getting there, but it never did.

That does not prove that there is a problem with the receiving code. Also, your line of code is superfluous because if data was received the code in showData() would print.

More seriously, the fact that you changed the program leads me to think that the programs posted in your Original Reply and in Reply #4 are not actually the programs that YOU have uploaded to your Arduinos. Please post the programs that you are actually using together with a sample of the output from each of them.

You said that you have been able to get the SimpleTx and SimpleRx programs working. I suggest you get those working again and then without making ANY changes to the hardware try the SwapRoles programs from my Tutorial without ANY changes to the code.

...R

for now simple tx and simple rx work fine, I even did a wireless transfer to light up a led with it ^^

when I use YOUR programs, I get from Rx monitor :

Data received Message 0
Reply Sent 109, -4000
Tx failed

Data received Message 0
Reply Sent 109, -4000
Tx failed

Data received Message 0
Reply Sent 109, -4000
Tx failed

Data received Message 0
Reply Sent 109, -4000
Tx failed

Data received Message 0
Reply Sent 109, -4000
Tx failed

and on the Rx monitor :

Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed

When I saw that, I remembered how you solved this problem on my previous post, so I added

radio.setAutoAck(true);

to your codes, that becomes :

Rx:

// SlaveSwapRoles

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


#define CE_PIN   9
#define CSN_PIN 10

const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataReceived[10]; // must match dataToSend in master
int replyData[2] = {109, -4000}; // the two values to be sent to the master
bool newData = false;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second


void setup() {

    Serial.begin(9600);

    Serial.println("SlaveSwapRoles Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
    radio.openReadingPipe(1, slaveAddress);
    radio.setAutoAck(true);
    radio.setRetries(3,5); // delay, count
    radio.startListening();

}

//====================

void loop() {
    getData();
    showData();
    send();
}

//====================

void send() {
    if (newData == true) {
        radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyData, sizeof(replyData) );
        radio.startListening();

        Serial.print("Reply Sent ");
        Serial.print(replyData[0]);
        Serial.print(", ");
        Serial.println(replyData[1]);

        if (rslt) {
            Serial.println("Acknowledge Received");
            updateReplyData();
        }
        else {
            Serial.println("Tx failed");
        }
        Serial.println();
        newData = false;
    }
}

//================

void getData() {

    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
}

//================

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
    }
}

//================

void updateReplyData() {
    replyData[0] -= 1;
    replyData[1] -= 1;
    if (replyData[0] < 100) {
        replyData[0] = 109;
    }
    if (replyData[1] < -4009) {
        replyData[1] = -4000;
    }
}

Mx :

// SlaveSwapRoles

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


#define CE_PIN   9
#define CSN_PIN 10

const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataReceived[10]; // must match dataToSend in master
int replyData[2] = {109, -4000}; // the two values to be sent to the master
bool newData = false;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second


void setup() {

    Serial.begin(9600);

    Serial.println("SlaveSwapRoles Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
    radio.openReadingPipe(1, slaveAddress);
    radio.setAutoAck(true);
    radio.setRetries(3,5); // delay, count
    radio.startListening();

}

//====================

void loop() {
    getData();
    showData();
    send();
}

//====================

void send() {
    if (newData == true) {
        radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyData, sizeof(replyData) );
        radio.startListening();

        Serial.print("Reply Sent ");
        Serial.print(replyData[0]);
        Serial.print(", ");
        Serial.println(replyData[1]);

        if (rslt) {
            Serial.println("Acknowledge Received");
            updateReplyData();
        }
        else {
            Serial.println("Tx failed");
        }
        Serial.println();
        newData = false;
    }
}

//================

void getData() {

    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
}

//================

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
    }
}

//================

void updateReplyData() {
    replyData[0] -= 1;
    replyData[1] -= 1;
    if (replyData[0] < 100) {
        replyData[0] = 109;
    }
    if (replyData[1] < -4009) {
        replyData[1] = -4000;
    }
}

but then rx get worse and only say :

SlaveSwapRoles Starting

ok, I reupload my simplerx and simpletx and rx don't show anything too --'
(and the modules are working, I checked with your test program)

LunaticCat:
ok, I reupload my simplerx and simpletx and rx don't show anything too --'
(and the modules are working, I checked with your test program)

Does that mean the problem has been solved? Or is there still something you need help with?

...R