RF24Network Library / sending multiple messages

Hey all,

So I had recently purchased the nRF24L01 modules : http://www.amazon.com/gp/product/B00E594ZX0/ref=oh_details_o00_s00_i00?ie=UTF8&psc=1

I had downloaded the basic RF24 and the RF24Network (GitHub - maniacbug/RF24Network: Network Layer for nRF24L01(+) Radios) and compiled / wired everything fine according to this blog post:
http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo

My problem lies in using the RF24Network library for my needs. I essentially want to transmit different messages from the transmitter (I have 4 total push buttons… so if button A is pressed…send A, if button B pressed, send B…etc) to be received from the other module. I found this code for the transmitter: RF24Network: helloworld_tx.pde

and the code for the receiver: RF24Network: helloworld_rx.pde

and attempted to do what I had mentioned without any luck. it seems the “up” section of my code continuously prints and then the “left” string becomes jumbled with the up string.

here is my code for the transmitter:

/*
 Copyright (C) 2012 James Coliz, Jr. <maniacbug@ymail.com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */

/**
 * Simplest possible example of using RF24Network 
 *
 * TRANSMITTER NODE
 * Every 2 seconds, send a payload to the receiver node.
 */

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

const int leftPin = 3;     // the number of the pushbutton pin
const int upPin = 4;     // the number of the pushbutton pin

int upState = 0;         // variable for reading the pushbutton status

int leftState = 0;         // variable for reading the pushbutton status

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

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

// Address of our node
const uint16_t this_node = 1;

// Address of the other node
const uint16_t other_node = 0;

// How often to send 'hello world to the other unit
const unsigned long interval = 1000; //ms

// When did we last send?
unsigned long last_sent;

// How many have we sent already
unsigned long packets_sent;

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

void setup(void)
{
  
  Serial.begin(57600);
  Serial.println("RF24Network/examples/helloworld_tx/");
   pinMode(leftPin, INPUT); 
   pinMode(upPin, INPUT);     
   

  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
}

void loop(void)
{
    leftState = digitalRead(leftPin);
    upState = digitalRead(upPin);

  // Pump the network regularly
  network.update();

  // If it's time to send a message, send it!
  unsigned long now = millis();
  if ( now - last_sent >= interval  )
  {
    last_sent = now;
packets_sent = 'R';
    Serial.print("Sending...");
  //  payload_t payload = { millis(), packets_sent++ };
    //payload_t payload = {packets_sent};
                  const char* left = "turn L";
               const char* up = "go forward";

        const char* right = "right";
        const char* down = "down";


    RF24NetworkHeader header(/*to node*/ other_node);
 // bool ok = network.write(header,&payload,sizeof(payload));
     if (leftState == HIGH) {    
 
  bool go = network.write(header,left,strlen(left));

     }
      if (upState == HIGH) {    
 
  bool go = network.write(header,up,strlen(up));

     }
     
     
     
    
}
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

and code for my receiver

/*
 Copyright (C) 2012 James Coliz, Jr. <maniacbug@ymail.com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */

/**
 * Simplest possible example of using RF24Network,
 *
 * RECEIVER NODE
 * Listens for messages from the transmitter and prints them out.
 */

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

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

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

// Address of our node
const uint16_t this_node = 0;

// Address of the other node
const uint16_t other_node = 1;

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

void setup(void)
{
  Serial.begin(57600);
  Serial.println("RF24Network/examples/helloworld_rx/");
 
  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
}

void loop(void)
{
  

    
  // Pump the network regularly
  network.update();

  // Is there anything ready for us?
  while ( network.available() )
  {
    
    /*
    // If so, grab it and print it out
    RF24NetworkHeader header;
    payload_t payload;
    network.read(header,&payload,sizeof(payload));
    Serial.print("Received packet #");
    Serial.print(payload.counter);
    Serial.print(" at ");
    Serial.println(payload.ms);
    */
    
    
    
     // If so, grab it and print it out
    RF24NetworkHeader header;
    static char message[32];
    network.read(header,message,sizeof(message));
    //Serial.print("Received: ");
    Serial.println(message);
    
if(message=="turn L"){
  
  Serial.println("LEFT PRESS");
}

if(message=="go forward"){
    Serial.println("UP PRESS");

  
}

  
  }
  
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

The node numbers seem interesting. Both devices seem to think that they are 0 and that the other one is 1. Is that right?

All the commented out code clearly has nothing to do with your problem. Exercise your delete key, instead of posting that stuff for us to wade through.

    network.read(header,message,sizeof(message));

Where do you learn how many characters were received?

if(message=="turn L"){

The address of message will never be the same as the address of the string literal. You need to use strcmp() to compare the data at the addresses.

RF24Network includes a message type in every header. I would use that to indicate which button had been pressed. Something like

    payload_t payload = { millis(), packets_sent++ };
 
     if (leftState == HIGH) {    
         RF24NetworkHeader header(/*to node*/ other_node,/*msg type*/ 'L');
     }
     if (upState == HIGH) {    
         RF24NetworkHeader header(/*to node*/ other_node,/*msg type*/ 'U');
     }
   
     bool ok = network.write(header,&payload,sizeof(payload));
     if (ok)
         Serial.println("Received ok");
      else
         Serial.println("Not received");

For the receiver you just need a switch statement based on the message type

while ( network.available() )
  {
    // If so, have a look and see what message type it is
    RF24NetworkHeader header;

    network.peek(header);

    switch (header.type)
    {
      case 'L':
        payload_t payload;
        network.read(header,&payload,sizeof(payload));
        Serial.print("Received Left command from node ");
        Serial.print(header.from_node);
        Serial.print(" at ");
        Serial.println(payload.ms);      
      break;
      case 'U':
        payload_t payload;
        network.read(header,&payload,sizeof(payload));
        Serial.print("Received Up command from node ");
        Serial.print(header.from_node);
        Serial.print(" at ");
        Serial.println(payload.ms);
      break;
   default:
     // Read message to clear buffers when we receive message type not in switch list above
        Serial.println("Unknown message type");
        network.read(header,&payload,sizeof(payload));
      break;
    };

Of course, if you want to press more than 1 button at the same time you will need to get a bit more sophisticated.

The node numbers seem interesting. Both devices seem to think that they are 0 and that the other one is 1. Is that right?

I am confused on this front also. Should they be set to the same number address? This was in the default code, but I am not clear how this is being used in all honesty.

I have attempted to incorporate both of your suggestions, and I am receiving the error message from the default switch case.
I had to set the payload_t payload declaration outside of the switch case loop since it wasnt detecting it in the scope. I have made some revisions from this tutorial mentioned in the forums: http://forum.arduino.cc/index.php?topic=138663.0 But I am having difficulty sending different packets at different points (not simultaneously) when a button out of four buttons is pressed. I am receiving two out of the 4 values, but seeing a “77” continuously printed in the monitor.

code below:

transmitter

//TRANSMITTER


#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];


RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 7;
int SW2 = 6;

int SW3 = 5;

int SW4 = 4;


 
void setup(void){
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);
}
 
void loop(void){
  if (digitalRead(SW1) == HIGH){
  msg[0] = 111;
  radio.write(msg, 1);


}
  if (digitalRead(SW2) == HIGH){
  msg[0] = 222;
  radio.write(msg, 1);


}
  if (digitalRead(SW3) == HIGH){
  msg[0] = 333;
  radio.write(msg, 1);


}
  if (digitalRead(SW4) == HIGH){
  msg[0] = 444;
  radio.write(msg, 1);


}


}

and receiver

//RECEIVER

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];



RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 7;
int LED2 = 6;

int LED3 = 5;

int LED4 = 4;

 
void setup(void){
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);

  pinMode(LED3, OUTPUT);

  pinMode(LED4, OUTPUT);



}
 
void loop(void){
  if (radio.available()){
    bool done = false;    
    while (!done){
      done = radio.read(msg, 1);      
      Serial.println(msg[0]);
       

    
    
    
      if (msg[0] == 111){
      delay(10);
    digitalWrite(LED4, HIGH);
      digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);

    digitalWrite(LED3, LOW);
  }
      else {
        digitalWrite(LED4, LOW);
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);

    digitalWrite(LED3, LOW);

    }
    
    
    
      if (msg[0] == 222){
      delay(10);
    digitalWrite(LED1, HIGH);
        digitalWrite(LED2, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);

  }
      else {
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    }
    
    
    
    
      if (msg[0] == 333){
      delay(10);
    digitalWrite(LED2, HIGH);
     digitalWrite(LED1, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
  }
      else {
        digitalWrite(LED2, LOW);
      digitalWrite(LED1, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    }
     
    
    
        if (msg[0] == 444){
      delay(10);
    digitalWrite(LED3, HIGH);
     digitalWrite(LED2, LOW);
     digitalWrite(LED1, LOW);

    digitalWrite(LED4, LOW);
  }
      else {
       digitalWrite(LED2, LOW);
     digitalWrite(LED1, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    }
    delay(100);
    } 
  }
  
  else{
  
  Serial.println("No radio available");

  }
}
int msg[1];

One element array look pretty stupid, usually. This one is no exception.

  radio.write(msg, 1);

How many bytes in an int? No, not 1.

      done = radio.read(msg, 1);

How many bytes in an int?