rf24network simple bidirectional sketch

I modified the helloworld example since i need to have a bidirectional connection between two (for now) units
it now includes an on the fly unit number selection (not saved between reboots) and led for it.
this sketch simply sends a message to the other unit when the button is pressed.

I hope it’s useful for someone.
The next step for me is discovering how the message (payload) can be customized, ideally i would send-receive some characters and a value (like “date,20141018” time or other commands to be executed on the remote or local unit)

if someone can help, i also not fully comprehend what the RF24 library is for. It seems to me that all the tasks are made by RF24Network library.

Thank you!

//this sketch makes two arduinos exchange a message via rf24network
//to set it up you need  5 resistors (i use 220 ohm resistors)and 4 leds, red, yellow, green and blue (or choose your colors)
//you also need a push button connected to ground via a resistor
//after the arduino starts, on one unit press the button when the yellow led is on, this sets that unit to node 0

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

// nRF24L01(+) radio attached using Getting Started board 
RF24 radio(9,10);

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

unsigned long packets_sent;

int node=1;
int other_node;
int red=2;
int yellow=3;
int button=4;
int green=5;
int blue=6;
int buttonstate=0;
int chosen=0;

int timed=200; //global delay setting

// Structure of our payload
struct payload_t
  unsigned long ms;
  unsigned long counter;

void setup(void)
  pinMode(button, INPUT);       //the button is connected to ground via a resistor
  digitalWrite(button,HIGH);    //i made this to be compatible with devduino sensor node
  pinMode(red, OUTPUT);         //used for error on transmission
  pinMode(yellow, OUTPUT);      //used for transmissiok ok and in setup
  pinMode(green, OUTPUT);       //used for recieved message
  pinMode(blue, OUTPUT);        //used for role display
  digitalWrite(yellow, HIGH);   //turn on yellow in setup, during theese 3 secs pushing the button sets the unit as 0
  while(millis()<3000&&chosen==0) //the while ends in 3 secs or when user choices the role
    if(digitalRead(button)==0&&chosen==0)  //if the button is pressed and we have not jet made a choice we can set the unit as 1
      digitalWrite(blue,HIGH);   //notify the user of the role of this unit
  digitalWrite(yellow, LOW);     //the setup is done, so we can turn off the led
  delay(500);                    //this delay ensures the user has the time to lift 
                                 //the finger from the button, otherwise it will trigger a tansmit command
  if(node==0)          //if we are node 0 the other node is 1
    other_node=0;      //if we are node 1 the other node is 0

  network.begin(/*channel*/ 90, /*node address*/ node);
  Serial.print("Hi! we are the unit number ");

void loop(void)
  // Pump the network regularly

  // Is there anything ready for us?
  while ( network.available() )
    // If so, grab it and print it out
    RF24NetworkHeader header;
    payload_t payload;
    Serial.print("Received packet #");
    Serial.print(" at ");


  if(digitalRead(button)==0)  //if the button is pressed send a message to the other node
    payload_t payload = { 
      millis(), packets_sent++    };

    RF24NetworkHeader header(/*to node*/ other_node);

    bool ok = network.write(header,&payload,sizeof(payload));
    if (ok)                       //if ok is true we have transmitted the message
    else                          //if ok is false the reciever can't be contacted (or is busy doing something else)


For managing data being sent, you probably want to take a look at data structures:

http://playground.arduino.cc/Code/Struct http://www.cplusplus.com/doc/tutorial/structures/

The RF24 driver is a lower level driver (OSI layer 2) and the network layer is a higher level driver (OSI layer 3), upon which additional OSI layer 4 protocols (such as TCP) can be implemented.