NRF24L01+ Received Garbage

'------------------------------------------'
'Transmitter Using NRF24L01+'
' '
'String MyID;'
'String MyVal;'
'String DTS;'
' '
'DTS = MyID + MyVal; //Should Transmit RT70'
' '
'radio.write(&DTS, sizeof(DTS));'
' '
'Serial.println(DTS) //Does Show RT70'
'------------------------------------------'
'Receiver Using NRF24L01+'
' '
'String MyID;'
'String MyVal;'
'String DTS;'
' '
'DTS = "";'
' '
'radio.read(&DTS, sizeof(DTS));'
' '
'Serial.println(DTS); //Should Receive RT70'
'------------------------------------------'
'The receiver shows garbage instead of the values that were sent.'
' '
'Any ideas?'

have a look at how-to-get-the-best-out-of-this-forum

How to get the best out of this forum?

Step1. Search the forums for something to help you. (DONE)

Step 2. Post a question.

Step 3. Wait for someone to help.

Unless your unhelpful reply was just to up your count in the forums.

1 Like

Strings will not work, use char arrays.

Thank you very much for your response. I will try that.

It works like a champ now Whandall. Thank you again. Here is the working code that I am using.

Transmitter

DTS = MyID + MyVal;

char charBuf[6];
DTS.toCharArray(charBuf, 6);
radio.write(&charBuf, sizeof(charBuf)); //Sends RT70
Receiver

char text[6] = "";
radio.read(&text, sizeof(text));
Serial.println(text); //Prints Out RT70

I hope that this will help others.

The sending will work by using the c_str() function of the String object,
there is no need to copy the String to a char array.

Thank you again Whandall. Your first bit of advice worked so I will try your second suggestion.

Just remember that sizeof() will not work in that case,
because it returns the size of the String descriptor,
not the size of the string contained within.

I can just x = sizeof(MyString); and then use my variable to set the sizeof() in the radio.write code.

Check the workings of sizeof() in regard to String with a small test program,
I think you did not understand #10.

Works like a champ. Here is the finished code.

Transmitter

DTS = MyID + MyVal; //DTS Now Contains RT70
radio.write(DTS.c_str(), sizeof(DTS));

The receiver code stayed the same and receives RT70.

By accident.

Try to send "Hello receiver!".

The receiver prints out Hello and 6 square characters. I will only be using it to transmit a maximum of 5 characters(FF125) which works like a champ.

It is better to learn about the mistake you have in your code, the wrong sizeof().

Use .length() on the String to get the length.

sizeof() of any String is constant, because it reports the size of the String object,
which is probably a pointer and two integers (buffer, buffer size, used buffer size).

The way that I did it was always a length of 6 no matter what was put into DTS. The following works great.

DTS = "Hello Receiver";
radio.write(DTS.c_str(), DTS.length());

Thank you again.

Much better. :grinning:

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