NRF24L01 connection issues

Hi Everyone

I have made code to transmit data from transmitter to receiver uisng NRF24L01.

Connection, acknowledgment everything works fine. but after few hours i can notice that there was connection lost for few minutes and reconnected again.

I have attached 10microFarad capacitor to both nrf24l01. At transmitter side i am using simple NRF24L01 without antenna and receiver side i am using nrf with antenna.

Receiver is placed inside truck cabin and transmitter is placed out side the cabin near. distance between then is around 10 meters.

can some one please check the code and help me how to solve this issue.

Thanks in advance

Transmitter code

#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <SPI.h>
#include "RF24.h"
#define TRANSMITER_NUM    0 // or 1
#define TILT_PIN          3
const uint64_t talking_pipes[2] = { 0xF0F0F0F0D1LL, 0xF0F0F0F0C2LL };
const uint64_t listening_pipes[2] = { 0x3A3A3A3AD1LL, 0x3A3A3A3AC2LL };
struct DataPacket {
  int voltage;
  bool tilt;
};
bool volatile interruptTriggered = false;
RF24 radio(7,8);
void TiltPinInterrupt(void)
{
  sleep_disable();
  detachInterrupt(digitalPinToInterrupt(TILT_PIN));

  interruptTriggered = true;
}

void watchdogSetup(void)
{
  wdt_reset();
  
  WDTCSR = bit (WDCE) | bit (WDE);
 
  WDTCSR = bit (WDCE) | bit (WDE) | bit (WDIE) | bit (WDP3) | bit (WDP0);
}

void disableHardware() {

  radio.powerDown();          
  power_spi_disable();   
  power_timer0_disable();
  power_timer1_disable(); 
  power_timer2_disable(); 
  power_twi_disable();    
 power_usart0_disable(); 
ADCSRA &= ~(1 << ADEN); 
  power_adc_disable();   
}

void enableHardware() {
  power_spi_enable();    
  power_timer0_enable(); 
  power_timer1_enable(); 
  power_timer2_enable();  
  power_twi_enable();   
#ifdef SERIAL_PRINT
  power_usart0_enable(); 
#endif
  power_adc_enable();    
  radio.powerUp();                             
}

void enterSleep(void)
{ADCSRA = 0; 
sleep_enable();
disableHardware();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli(); 
noInterrupts();
MCUSR = 0;
watchdogSetup();
attachInterrupt(digitalPinToInterrupt(TILT_PIN), TiltPinInterrupt, CHANGE);
sei();
interrupts();
sleep_cpu();
sleep_disable();
enableHardware();
}

void setup()
{
Serial.begin(9600);
pinMode(TILT_PIN, INPUT);
radio.begin();
radio.setAutoAck(false); 
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(108);
  radio.openWritingPipe(talking_pipes[TRANSMITER_NUM]);
  radio.openReadingPipe(1,listening_pipes[TRANSMITER_NUM]);
  radio.startListening();
  getBandgap();
#ifdef SERIAL_PRINT
  Serial.println("Initialisation complete.");
#endif
}
ISR (WDT_vect) 
{
   wdt_disable();  
}  
const long InternalReferenceVoltage = 1100;  
int getBandgap () 
{
   ADMUX = bit (REFS0) | bit (MUX3) | bit (MUX2) | bit (MUX1);
   ADCSRA |= bit( ADSC ); 
   while (ADCSRA & bit (ADSC))
     { }  
   ADCSRA |= bit( ADSC );  
   while (ADCSRA & bit (ADSC))
     { } 
   int results = (((InternalReferenceVoltage * 1024) / ADC) + 5) / 10; 
   return results;
} 
int cnt=0;
void loop()
{
  bool sendValue = false;
  DataPacket data; 
  cnt++;
  if (cnt >=3 ) {
    cnt = 0;
    sendValue = true;
  }
  if (interruptTriggered) {
  sendValue = true;
    interruptTriggered = false;
  }
 if (sendValue) {
    cnt = 0;
    data.voltage = getBandgap();
    data.tilt = digitalRead(TILT_PIN);
 int resend = 0;
    do {
      resend++;
      radio.stopListening();
      if (radio.write( &data, sizeof(data) )) {
        radio.startListening();
        unsigned long started_waiting_at = millis();
        bool timeout = false;
        while ( ! radio.available() && ! timeout )
          if (millis() - started_waiting_at > 250 )
            timeout = true;
        if ( timeout )
        {
#ifdef SERIAL_PRINT
          Serial.println("Failed, response timed out.\n\r");
#endif
        }
        else
        {
          unsigned long dummy;
          radio.read( &dummy, sizeof(unsigned long) );
#ifdef SERIAL_PRINT
          Serial.println("Got response");
#endif
          resend = 0;
        }
      } else {
#ifdef SERIAL_PRINT
        Serial.println("Error sending values");
#endif
      }
    } while ((resend > 0) && (resend <=3));
  }
#ifdef SERIAL_PRINT
  Serial.println(data.voltage, DEC);
  Serial.println(data.tilt, DEC);

  delay(100);
#endif
  enterSleep();
}

Receiver code

#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <SPI.h>
#include "RF24.h"
#define LED_TILT      3
#define LED_VOLTAGE   4
#define LOW_BATTERY   260
struct DataPacket {
  int voltage;
  bool tilt;
};
const uint64_t talking_pipes[2] = { 0xF0F0F0F0D1LL, 0xF0F0F0F0C2LL };
const uint64_t listening_pipes[2] = { 0x3A3A3A3AD1LL, 0x3A3A3A3AC2LL };
DataPacket data[2];
unsigned long lastMilis[2];
RF24 radio(7,8);
void watchdogSetup(void)
{
  wdt_reset();
  WDTCSR = bit (WDCE) | bit (WDE);
  WDTCSR = bit (WDCE) | bit (WDE) | bit (WDIE) | bit (WDP3) | bit (WDP0);    // set WDIE, and 8 seconds delay
}

void setup()
{
  Serial.begin(9600);
  pinMode(LED_TILT, OUTPUT);
  pinMode(LED_VOLTAGE, OUTPUT);
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(0x66);
  radio.openReadingPipe(1,talking_pipes[0]);
  radio.openReadingPipe(2,talking_pipes[1]);
  radio.startListening();
  data[0].voltage = 500;
  data[0].tilt = 1;
  data[1].voltage = 500;
  data[1].tilt = 1;
  lastMilis[0] = 0;
  lastMilis[1] = 0;
  Serial.println("Initialisation complete.");
}
unsigned long previousMillis = 0; 
long interval = 1000; 
void loop()
{
  
  uint8_t pipe_num;
  if ( radio.available(&pipe_num) )
  {
    unsigned long got_time;
    bool done = false;
      Serial.println(pipe_num, DEC);
      radio.read(&data[pipe_num - 1], sizeof(DataPacket) );
      lastMilis[pipe_num - 1] = millis();
      Serial.println(data[pipe_num - 1].voltage, DEC);
      Serial.println(data[pipe_num - 1].tilt, DEC);
    radio.stopListening();
    radio.openWritingPipe(listening_pipes[pipe_num-1]);
    radio.write( &got_time, sizeof(unsigned long) );
    printf("Sent response to %04x.\n\r",pipe_num);
    radio.startListening();
  }
  unsigned long currentMillis = millis();
  if (currentMillis > (lastMilis[0] + 30000)) {
    data[0].tilt = true;
    data[0].voltage = 0;
  }
  if (currentMillis > (lastMilis[1] + 30000)) {
    data[1].tilt = true;
    data[1].voltage = 0;
  }
  if ((!data[0].tilt) || (!data[1].tilt))
    digitalWrite(LED_TILT, HIGH);
  else
    digitalWrite(LED_TILT, LOW);
  if ((data[0].voltage >= LOW_BATTERY) && (data[1].voltage >= LOW_BATTERY))
    digitalWrite(LED_VOLTAGE, LOW);
  else 
    if ((data[0].voltage < LOW_BATTERY) && (data[1].voltage < LOW_BATTERY))
      digitalWrite(LED_VOLTAGE, HIGH);
    else {
      if (data[0].voltage < LOW_BATTERY)
        interval = 1000;
      else
        interval = 2000;
      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
        if (digitalRead(LED_VOLTAGE) == LOW) {
          digitalWrite(LED_VOLTAGE, HIGH);
        } else {
          digitalWrite(LED_VOLTAGE, LOW);
        }
    }
  }
}

I am sorry with the receiver code. there is one small correction with channel number.

radio.setChannel(108); on both transmitter and receiver radio channel is same

Better to use the adaptor designed for the units, both on tx and rx.
Didn,t look at your code as it is usual to use tried and true from for example, Robin2 to make sure things work as expected.
10 metres might just need the high power version as I have found previously.

bmg1234:
Connection, acknowledgment everything works fine. but after few hours i can notice that there was connection lost for few minutes and reconnected again.

You have two fairly complex programs. I suggest you start debugging your nRF24 problem with something much simpler. Have a look at the examples in this Simple nRF24L01+ Tutorial. The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with.

If there is no problem with the very simple code then piece by piece add the extra capabilities, testing at each stage.

I notice that both programs have "sleeping". Is it possible that your problem arises when the sleep gets out of sync?

...R

Robin2:
You have two fairly complex programs. I suggest you start debugging your nRF24 problem with something much simpler. Have a look at the examples in this Simple nRF24L01+ Tutorial. The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with.

If there is no problem with the very simple code then piece by piece add the extra capabilities, testing at each stage.

I notice that both programs have "sleeping". Is it possible that your problem arises when the sleep gets out of sync?

...R

Thank you Robin for your response.

Only transmitter will be in sleep if there is any interrupt it will wakeup and send data, if there is no interrupt every 24 seconds device sends the bandgap and interrupt status. this i have made to check the connection.

device was working fine for 4days later after few hours connection was lost and device automatically reconnected.

But in some cases device did not connect for 5 hours.

What will the solution to make device reconnect. my transmitter will be sealed in the plastic box which cannot open.

bluejets:
Better to use the adaptor designed for the units, both on tx and rx.
Didn,t look at your code as it is usual to use tried and true from for example, Robin2 to make sure things work as expected.
10 metres might just need the high power version as I have found previously.

what do you mean by adapter design? could you please explain me in detail

bmg1234:
What will the solution to make device reconnect. my transmitter will be sealed in the plastic box which cannot open.

Without doing extensive testing - starting as I suggested in Reply #3 - I have no way of knowing. Sorry.

The solution might be trivially simple but it could take 2 weeks to identify the problem.

...R