Two-way transmission by swapping roles
I have not found a need for this approach but I will illustrate it in case it is useful for someone else.
In this example the two nRF24s (master and slave) spend most of their time listening and only switch to talking for the minimum time needed to send a message. As with the ackPayload example the slave only sends a message in response to a message from the master.
MasterSwapRoles.ino
// MasterSwapRoles
#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'};
const byte masterAddress[5] = {'T','X','a','a','a'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[10] = "Message 0";
char txNum = '0';
int dataReceived[2]; // to hold the data from the slave - must match replyData[] in the slave
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
//============
void setup() {
Serial.begin(9600);
Serial.println("MasterSwapRoles Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openWritingPipe(slaveAddress);
radio.openReadingPipe(1, masterAddress);
radio.setRetries(3,5); // delay, count
send(); // to get things started
prevMillis = millis(); // set clock
}
//=============
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
getData();
showData();
}
//====================
void send() {
radio.stopListening();
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
radio.startListening();
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt) {
Serial.println(" Acknowledge received");
updateMessage();
}
else {
Serial.println(" Tx failed");
}
}
//================
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.print(dataReceived[0]);
Serial.print(", ");
Serial.println(dataReceived[1]);
Serial.println();
newData = false;
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
SlaveSwapRoles.ino
// SlaveSwapRoles
#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'};
const byte masterAddress[5] = {'T','X','a','a','a'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataReceived[10]; // must match dataToSend in master
int replyData[2] = {109, -4000}; // the two values to be sent to the master
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup() {
Serial.begin(9600);
Serial.println("SlaveSwapRoles Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
radio.openReadingPipe(1, slaveAddress);
radio.setRetries(3,5); // delay, count
radio.startListening();
}
//====================
void loop() {
getData();
showData();
send();
}
//====================
void send() {
if (newData == true) {
radio.stopListening();
bool rslt;
rslt = radio.write( &replyData, sizeof(replyData) );
radio.startListening();
Serial.print("Reply Sent ");
Serial.print(replyData[0]);
Serial.print(", ");
Serial.println(replyData[1]);
if (rslt) {
Serial.println("Acknowledge Received");
updateReplyData();
}
else {
Serial.println("Tx failed");
}
Serial.println();
newData = false;
}
}
//================
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
}
}
//================
void updateReplyData() {
replyData[0] -= 1;
replyData[1] -= 1;
if (replyData[0] < 100) {
replyData[0] = 109;
}
if (replyData[1] < -4009) {
replyData[1] = -4000;
}
}
End of Tutorial
...R
Edit 02 September 2019 to remove reference to broken link