nRF24L01 radio.available() always true

I have a few bare-bones POCs for communicating via nRF24L01 and I'm now at the point of attempting to move the code into a header file. Of note... I am brand new to c++, learning that and the arduino platform together, so I think my problem is coding related, specifically to me not understanding scope or memory (coming from Java and Python)?

With the project outlined below, I consistently output

Received Long Range Data :

with no corresponding data. My code is structured in a way that it should only print when radio.available returns true, which implies available is always returning true.

Hub.h

class LongRangeListener
{
private:
  RF24 radio;
  const byte address[5] = {'R', 'x', 'A', 'A', 'A'};
  static const uint8_t bufLength = 35;
  char longRangeBuf[bufLength];

public:
  LongRangeListener(const byte cePin, const byte csnPin);
  void start();
  char *listen();
};

Hub.cpp

#include <Arduino.h>
#include <Hub.h>

// Long Range Listener
LongRangeListener::LongRangeListener(const byte cePin, const byte csnPin) : radio(cePin, csnPin) {}

void LongRangeListener::start()
{
  Serial.println("Starting Long Range Listener");

  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.openReadingPipe(1, address);
  radio.startListening();

  Serial.println("Long Range Listener started.");
}

char *LongRangeListener::listen()
{
  if (radio.available())
  {
    radio.read(&longRangeBuf, sizeof(longRangeBuf));
    Serial.print("Received Long Range Data : ");
    Serial.println(longRangeBuf);
    return longRangeBuf;
  }
  return nullptr;
}

receiver.ino

#include <Hub.h>

#define CE_PIN 9
#define CSN_PIN 10

LongRangeListener* listener;

void setup() {
  Serial.begin(9600);
  Serial.println("Setting up board");

  listener = &LongRangeListener(CE_PIN, CSN_PIN);
  (*listener).start();

  Serial.println("Board setup complete");
}

void loop() {
  (*listener).listen();
}

However, when I use my POC code below, it works as expected, outputting the message from the transmitter. So I feel confident I have my module wired up correctly.

simplerx.ino

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

#define CE_PIN   9
#define CSN_PIN 10

const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN);

char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;

//===========

void setup() {

    Serial.begin(9600);

    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

//=============

void loop() {
    getData();
    showData();
}

//==============

void getData() {
    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
        newData = false;
    }
}

I am really hoping for some help understanding why my project with a header file fails to run correctly when my single file ino runs correctly. It is feeling like I am making a mistake in instantiating or storing the radio, but I am at a loss. Any assistance is greatly appreciated.

My problem was a problem with the lifetime of the objects I was creating. I understand that now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.