NRF24 multiple transmitters and 1 Receiver problems

Hello all,

I have been scouring this form for a solution and there are a few other threads similar, but I could not get their given solutions to work with my code / libraries. I am trying to have 2 different NRF transmitters send data to a single receiver. I can get each transmitter to send data separately, but when I have the two sending data simultaneously, using different address and reading pipes, I can only read data from one of the transmitters. To complicate the matter further, when i have the code print out which pipe is is receiving data from, it lists the wrong pipe. I posted the code for the two transmitters and receiver.
Transmitter 1:

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

RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_HIGH);
  radio.stopListening();

}

void loop() {

  float data[2]={2.22,3.33};
  radio.write(&data,sizeof(data));
  

  delay(5000);

}

Transmitter 2:

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

RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00002";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_HIGH);
  radio.stopListening();

}

void loop() {

  float data[2]={8.88,9.99};
  radio.write(&data,sizeof(data));
  

  delay(1000);

}

Receiver code:

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

const int ledpin = 6;

RF24 radio(7, 8); // CE, CSN
const byte address[][6] = {"00001","00002"};

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1, address[0]);
  radio.openReadingPipe(2, address[1]);
  radio.setPALevel(RF24_PA_HIGH);
  radio.startListening();

  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin,LOW);

}

void loop() {
  byte pipe;
  if (radio.available(&pipe)){
    digitalWrite(ledpin,HIGH);
    float data[2]={0,0};
    radio.read(&data,sizeof(data));
    Serial.println(pipe);
    Serial.println(data[0]);
    Serial.println(data[1]);
    Serial.println();
    delay(100);
   }
 else{
  digitalWrite(ledpin,LOW);
 }

}

The serial output is:
2
2.22
3.33
and it repeats every 5 seconds.

Any tips, suggestions, or help is much appreciated. I am very new to all this, so please bare with me if what I ask seems simple.

As a reference, i tried to model my code off of this. You can find which library i am using from the same site too.

Thanks in advance!
Sami

"DUH" comes to mind. Have you read the data sheet on the controller. Specifically states the problem you have is the result of the impossible.

Paul

Paul_KD7HB:
"DUH" comes to mind. Have you read the data sheet on the controller. Specifically states the problem you have is the result of the impossible.

What can you see that I can't see?

...R

@samihawasli, first, have a look at this Simple nRF24L01+ Tutorial - the examples do work. It includes an example of a master communicating with 2 slaves and that could easily be extended to several slaves.

My master-multiple-slave example is probably not suitable if your slaves need to be asleep for most of the time to save energy. In that case you will need to devise a system that prevents more than one slave from talking at any one time. Even though an nRF24 has 6 pipes it has only one receiver and messages only get put into pipes after they are received successfully.

Devising a scheme so that the slaves do not interfere with each other can require some careful thinking on your part. For example the use of extra retries for failed communication may actually make things worse. Consequently, if there is no requirement to save energy I suggest you use the system in my tutorial.

...R

Robin2:
Even though an nRF24 has 6 pipes it has only one receiver and messages only get put into pipes after they are received successfully.

I looked through that example before originally posting. Sadly my ignorance is a limiting factor in completely understanding what the code is doing. But your explanation above made me try something a bit different. In the main loop i simply cycle through the two address. This may not be perfect, but it seems to work. Can you see anything inherently wrong with my algorithm?

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

const int ledpin = 6;

RF24 radio(7, 8); // CE, CSN
const byte address[][6] = {"00001","00002"};

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1, address[0]);
  //radio.openReadingPipe(1, address[1]);
  radio.setPALevel(RF24_PA_HIGH);
  radio.startListening();

  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin,LOW);

}

void loop() {
 radio.openReadingPipe(1, address[0]);
 while (!radio.available()){
    //Serial.println("Waiting");
  }
  Serial.println("Address 1");
  digitalWrite(ledpin,HIGH);
  float data[2]={0,0};
  radio.read(&data,sizeof(data));
  Serial.println(data[0]);
  Serial.println(data[1]);
  Serial.println();
  delay(100);
  digitalWrite(ledpin,LOW);

  radio.openReadingPipe(1, address[1]);
 while (!radio.available()){
    //Serial.println("Waiting");
  }
  Serial.println("Address 2");
  digitalWrite(ledpin,HIGH);
  float data2[2]={0,0};
  radio.read(&data2,sizeof(data2));
  Serial.println(data2[0]);
  Serial.println(data2[1]);
  Serial.println();
  delay(100);
  digitalWrite(ledpin,LOW);
}

samihawasli:
Can you see anything inherently wrong with my algorithm?

Alas, yes.

How can you be sure that a slave will transmit at the time you are listening on its address.

If you want to use multiple pipes then you can listen on a number at the same time with each pipe using a separate address. It is not the listening that causes the problem, it is the sending by the slave - if two or more slaves send at the same time all the messages will be garbled. The way the pipes work is that first the message is received and checked that it is not garbage. Then it is checked to see if it has a valid address.

However IMHO for most applications there is no need to use multiple pipes. All the slaves can send to the same address and each slave includes an ID character in its message so the master knows which slave sent the message.

In the example in my tutorial the master calls each slave separately so there is no risk of two or more slaves talking at the same time.

...R

Upon further review!!! I see this problem can be automatically over come: When multiple PTXs are transmitting to a PRX, the ARD can be used to skew the auto retransmission so that they only block each other once.

By setting the automatic retransmission time-out to a different number of 250ms. increments, the message crash will be recovered from.

I have not gotten beyond reading the data sheet and receiving two boards with the devices on them. Sure are tiny!

Paul

Paul_KD7HB:
By setting the automatic retransmission time-out to a different number of 250ms. increments, the message crash will be recovered from.

I think I would prefer something more robust - the retries being decided by my program, rather than automatically.

A transmission failure could just as easily be caused by external interference and then the intended different timeouts might actually coincide.

Better still, write a program that won't have any risk of data collisions.

...R