Hi everyone,
I'm having an issue with transmitting (or receiving, I'm not sure exactly where the problem is occurring) Strings between two NRF24L01 modules. I have one set up as a transmitter and one as a receiver. The codes are below, one simply spits out a String and the other listens for it. The codes work just fine if I use an int or a simple char, but when I try to use a String to send text, I get some gibberish and then a failure. I haven't the slightest clue what's going on, I haven't been doing Arduino for that long.
Here's the code for the transmitter;
#include <SPI.h>
#include "RF24.h"
bool radioNumber = 0;
RF24 radio(7,8);
byte addresses[][6] = {"1Node","2Node"};
void setup() {
Serial.begin(115200);
Serial.println(F("Starting- Transmitter"));
radio.begin();
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
}
void loop() {
Serial.println(F("Now sending"));
String test = "Test";
if (!radio.write( &test, sizeof(String) )){
Serial.println(F("failed"));
}
Serial.print(F("Sent "));
Serial.println(test);
delay(2000);
}
Here's the code for the receiver;
#include <SPI.h>
#include "RF24.h"
bool radioNumber = 1;
RF24 radio(7,8);
byte addresses[][6] = {"1Node","2Node"};
void setup() {
Serial.begin(115200);
Serial.println(F("Starting- Receiver"));
radio.begin();
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
radio.startListening();
}
void loop() {
String test;
if( radio.available()){
// Variable for the received timestamp
while (radio.available()) { // While there is data ready
radio.read( &test, sizeof(String) ); // Get the payload
}
Serial.print("Got: ");
Serial.println(test);
}
}
This is what the Serial monitor gives on the transmitter side;
Starting- Transmitter
Now sending
Sent Test
Now sending
Sent Test
Now sending
Sent Test
Now sending
Sent Test
Now sending
Sent Test
Now sending
failed
Sent Test
Now sending
failed
Sent Test
Now sending
failed
Sent Test
Now sending
failed
Sent Test
Now sending
failed
Sent Test
It just goes on like this.
And here's the Serial monitor on the receiver side;
Starting- Receiver
Got: ö/*
Got: /*
Like I said, I have no idea what this means. Any help would be greatly appreciated.
Thanks
~Josh Blackburn