0

I have a pair of nRF24L01s connected to a pair of Arduino Unos and I'm using the RF24 library from here: RF24 library. When I run the pingpair example sketch, the radios transmit and receive back and forth just fine. So I tried to program my own very simple example but I'm running into a problem where the first message transmits successfully (and is received successfully), however, subsequent messages fail to send. The call to radio.write() returns false.

I'm using the following code on the 'sender':

RF24 radio(9,10);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
uint8_t payload[1];

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.setPayloadSize(1);
  radio.openWritingPipe(pipes[0]);
}

void loop() {
  payload[0] = 1;
  bool ok = radio.write(payload, 1);
  Serial.println("Status: " + (String) ok);
  delay(200);
}

And the following on the 'receiver':

RF24 radio(9,10);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
uint8_t payload[1];

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.setPayloadSize(1);
  radio.openReadingPipe(1, pipes[0]);
  radio.startListening();
  Serial.println("Listening: " );
}

void loop() {
  if (radio.available()) {
    bool done = false;
    while (!done) {
      done = radio.read(payload, 1);
      Serial.println("Got payload: " + (String) payload[0]);
    }
  }
}

I'm guessing the problem has to be with my code because the example sketches work, but I can't figure out what the problem is. Again to summarize, when I reset the Arduino, the first call to radio.write() returns true and the message is transmitted. After that, subsequent calls to radio.write() return false.

Also note that I did try adding a 10uF capacitor across +3.3V and Ground but it did not help.

Thanks for any help.

4
  • Do you need to reset both arduinos to make it work or just one? Commented Mar 11, 2015 at 6:03
  • Which library are you using?
    – Gerben
    Commented Mar 11, 2015 at 16:19
  • 1
    I agree with the answer below to use several Serial.println to figure out where it gets stuck. In the library config file (RF24_config.h) there is a #undefine at line 32 - change that to #define so you get the serial debug strings from the library as well so you can try to figure out where the error is happening. If you still can't figure it out then post the debug output here.
    – rslite
    Commented May 29, 2015 at 2:20
  • 1
    What version of the library are you using? Please make sure you are using the maintained fork by TMRh20. tmrh20.github.io/RF24
    – Avamander
    Commented Sep 26, 2015 at 18:29

3 Answers 3

1

Comparing your rx against the example, i would suggest not having the while done statement. You could also throw in a few serial.write debug lines in both sketches, to log different parts of the code, to find where its getting stuck.

1

I was just having this issue. You need to be able to read and write on both ends. You send up sending an ack package back to the sending arduino like in the test sketch.

You ripped that part out.

You need to read back through the example code and line by line understand what the data is doing and why it's doing it that way before you can start using rf24.

The short of it is that the sender will send data and then finish and wait for a response packet before sending the next one. The reciever works much the same way except it always will be listening for incoming data until the sender message ends. Then it in turn will send a reply packet saying that it recieved the first packet.

Make sure that the reciever isn't looping waiting for packets. It should only respond once it recieves something. The sender should have a timeout on it so that it knows that it's messages are failing. This is also incredibly important for debugging so you can see who failed and why.

This is basically tcp at work.

You can also use noack and that's called udp

0

You should open a reading pipe on the transmitter end because when the reader receives your message it will reply with an acknowledge packet, which is expected by the writer in order to confirm the packet has been delivered. With your current setup, your writer is sending out a packet and expecting to receive the response on an address which is by default configured as a broadcast address.

On the sender side, you also need to start listening after sending the packet and stop listening before sending the new packet.

The transmission protocol works like this:

  1. the sender sends a packet in the air and starts waiting for the ack
  2. all listeners receive the packet, no matter their receive address
  3. each listener check if the sender address matches with any configured reading pipe address
    1. if the address matches then the listener sends back an acknowledge packet to inform the transmission has been accomplished
    2. if the address does not match it discards the packet
  4. if the sender receives at least one acknowledge packet then it marks the packet as being sent successfully
  5. if the sender doesn't receive the ack packet within the configured time to live then sends the packet again until the retry count has not been reached
  6. if the retry count has been reached then it marks the transmission as failed

So change your sender setup() and loop() functions above with:

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.setPayloadSize(1);
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1, pipes[1]); // add this
}

void loop() {
  radio.stopListening(); // add this
  payload[0] = 1;
  bool ok = radio.write(payload, 1);
  radio.startListening(); // add this
  Serial.println("Status: " + (String) ok);
  delay(200);
}

You might also want to set the writing address on the receiver if you plan to use more than 2 radios, otherwise, you will not be able to distinguish who has acknowledged different packets:

radio.openWritingPipe(pipes[1]); // add this to receiver `setup()`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.