I am trying to send the data from one Arduino Uno to other using the Nrf24l01 transceiver. When I realized something is not right with the transceiver, I tried a quick demo to see if they were working properly. The code I use is from this forum Simple nRF24L01+ 2.4GHz transceiver demo - Exhibition / Gallery - Arduino Forum, as described below:
Transmitter
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t slaveAddress = 0x000000000000FFFF;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[10] = "Message 0";
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() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
Receiver
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t thisSlaveAddress= 0x000000000000FFFF;
RF24 radio(CE_PIN, CSN_PIN);
char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;
//===========
void setup() {
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop() {
getData();
showData();
}
//==============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
newData = false;
}
}
To summarize the code, the transmitter will send out 10 messages from 0 to 9, and if the package does not arrive, the transmitter will try to send them again and again. The receiver is straight forward, if it receive anything, just print it on the Serial. So after running for the first time, only one package get through the transmitter to the receiver's Serial, as shown in two Serial:
Transmitter's Serial:
SimpleTx Starting
Data Sent Message 0 Acknowledge received
Data Sent Message 1 Tx failed
Data Sent Message 1 Tx failed
Data Sent Message 1 Tx failed
Data Sent Message 1 Tx failed
...
Receiver's Serial
SimpleRx Starting
Data received Message 0
So the package only get through one time only. The Nrf24l01 uses UDP protocol so I can't tell which one is not working properly. Interesting enough, when I run the program again, this time no package get through. It worked as stated before when I unplug the cord from the Arduino and plugged them in again. I am not very familiar with Arduino or Cpp, so I hope my post is clear enough, and if I miss something, please point it out. Thank you very much.