nRF24L01 - sending Strings

Hey people,

I have a problem using the nRF24L01 transceivers.

Here is a shortened version of my code, with the important parts for the Transmitter:

#include <arduino.h>
#include <SPI.h>
#include "RF24.h"

...

// Initialize Array with Radio Node addresses
byte addresses[][6] = {"1Node","2Node"};

// INITIALIZING THE RADIO COMMUNICATION
RF24 radio(6,7);

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(addresses[0]);
  radio.openReadingPipe(1,addresses[1]);
  radio.startListening();
}

void loop() {
  String command = ""; // Storing the latest command received

  ...
  
	if(command.startsWith("setFlag") && command.endsWith("#")) {
	  // If this is the case, the control unit got the command to send a new flag color

	  radio.stopListening();
	  /* Parse the given command into the Flag Unit number and the flag color */
	  int firstSeparator = command.indexOf('|');
	  int secondSeparator = command.indexOf('|', firstSeparator + 1);
	  int lastChar = command.indexOf('#');
	  String unitNo = command.substring(firstSeparator + 1, secondSeparator);
	  String flagColor = command.substring(secondSeparator + 1, lastChar - 1);
	  String sendingString = unitNo + "|" + "flagColor";

	  /* Send Flag Change Command to every Flag Unit */
	  radio.stopListening();
	  if(!radio.write(&sendingString, sizeof(String))) {
		throwError("FailedSending");
		return;
	  }

	  /* Wait for the response and watch out for a timeout */
	  radio.startListening();

	  unsigned long startingTime = micros();
	  bool timeout = false;
	  while(!radio.available()) {
		if(micros() - startingTime > 200000) {
		  throwError("Timeout");
		  timeout = true;
		  break;
		}
	  }

	  if(timeout == false) {
		String response;
		radio.read(&response, sizeof(String));

		if(response != "success") {
		  throwError("FalseResponse");
		}
	  }
	}
}

...

void throwError(String errorType) {
  Serial.print(STX);
  Serial.print(errorType);
  Serial.print(ETX);
}

However, it doesn’t even get to communicate a response to me. When I type in a command like “setFlag|1|yellow#”, I instantly get the “FailedSending” message.

It doesn’t seem to be due to the type of data I’m trying to send though, as even when I try “radio.write('a', sizeof(char))”, the sending process fails.

The RF24 library’s examples, from where I “learned” the code, worked just fine, so I think there’s some problem with the configuration of my radio.

Thank you for your help!

Instead of parsing/concatenating the characters before writing them, maybe try to send character by character from and see if you receive them correctly.

I thought so too, at first, but:

TheEpicSnowWolf:
It doesn't seem to be due to the type of data I'm trying to send though, as even when I try "radio.write('a', sizeof(char))", the sending process fails.

So that's at least only part of the problem. I will keep that in mind, though.

You can not send Strings like you try to.

	 if(!radio.write(&sendingString, sizeof(String))) {

sizeof(String) is not the length of the char array that is contained in the string.

You can send the content of a char array.
You can receive bytes and place them into a char array.

Something like

	 if(!radio.write(sendingString.c_str(), sendingString.length()) {

could work.

Better drop all Strings and use char arrays (on 8-bit-Arduinos).

Thank you for clarifying this. So I changed the sending part into this:

      for(int i = 0; i < sendingString.length(); i++) {
        int sendingChar[1];
        sendingChar[0] = sendingString.charAt(i);
        if(!radio.write(sendingChar, 1)) {
          throwError("FailedSending");
          return;
        }
      }

But I still get the same error.

Have a look at this Simple nRF24L01+ Tutorial. The examples include sending cstrings (arrays of type char terminated by 0)

...R

Okay, I changed my program again so from now on it's definitely using a char array to send, with a fixed length and a command pattern like that: setFlag|01|01#

      radio.stopListening();
      /* Parse the given command into the Flag Unit number and the flag color */
      int firstSeparator = command.indexOf('|');
      String sendingString = command.substring(firstSeparator + 1);
      char sendingArray[5];
      sendingString.toCharArray(sendingArray, 5);

      radio.stopListening(); 

      /* Send Flag Change Command witht the important information to every Flag Unit */
      if(!radio.write(&sendingArray, sizeof(sendingArray))) {
        throwError("FailedSending");
        return;
      }

Still, the same error. I feel like this problem has its cause not in what is sent but in some kind of configuration trouble.

TheEpicSnowWolf:
Okay, I changed my program again

Still, the same error.

Did you try any of my examples? They do work and it is much easier to help if you are using simple code I am familiar with.

And if you want help with your own code you need to post the complete program.

...R

Yeah, actually I used the exact same pattern of sending as you did in your example.

However, I fixed it now. Turned out to be an error with the receiving unit. Got confused by the fact that I thought sending the bytes should work regardless of whether it was successfully received on the other end.

Thank you for your help!

what should i do to compare two string which was recieved by nrf24l01

elavarasan7092:
what should i do to compare two string which was recieved by nrf24l01

This is a different question. I suggest you use your existing Thread and explain (with examples) exactly what you are trying to do.

The strcmp() function may be appropriate.

...R