NRF24L01 in condition statement.


In a project, I have to command 5slaves of NRF24L01 by a master one.

I use the RF24Network.h for that.

The problem, is that I have a list of messages to send (one message by in a time) to a given slave in function of other global variables to a given slave. So the choice of the message and the slave depends on thes variables in the program.

this is a part of my global code where I set the variables and the setup of this communication:

#include <SPI.h>
#include <RF24.h>
#include <RF24Network.h>

RF24 radio(8, 9); // CE, CSN

RF24Network network(radio);      // Include the radio in the network

const uint16_t this_node = 00;   // Address of this node in Octal format ( 04,031, etc)
const uint16_t node01 = 01;      
const uint16_t node02 = 02;      
const uint16_t node03 = 03;      
const uint16_t node04 = 04;      
const uint16_t node05 = 05;      

const char MSG_X[] = "ADGHJJYTVF";
const char MSG_Y[] = "XCEGHNGG";
const char MSG_Y[] = "kknkjbkn";
const char MSG_A[] = "ljj";
const char MSG_B[] = "ljoijoij";
const char MSG_C[] = "bbkihhlj";
const char MSG_D[] = "knoljpmo";
const char MSG_E[] = "joij4";
const char MSG_F[] = "kpojoij";

void setup()
;// wait for serial port to connect. Needed for native USB port only

network.begin(90, this_node); //(channel, node address)

Now, In my loop program, I have other paramters which change of state. In function of this state (if statement), I would like to send a specific message (MSG_A, or _X or _Y…) to a specific slave using these two statements:

RF24NetworkHeader header(node!!);     // (Address where the data is going)
network.write(header, &MSG_???, sizeof(MSG_???)); // Send the data

As you can understand,
!! : can be equal to 01, 02, 03, or 05
??: can be equal to X, Y, Z, A, B, C, …F

Now, how to paraetrize (for example in a function) the setting of “node!!, MSG_?? and header” in function of other global variables in the program?

For the moment, I can do it using a big number of if statements, but this is long and repetitives (In my project, the reel number of slaves is 10 and the number of messaes is ~30. So you can imagine the number of if statements lines to choice the correct slave and correct message to send!

Thank you,


Why not put the messages all in one array then use the array index to select the message to send ?

char * messages[] = {"ONE", "TWO", "THREE", "ETC"};

void setup()
  while (!Serial);

void loop()

I suspect that you can also put your NRF24 objects in an array too but I have not tried that

One more detail:
My idea is to use an array of messages for MSG_X,…MSG_F:

char *MSG[10] = {“ADGHJJYTVF”, …, “kpojoij”};

and an array o slaves:

char *NODES[5] = {“node01”, “node02”, “node03”, “node04”, “node05”}

and run my if statements o the other golbal variables, and in fuction of that to determine the index i of MSG and index j of NODES[j].
and a fuction will use that to set NODE[j] and MSG in the statements:
RF24NetworkHeader header(node[j]); // (Address where the data is going)
network.write(header, &MSG_, sizeof(MSG*)); // Send the data*
and also refresh the “header” to avoid any conflict between two different RF sent.
but how to do that?

Thank you UKHeliBob for your quick reply.

That was my idea for more than one week :).

the use of two arrays is a good idea, but I ma not able to set the found MSG and NODE in the NRF24 statements and also to refresh the header for ecah sent.

That is really my big problem!


Where are these MSG and node numbers coming from ?

Thanks UKHeliBob for help.

MSG and NODES are constant. (~10Slave nodes, and ~30messages).

so I can store them in two different arrays, to do simple. I suggest to fix their number to 5:

char *MSG[5] = {"toto0", "toto1", "toto2", "toto3","toto4"}; char *NODE[5] = {"node01", "node02", "node03", "node04","node05"};

Now, Imagine that I have different others variables in my loop prg, and after some if conditions, I can statuate that, I have to use: MSG[3] and NODE[4]

The problem is that these statements:

RF24NetworkHeader header(NODE[4]);     // (Address where the data is going)
network.write(toto, &MSG[3], sizeof(MSG[3])); // Send the data

fail, and I don't know how to "format" NODE and MSG[j] in thes statements. Best regards,

RF24NetworkHeader header(NODE[4]);     // (Address where the data is going)

Are you sure that the parameter to this call is a string ?

Which RF24 library are you using ? Does it have any examples ?

I use these libs:

#include <SPI.h>
#include <RF24.h>
#include <RF24Network.h>

I’m not expert of the NRF24L01.

That’s why I opened this help here to help me in this case.


Where did you get the RF24 libraries from ? There are several versions available with the same name

NRF24Network by TMRh20 version 1.0.9

From the examples with the library it looks like

RF24NetworkHeader header(NODE[4]);     // (Address where the data is going)

takes an RF24 object as its parameter as in

RF24 radio(7,8);                    // nRF24L01(+) radio attached using Getting Started board 

RF24Network network(radio);          // Network uses that radio

so you can't use a string as the parameter, but beyond that I have nothing more to offer I am afraid