Goal: Trying to send a String variable via an nRF24L01.
I'm trying to send user-entered text on one arduino to display on a receiver. I am able to send the int variable but I need to send text as well. When trying to send String variable, I either get a numbers on the receiver or "¿ñºÉ" response. I am way over my head trying to do more than the basics with these things.
This is not the full code, just playing with the basics to get an example working. They are the down and dirty super basic code at this point.
I think if someone could give me some direction or a basic sketch, I could do the rest. But any help would be great. I was not really able to follow others on their string posts about this. I've only seen a few and they were not quite what I needed.
transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio (9, 10);
const byte address[6] = "000001";
int hint = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setDataRate( RF24_250KBPS);
radio.setPALevel(RF24_PA_MAX);
radio.stopListening();
}
void loop() {
hint ++;
radio.write(&hint, sizeof(hint));
delay(1000);
}
Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio (9, 10);
const byte address[6] = "000001";
int hint= 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setDataRate( RF24_250KBPS);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
}
void loop() {
if (radio.available()) {
radio.read(&hint, sizeof(hint));
Serial.println(hint);
}
}
So for those examples, I am not quite able to follow the code to edit it to what I need. Eventually, I have a keyboard that you can enter text onto, saved as a String and then need to send that String.
I have all the rest figured out already and just working on the wireless part.
pelicansparky:
So for those examples, I am not quite able to follow the code to edit it to what I need.
Did you manage to get one of the examples to work with no modifications?
Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.
The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work
There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to. If the first example does not work be sure to try the connection test for both of your Arduinos. Nothing will work if the connection test fails.
A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. This seems to be a particular problem with the nano. The high-power nRF24s (with the external antenna) will definitely need an external power supply. At least for testing try powering the nRF24 with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.
If you are using the high-power nRF24s (with the external antenna) it may help to have a distance of about 3 metres between the two nRF24s so that the signal does not overwhelm the receiver. However someone on the Forum has had them working without that separation - I don't have any personal experience with them. If you are new to nRF24s it may be better to start with a pair of low power modules with the pcb antenna.
Robin2:
Did you manage to get one of the examples to work with no modifications?
Yeah, I got it to work with the example, I already had the up-to-date library. So I just copied and pasted. I will play around a little more with your code to see if I can follow it.
I'm using Uno's with the 5v breakout board for them. I have tested the long-range ones out to 1500feet without issues. My data is going through just fine, it's just the wrong data!
I'm going to play with it again tonight and tomorrow. I'll let you know if I can tinker enough with it to understand it anymore. I've spent so much time researching and trying to follow instructions, I was hoping I was just doing something easy and stupidity wrong that would be a quick fix.
Robin2:
Post an example of the data you want to send.
...R
My goal is for user-entered text data to be sent from the master to the slave. This data needs to be variable text data. I can send int just fine.
An example of text would be
"Team Red, GO"
"Team Green, TOO FAR"
"Team Yellow, Almost"
But I can't really store everything I want as a present variable can call on that, I really need to customize that text to the situation.
This is the original code from my SimpleTx program with some changes marked which should give you the idea.
You need some equivalent changes in the Rx program.
This will only change the message once. I leave it as your homework to figure out how to change it to other messages.
(I have not tested this, but I think it is correct)
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[60] = "Team Red, GO"; // changed
char txNum = '0';
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup() {
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}
//====================
void send() {
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt) {
Serial.println(" Acknowledge received");
updateMessage();
}
else {
Serial.println(" Tx failed");
}
}
//================
void updateMessage() {
// change the message
strcpy(dataToSend, "Team Green, TOO FAR"); // changed
}
OK first things first... Robin2, I should have told you that I know enough to be a pain in the ass for the people that really know what they are doing. After playing with your code, it is working beautifully. Not only is your code so much more stable when I started networking multiple receivers together, but it is also way simpler than I was thinking and making it out to be.
I was trying to run it all via the Void Loop()... This is what I ended up doing to send both a custom string and then sending the integer on its own void... Thanks so much!!!
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).