RF24 payload problem

I'm trying to read part of the payload but I'm getting all sorts of problem. Basically it should receive a 7 digit char which the first 5 are treated separately from the last 2:

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

RF24 radio(10, 9);
const byte address[6] = "00001";

int pinoBotao = 2;
int ledPin = 3;
int ini = 0;
int secsVeri = 60;
unsigned long secsIni, msecsIni, veri;

int id = 3;

void setup() {
    Serial.begin(9600);
    pinMode(pinoBotao, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);

    radio.begin();
    radio.openWritingPipe(address);
    radio.setPALevel(RF24_PA_MIN);
    radio.setDataRate(RF24_250KBPS);
    radio.enableAckPayload();
    radio.stopListening();
    Serial.println("Connecting...");
    delay(3000);

    char ackData[8];
    char secs[6]; //FIRST PART I WANT
    char interval[3]; //SECOND PART I WANT

    for (int i = 0; i >= 0; i++) {  // endless loop
        delay(1000);

        // Serial.print("trying: ");
        // Serial.println(i);
        if (radioWrite(0, 0)) { //HELLO RECEIVER, GIVE ME MY INITIAL DATA
            ini = 1;
            memset(secs, 0, sizeof(secs));
            memset(ackData, 0, sizeof(ackData));
            memset(interval, 0, sizeof(interval));

            if (radio.isAckPayloadAvailable()) {
                radio.read(&ackData, sizeof(ackData));
                Serial.print("Answer: ");
                Serial.println(ackData); //PRINTING FINE

                // for(int i = 0; i < 5; i++)
                //   secs[i] = ackData[i];
                strncpy(secs, ackData, 5); //GETS FIRST PART I WANT. WORKING FINE

                // interval[0] = ackData[5]; //DIDNT WORK
                // interval[1] = ackData[6]; //DIDNT WORK
                strncpy(interval, ackData + 5, 2); //ALSO NOT WORKING

                secsIni = atol(secs);
                msecsIni = millis();

                secsVeri = atoi(interval);
                veri = millis();

                Serial.print("Secsini: ");
                Serial.print(secs); //WORKING FINE
                Serial.print(" Secs: ");
                Serial.print(secsVeri);
                Serial.print(" Veri: ");
                Serial.println(interval);

                if (secsIni == 0 || secsVeri == 0) continue; //TRY UNTIL CAN EXTRACT THE 2 PARTS FROM PAYLOAD

                digitalWrite(ledPin, HIGH);
                delay(1500);
                digitalWrite(ledPin, LOW);
                break;
            }
        }
    }
}


void loop() {
    //NON RELEVANT NOW
}

bool radioWrite(int n, unsigned long time) {
    char ret[8];
    memset(ret, 0, sizeof(ret));

    sprintf(ret, "%02d%i%05d", id, n, time);
    // Serial.println(ret);
    return radio.write(&ret, sizeof(ret));
}

This is what I'm getting on serial monitor

13:01:23.303 -> Answer: 6000860
13:01:23.303 -> Secsini: 60008 Secs: ����������������������� Veri: ��
13:01:24.335 -> Answer: 6000960
13:01:24.335 -> Secsini: 60009 Secs: ����������������������� Veri: ��

Answer is ok, I can get the first 5 digits but can't get the last two. Whats happening?

What is happening is that your sketch does not compile. Where, for instance, is secsIni declared? Or pinoBotao? Or ledPin? Or endereco? Or radio?

1 Like

Actually it does. I thought that declaration part was irrelevant and I omitted it. Anyway, I edited the post and put it.

No, your sketch as originally shown did not compile. If you doubt that, I invite you to try and compile what you originally posted. It fails to compile, as it was incomplete.

Your amended sketch does compile. But not without warnings.

arduino-cli compile -b arduino:avr:uno --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
already nofloat
/home/me/Documents/sketchbook/Uno_R3/test/test.ino: In function 'bool radioWrite(int, long unsigned int)':
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:91:43: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long unsigned int' [-Wformat=]
     sprintf(ret, "%02d%i%05d", id, n, time);
                                           ^
Sketch uses 5798 bytes (17%) of program storage space. Maximum is 32256 bytes.
Global variables use 294 bytes (14%) of dynamic memory, leaving 1754 bytes for local variables. Maximum is 2048 bytes.
Compilation finished successfully.

And that warning drew my eye to a bit of your code that has a subtle error. One that I'm sure you will have no trouble finding and correcting now that you know where to look.

Thanks for your help. It wasn't showing any warnings to me. I edited the preferences and it's showing now. Also the sprintf is working now without warnings but the main problem still remains :confused:

I commented this line

radio.setDataRate(RF24_250KBPS);

and it's working now. According to RF24's documentation:

Warning:
setting RF24_250KBPS will fail for non-plus units

Seems like my RF24 nano is a non-plus unit whatever it means.

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