Thanks to both of you ( @Robin and @ Whandall ) been able to get more clarity and the fine print. And after reading @Whandalls points on the AckPayLoad , it looks loaded with traps.
Now I understand why Robin was dissuading me from start on this.
Putting together all what you said, I have successfully coded a Tx and Rx unit to transact four lines of 21 byte data. Its working well. Now I can extend this to my project, where the Master will call for data from each slave when it requires. And no AckPayLoad used .
The Tx code :
/*
06 Oct 2017 : RF24 Tx module code to request four lines of data from a slave. Works well.. ProMini 3.3V
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/****************** User Config ***************************/
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7, 8 );
/**********************************************************/
byte addresses[][6] = {"1Node", "3Node"} ; // Radio pipe address of this node and Rx node
byte rfOkLed = 3, pinSS = 10;
unsigned long rfTimeOut;
char recdMsg[21];
void setup()
{
Serial.begin(9600);
Serial.println(F("*** STARTING MASTER TX *** "));
pinMode(rfOkLed, OUTPUT);
pinMode(pinSS, OUTPUT);
// Setup and configure radio
radio.begin();
radio.setChannel(108); // Above most Wifi Channels
radio.setDataRate( RF24_250KBPS );
radio.setPALevel(RF24_PA_MAX);
radio.openWritingPipe(addresses[0]); // Open a writing pipe on own address...
radio.openReadingPipe(1, addresses[1]); // and a reading pipe on Slave address
digitalWrite(rfOkLed, HIGH); // To check LED wiring
delay(2000);
digitalWrite(rfOkLed, LOW);
}
//****************************************************
void loop(void)
{
for ( byte count = 0; count < 4; count++ )
{
sendDataToMe(count);
delay(10); // Is a delay required at all here ?
}
Serial.println(); // Get a gap between set of data..
delay(1000);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// FUNCTION TO SEND A DUMMY VALUE TO REQUEST FOR RESPONSE FROM SALVE..
void sendDataToMe (byte count )
{
//<<<<<<<<<<<<<< SEND DATA >>>>>>>>>>>>>>>>
radio.stopListening();
byte txPacket = count;
if (radio.write( &txPacket, sizeof(txPacket)))
{
//Serial.println( txPacket); // Use for debugging
}
//<<<<<<<<<<<<<< GET DATA >>>>>>>>>>>>>>>>>
radio.startListening();
unsigned long started_waiting_at = millis(); // Wait here until we get a response, or timeout (250ms)
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 250 ) timeout = true;
if ( timeout ) // Explain what happened ...
{
Serial.println("Failed, response timed out.");
digitalWrite(rfOkLed, LOW);
}
else
{
radio.read( &recdMsg, sizeof(recdMsg)) ;
Serial.print(" Success ! " );
Serial.println(recdMsg);
digitalWrite(rfOkLed, HIGH);
}
}
//**********************************************
The Rx code :
/*
06 Oct 2017 : Code to send 4 lines of data based on request from a master. Works ok. ( Pro Mini 3.3V)
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/****************** User Config ***************************/
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7, 8);
/**********************************************************/
byte addresses[][6] = {"1Node", "3Node"} ; // Radio pipe address of Tx node and this node
byte rfOkLed = 3, rfErrLed = 6, pinSS = 10;
byte rxData ; // Data being recieved
char message[21] ; // Data being sent back
unsigned long rfTimeOut;
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$
void setup() {
Serial.begin(9600);
Serial.println(F("*** STARTING RECIEVER *** "));
pinMode(pinSS, OUTPUT);
SPI.begin();
// Setup and configure radio
radio.begin();
radio.setChannel(108);
radio.setDataRate( RF24_250KBPS );
radio.setPALevel(RF24_PA_MAX);
radio.setRetries(5, 10);
radio.openWritingPipe(addresses[1]); // Open a writing pipe on own address..
radio.openReadingPipe(1, addresses[0]); // and a reading pipe on Tx address
radio.startListening();
pinMode(rfOkLed, OUTPUT);
digitalWrite(rfOkLed, HIGH);
delay(2000);
digitalWrite(rfOkLed, LOW);
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$
void loop(void)
{
uint8_t pipe_num;
radio.startListening(); // Now, resume listening so we catch the next packets.
if ( radio.available(&pipe_num) )
{
radio.read( &rxData, sizeof(rxData)) ;
Serial.print ("Got payload from TxUnit.... ");
Serial.println( rxData);
switch (rxData)
{
case 0:
strcpy( message, "This is line One" );
break;
case 1:
strcpy( message, "This is line Two" );
break;
case 2:
strcpy( message, "This is line Three" );
break;
case 3:
strcpy( message, "This is line Four" );
break;
default:
break;
}
radio.stopListening(); // First, stop listening so we can Send
radio.write( &message, sizeof(message));
rfTimeOut = millis();
}
if ( millis() - rfTimeOut > 5000 ) // Declare the status to user
{
digitalWrite(rfOkLed, LOW);
}
else
{
digitalWrite(rfOkLed, HIGH);
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%