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.
Instead of parsing/concatenating the characters before writing them, maybe try to send character by character from and see if you receive them correctly.
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.
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.
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.