[SOLVED] nRF24L01+ receiving wrong data in Arduino

I’ve been some days trying to make work this module, and I have almost all working.
I have a “little” error when I’m tryingo receive the data sent from Raspberry Pi

In Raspberry Pi (Python) I send this (with this: library GitHub - BLavery/lib_nrf24: Python library for NRF24L01+ Transceivers)

buf = ['1','2']

And in Arduino UNO, with the maniacbug library GitHub - maniacbug/RF24: Arduino driver for nRF24L01 I received:

Got payload 842150449

This last one output was from the getting started example wich print the data with:

printf("Got payload %lu...",got_time);

I edited the file with this:

if(radio.available()){ 
    char msg[radio.getDynamicPayloadSize()];
    bool ok = radio.read(&msg, radio.getDynamicPayloadSize());

    if(ok){
        Serial.println(msg);
    }else{
        Serial.println("Error");
    }
  }

And now print

12	_ò

wich is closer of the real result.

How can I receive an optimal data?
I now I’m doing something wrong but I don’t know what, can anyone help me?

EDIT: Full code

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

RF24 radio(9,10);

const uint64_t pipes[2] = {0xe7e7e7e7e7,0xc2c2c2c2c2};

void setup(void){
  Serial.begin(57600);
  printf_begin();
  radio.begin();

  radio.setRetries(15,15);
  radio.setPayloadSize(32);
  radio.setDataRate(RF24_1MBPS);
  radio.setPALevel(RF24_PA_MAX);
  radio.setChannel(0x4c);
  radio.enableDynamicPayloads();
  radio.openReadingPipe(1,pipes[1]);

  radio.printDetails();
  radio.startListening();
}


void loop(void){
  if(radio.available()){  
    char msg[radio.getDynamicPayloadSize()]; 
    
    bool ok = radio.read(&msg, radio.getDynamicPayloadSize());

    if(ok){
      Serial.println(msg);
    }else{
      Serial.println("Error");
    }
  }
}

printf.h attached

printf.h (656 Bytes)

Post your complete code. It is impossible to make sense of all those snippets.

What are you expecting / hoping to receive ?

...R

It appears to be not clearing the buffer, from the limited info you have. The message is working once and maybe adding together the second time?

    char msg[radio.getDynamicPayloadSize()];

Wrong. You KNOW the maximum payload size. Use THAT value to size this array. The array need to be one element larger, to hold the terminating NULL, which, by the way, you don't put in the array.

Paul, I've trying with this too:

char msg[32];

and I get the same result. I edited the first post, and added the full code

The radio sends bytes, not characters. Byte arrays are not NULL terminated. Char arrays that are to be considered strings MUST be NULL terminated. You are not NULL terminating the array, but still expect it to work with functions that expect strings. That expectation is unrealistic.

Thanks for that info Pault, as I said I know I'm doing something wrong, or waiting something unrealistic as you said.

is there a better way to do this?

msg[ radio.getDynamicPayloadSize()] = '\0';

there is a better way to do this?

Does that work? If so, there is no better way.

It does, just I'm asking if is corret the way I'm passing from bytes to char. If that is the only way, so there is nothing else to ask.

Thanks for your help Paul

Bueno esto debí haberlo posteado hace dias, en cuanto vi tu práctica con el nRF24L01

TMRh20s Project Blog

Es un blog que cubre todos los aspectos de este módulo que personalmente me encanta. Te lo recomiendo, incluso transmitiendo audio o con el Raspeberry Pi como Server.