This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

TO find out the number of retries with nRF24L01+

Hi, I have written a simple program to calculate the number of retries with nRF24L01+. On transmitter side, maximum number of allowed retries is set at 1. The transmitter sends temperature values to the receiver. Based on the response of the receiver, it calculates the packet success rate and average number of retries. However, when packet transmission fails the number of retries remains at 0 and do not change. The code has used RF24L01 library functions.

Transmitter side code:

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

 
//int outputpin= 0;//this sets the ground pin to LOW and the input voltage pin to high
 
RF24 radio(9,10);
const int analogInPin = A1;  // Analog input pin that the potentiometer is attached to

int pinReading;                 // value read from the analogue pin
// These constants are used to make the sketch easier to read
const float Vref   = 1.1;       // (V)    reference voltage used
const float offset = 0.4;       // (V)    output at 0 °C
const float slope  = 0.0195;    // (V/°C) slope of sensor response curve
float temperature;              // (°C)   calculated temperature
int DELAY =15;
int COUNT =1;
int buf;
const uint64_t pipe = 0xE8E8F0F0E1LL;
int success =0;
int total =0;
int total_retry =0;
void setup(void)
{
  Serial.begin(9600);
  radio.begin();
//  radio.setChannel(90);
  radio.openWritingPipe(pipe);
  radio.setPALevel(RF24_PA_HIGH);
  radio.setDataRate(RF24_250KBPS);
  radio.setRetries(DELAY, COUNT);
 analogReference(INTERNAL); //for UNO
//analogReference(INTERNAL1V1);
  delay(10);
 radio.powerUp();
 printf_begin();
 radio.printDetails();
 Serial.print("#S|LOGFIXED|[");
 Serial.print("------Fixed transmission at HIGH (-6 dBm) power Level ----------");
 Serial.println("]#");
 
}
 
void loop(void)
{
total++;
 pinReading = analogRead(analogInPin);  // read the analog in value:
 temperature = (((float)pinReading * (Vref/1024))-offset)/slope; // convert value read to °C
 bool ok =radio.write(&temperature, sizeof(temperature));

if(ok){
Serial.println("Success");
success ++;

}
else {
Serial.println("failure");
}
buf = radio.read_register(OBSERVE_TX);
//int lost_pack = (buf & 240) >>4;
//int ret_pack = (buf & 15);
int ret_pack = (buf & 0X0F);
total_retry = ret_pack + total_retry;




Serial.println(ret_pack);

{
int c =c +1;
lost_pack = c*15;
Serial.print("#S|LOGTEST|[");
Serial.print(lost_pack);
Serial.println("]#");
buf = (buf | 0x00);
radio.write_register(OBSERVE_TX,buf);
buf = radio.read_register(OBSERVE_TX);

//}*/
//Serial.println(P);
if(total %100 ==0)
{
   float PSR = 100*float(success)/float(total);
   float AvgRet = float(total_retry)/float(total);
   Serial.print("#S|LOGFIXED|[");
   Serial.print("Success Rate:");
   Serial.print(PSR,2);
   Serial.print(" %");
   Serial.print("Avg Retry :");
   Serial.print(AvgRet,4);
   Serial.println("]#");
  
  success =0;
  total =0;
  total_retry =0;
}

  delay(1000);
 
 
}

Receiver side code:


#include <SPI.h>
#include <nRF24L01.h>
#include "RF24.h"
#include "printf.h"
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
float celsius;
void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  
 //radio.setChannel(90);
  radio.setPALevel(RF24_PA_MAX);    
  radio.setDataRate(RF24_250KBPS);  
  radio.openReadingPipe(1,pipe);
  radio.startListening();
printf_begin();
radio.printDetails();
}
 
void loop(void)
{
  if ( radio.available() )
  {
     
    bool done = false;
    while (!done)
    {
      // Fetch the payload, and see if this was the last one.
     // done = radio.read( joystick, sizeof(joystick) );
    done= radio.read( &celsius, sizeof(celsius));
    Serial.println(celsius);
 
    }

  }
 
  {
   Serial.println("No radio available");
 }*/
    
}
Parents Reply Children
No Data
Related