nRF24l01, Arduino and Pi - delay

Hi!

I've got a problem with setting delays between sending two packets from Arduino (transceiver) to Pi (receiver) using nRF24l01 module to communicate. Here are codes for both devices:

Arudino:

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

int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void)
{
  Serial.begin(9600);
  // WiFi
      radio.begin();
      radio.setRetries(15,15);
      radio.powerUp();
      radio.openWritingPipe(pipe); 
}

void loop(void)
{  
    msg[0] = 1;
   // radio.powerUp(); 
    // delay(1000);
 
    if(radio.write(msg, sizeof(int)))
    {
        Serial.println(msg[0]); 
    }
    else
    {
        Serial.println("Failed!"); 
    }
}

Pi:

#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <linux/spi/spidev.h>
#include "/home/pi/RF24/librf24-rpi/librf24/RF24.h"

using namespace std;

RF24 radio("/dev/spidev0.0", 800000, 25);
const int role_pin = 7;
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xE8E8F0F0E1LL };

void setup()
{
	//Prepare the radio module
	radio.begin();
	radio.setRetries(15, 15);
	radio.setAutoAck(true);
	radio.setChannel(0x4c);
	radio.setPALevel(RF24_PA_MAX);
	radio.setPALevel(RF24_PA_MAX);
	
	radio.openReadingPipe(1, pipes[1]);
	radio.powerUp();
	radio.startListening();
	radio.printDetails();
}

void loop()
{
	printf("Listening...\n");
	while(1)
	{
		int x;
		bool done = false;
		if(radio.available())
		{			
			while(!done)
			{
				done = radio.read(&x, sizeof(int));
				printf("Received: %i\r\n", x);			
			}
		}
	}
}

int main(int argc, char ** argv)
{
	
	setup();
	loop();	
}

The problem is the commented delay(). If I don't add it before or just after radio.write(), module sends messages correctly - but too fast. When I add the delay, module sends only one message and all the rest are failed. I discovered that calling radioPowerUp() just before the delay solves the problem, but I wouldn't say that this is the best sollution since I don't really know why it happens and because it takes some computational resources.

Does anyone know why there is such problem? Is there any natural way to set delays between sending two packets?

Please, help me.

Is there any natural way to set delays between sending two packets?

Yep. The blink without delay example shows how.

I've modified the code as shown below, but it doesn't help. Still the same problem.

  unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval)
    {
      previousMillis = currentMillis;
      if(radio.write(msg, sizeof(int)))
      {
          Serial.println(msg[0]); 
      }
      else
      {
          Serial.println("Failed!"); 
      }       
    }

but it doesn't help.

Nor does posting snippets.

I didn't change anything beside this. But ok, I can write the whole code again:

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

int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;

long previousMillis = 0;        
long interval = 1000; // ms

void setup(void)
{
  Serial.begin(9600);

  // WiFi
      radio.begin();
      radio.setRetries(15,15);
      radio.powerUp();
      radio.openWritingPipe(pipe); 
}

void loop(void)
{  
    msg[0] = 1;

    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval)
    {
      previousMillis = currentMillis;
      
      if(radio.write(msg, sizeof(int)))
      {
          Serial.println(msg[0]); 
      }
      else
      {
          Serial.println("Failed!"); 
      }       
    }    
}

When I add the delay, module sends only one message and all the rest are failed.

What does "all the rest are failed" mean?

Here:

if(radio.write(msg, sizeof(int)))
      {
          Serial.println(msg[0]); 
      }
      else
      {
          Serial.println("Failed!"); 
      }

It looks like the first packet is sent - radio.write() returns true and my Pi receives a packet - and for all the rest (after the first delay) radio.write() is false - and Pi doesn't receive anything.

Then, you should be debugging the radio.write() method, to see why it is failing.

I hoped to avoid this :slight_smile: I am not an expert in low-level programming . If there is no solution with using only the given methods, I will leave it with calling powerUp() method. It's very short function and the WiFi module works well. But thank you for your help.

I don't see anything obviously wrong in your code, but you don't call setPALevel(), and I wouldn't expect to need to call powerUp() to get reliable transmission.

I suggest you start with the examples that come with the RF24 library and get them working reliably to confirm you have the hardware set up correctly.

One feature I've noticed in the RF24 library is that write() will often report failure when it actually succeeded - one of the other forum members pointed out that the library clears the receive buffer after the transmit instead of before, which might account for that - so check what you're actually receiving and don't take the return value from write() at face value.