These two scripts came together from one source. One Master can get incoming RF from multiple slaves.
The master prints to Serial: [][][] Beginning nRF24L01+ master-multiple-slave program [][][]
Slave prints to Serial: RX_ADDR_P0-1 = 0xffffffffff 0xffffffffff
RX_ADDR_P2-5 = 0xff 0xff 0xff 0xff
TX_ADDR = 0xffffffffff
RX_PW_P0-6 = 0xff 0xff 0xff 0xff 0xff 0xff
EN_AA = 0xff
EN_RXADDR = 0xff
RF_CH = 0xff
RF_SETUP = 0xff
CONFIG = 0xff
DYNPD/FEATURE = 0xff 0xff
Data Rate = 1MBPS
Model = nRF24L01
CRC Length = 16 bits
PA Power = PA_MAX
I have a second Uno plugged into the PC, reading Serial with Termite.
When Slave was connected to PlatformIO after some time it said:
“Received request from master - sending preloaded data.”.
The second Uno, the Slave was plugged into the wall before I plugged into the PC for serial output.
There is no other RF source I’m aware of, and the Master was not plugged in.
That message does not come anymore.
/*************************************************************************
* Master-unit - nRF24L01+ radio communications *
* A program to operate as a master unit that transmits to and *
* receives data from three remote slave devices. Each slave *
* also has an nrf24l01+ transceiver, and replies with data *
* using the Acknowledgement payload facility of the Enhanced *
* ShockBurst packet structure. *
* *
* Author: B.D. Fraser *
* *
* Last modified: 25/06/2017 *
* *
*************************************************************************/
// include external libs for nrf24l01+ radio transceiver communications
#include <Arduino.h>
#include <RF24.h>
#include <SPI.h>
#include <nRF24l01.h>
#include <printf.h>
// set Chip-Enable (CE) and Chip-Select-Not (CSN) radio setup pins
#define CE_PIN 9
#define CSN_PIN 10
// set transmission cycle send rate - in milliseconds
#define SEND_RATE 1000
// create RF24 radio object using selected CE and CSN pins
RF24 radio(CE_PIN, CSN_PIN);
// setup radio pipe addresses for each sensor node
const byte nodeAddresses[3][5] = {
{'N', 'O', 'D', 'E', '1'},
{'N', 'O', 'D', 'E', '2'},
{'N', 'O', 'D', 'E', '3'}};
// integer to store count of successful transmissions
int masterSendCount = 0;
// simple integer array for data from each slave node: { node_id, returned_count }
int remoteNodeData[3][3] = {{
1,
1,
},
{2, 1},
{3, 1}};
// system operation timing variables - set SEND_RATE to limit transmit rate
unsigned long currentTime;
unsigned long lastSentTime;
/* Function: setup
* Initialises the system wide configuration and settings prior to start
*/
void setup()
{
// setup serial communications for basic program display
Serial.begin(9600);
Serial.println("[*][*][*] Beginning nRF24L01+ master-multiple-slave program [*][*][*]");
// ----------------------------- RADIO SETUP CONFIGURATION AND SETTINGS -------------------------//
// begin radio object
radio.begin();
// set power level of the radio
radio.setPALevel(RF24_PA_LOW);
// set RF datarate - lowest rate for longest range capability
radio.setDataRate(RF24_250KBPS);
// set radio channel to use - ensure all slaves match this
radio.setChannel(0x76);
// set time between retries and max no. of retries
radio.setRetries(4, 10);
// enable ack payload - each slave replies with sensor data using this feature
radio.enableAckPayload();
// --------------------------------------------------------------------------------------------//
}
/* Function: loop
* main loop program for the master device - repeats continuously during operation
*/
/* Function: receiveNodeData
* Make a radio call to each node in turn and retreive a message from each
*/
void receiveNodeData()
{
Serial.print("[*] Master unit has successfully sent and received data ");
Serial.print(masterSendCount);
Serial.println(" times.");
// make a call for data to each node in turn
for (byte node = 0; node < 3; node++)
{
// setup a write pipe to current sensor node - must match the remote node listening pipe
radio.openWritingPipe(nodeAddresses[node]);
Serial.print("[*] Attempting to transmit data to node ");
Serial.println(node + 1);
Serial.print("[*] The master unit count being sent is: ");
Serial.println(masterSendCount);
// boolean to indicate if radio.write() tx was successful
bool tx_sent;
tx_sent = radio.write(&masterSendCount, sizeof(masterSendCount));
// if tx success - receive and read slave node ack reply
if (tx_sent)
{
if (radio.isAckPayloadAvailable())
{
// read ack payload and copy data to relevant remoteNodeData array
radio.read(&remoteNodeData[node], sizeof(remoteNodeData[node]));
Serial.print("[+] Successfully received data from node: ");
Serial.println(node);
Serial.print(" ---- The node count received was: ");
Serial.println(remoteNodeData[node][1]);
// iterate master device count - keeps data changing between transmissions
if (masterSendCount < 500)
{
masterSendCount++;
}
else
{
masterSendCount = 1;
}
}
}
else
{
Serial.print("[-] The transmission to the selected node failed.");
}
}
Serial.println("--------------------------------------------------------");
}
void loop()
{
// ensure we dont collect data from nodes faster than selected rate
currentTime = millis();
while (currentTime - lastSentTime <= SEND_RATE)
{
}
// collect sensor data from all nodes
receiveNodeData();
lastSentTime = millis();
}
'''
#include <Arduino.h>
/*************************************************************************
* Remote node - nRF24L01+ radio communications *
* A program to operate a remote-node slave device that sends *
* data to a command unit on a given request message. The radio *
* transceiver used is the nRF24L01+, and it operates using the *
* TMRh20 RF24 library. *
* *
* Author: B.D. Fraser *
* *
* Last modified: 27/06/2017 *
* *
*************************************************************************/
// nRF24L01 radio transceiver external libraries
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <printf.h>
// chip select and RF24 radio setup pins
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN,CSN_PIN);
// setup radio pipe addresses for each sensor node
const byte nodeAddress[5] = {'N','O','D','E','1'};
unsigned long updateNodeData();
// simple integer array for remote node data, in the form [node_id, returned_count]
int remoteNodeData[2] = {
1,
1,
};
// integer to store count of successful transmissions
int dataFromMaster = 0;
/* Function: setup
* Initialises the system wide configuration and settings prior to start
*/
void setup() {
Serial.begin(9600);
// ----------------------------- RADIO SETUP CONFIGURATION AND SETTINGS -------------------------//
radio.begin();
// set power level of the radio
radio.setPALevel(RF24_PA_LOW);
// set RF datarate
radio.setDataRate(RF24_250KBPS);
// set radio channel to use - ensure it matches the target host
radio.setChannel(0x66);
// open a reading pipe on the chosen address - matches the master tx
radio.openReadingPipe(1, nodeAddress);
// enable ack payload - slave replies with data using this feature
radio.enableAckPayload();
// preload the payload with initial data - sent after an incoming message is read
radio.writeAckPayload(1, &remoteNodeData, sizeof(remoteNodeData));
// print radio config details to console
printf_begin();
radio.printDetails();
// start listening on radio
radio.startListening();
// --------------------------------------------------------------------------------------------//
}
/* Function: loop
* main loop program for the slave node - repeats continuously during system operation
*/
void radioCheckAndReply(void)
{
// check for radio message and send sensor data using auto-ack
if ( radio.available() ) {
radio.read( &dataFromMaster, sizeof(dataFromMaster) );
Serial.println("Received request from master - sending preloaded data.");
// update the node count after sending ack payload - provides continually changing data
updateNodeData();
}}
void loop() {
// transmit current preloaded data to master device if message request received
radioCheckAndReply();
}
/* Function: updateNodeData
* updates the count variable for the node and stores it in the nRF24L01+ radio
* preloaded ack payload ready for sending on next received message
*/
long unsigned int updateNodeData(void)
{
// increment node count - reset to 1 if exceeds 500
if (remoteNodeData[1] < 500) {
remoteNodeData[1]++;
} else {
remoteNodeData[1] = 1;
}
// set the ack payload ready for next request from the master device
radio.writeAckPayload(1, &remoteNodeData, sizeof(remoteNodeData));
}
/* Function: radioCheckAndReply
* sends the preloaded node data over the nrf24l01+ radio when
* a message is received by the master
*/
Depending on the distance between the one master and the slave, you might want to increase the power output. The low data rate is okay – the slower the transmission and the more power is used in the transmission, the greater the reach of the message (very simplified model). You may want to try one of
The master iterates through 3 slave node addresses. Do you also compile the slave nodes with these addresess? I.e, you flash the first slave node with “NODE1” as address, then your second one with “NODE2”