[solved] RF24Network Adressing Issue

Hi,

I am having a base node which sends to different children, and grandchildren. The address of the children are received via serial to the base node which then transmit the message to the child.

The problem is that the address sent via the serial as String and the address of the nodes is in octal format. The problem appeared when i started to communicate with grandchildren such as node 011.

I got the following lines which worked for all nodes (00 till 05) but not 011
char message[100];
String address=recvdStringAddressViaSerial;
RF24NEtworkHeader header(address.toInt());
network.write(header,message,sizeof(message);

I though the problem was with Octal format and i used this instead, but still did not work
char message[100];
String addressString=recvdStringAddressViaSerial;
uint16_t address = strtoul(adressString.c_str(),NULL,8);
RF24NEtworkHeader header(address);
network.write(header,message,sizeof(message);

Any help would be much appreciated

Thanks

Have a look at how the addresses are done in this Simple nRF24L01+ Tutorial

You can easily use any format that you find convenient.

...R

I read the article, but could not find something specific to help with Rf24 Network.

Could you add the exact link to the post you are referring to?

boshkash1151:
I read the article, but could not find something specific to help with Rf24 Network.

Could you add the exact link to the post you are referring to?

My tutorial has nothing about the RF24 Network. I just gave you the link so you could see that nRF24L01+ addresses can be created with simple human readable characters.

...R

Any help?

Any help?

What have you done with the advice you have already received?

boshkash1151:
Any help?

Post the latest version of your programs so we can see what the problem is.

...R

Thanks Robin

Master (Base) Node

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


#define MESSAGE_MAX_SIZE 42

RF24 radio(7,8);                    // nRF24L01(+) radio attached using Getting Started board 
RF24Network network(radio);          // Network uses that radio
const uint16_t this_node = 00;        // Address of our node in Octal format
const uint16_t other_node = 01;       // Address of the other node in Octal format
char message[MESSAGE_MAX_SIZE];

#define PROGRESS_PIN 10

void setup(void)
{
  Serial.begin(9600);
  Serial.println("RF24Network Master Started");
  pinMode(PROGRESS_PIN,OUTPUT);
  SPI.begin();
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);          
  radio.setAddressWidth(5);                  
  radio.setDataRate(RF24_2MBPS);        
  radio.setCRCLength(RF24_CRC_16 );              
  radio.setAutoAck(true);      
  network.begin(/*channel*/ 90, /*node address*/ this_node);
}

void loop() {
}



String recvdTextMessage;
boolean recvdTextMessageCompelete;
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    recvdTextMessage += inChar;
    if (inChar == ';') {
      recvdTextMessageCompelete = true;
      sendMessage();
    }
  }
}
//message format [RECIEVER_ADDRESS]:[RESPONSE_TIMEOUT_LIMIT]:[MESSAGE]
void sendMessage(){
  digitalWrite(PROGRESS_PIN,true);
  delay(250);
  network.update(); 
  char message[MESSAGE_MAX_SIZE];
  
  String trimmedMessage = recvdTextMessage.substring(0,recvdTextMessage.length()-1);
  int addressIndex = trimmedMessage.indexOf(":");
  String addressString = trimmedMessage.substring(0,addressIndex);
  String portion = trimmedMessage.substring(addressIndex+1);
  int responseTimeoutIndex = portion.indexOf(":");
  int responseTimeout = portion.substring(0,responseTimeoutIndex).toInt();
  String messageString = portion.substring(responseTimeoutIndex+1);
  memset(message, 0, MESSAGE_MAX_SIZE);
  for(int i =0;i<messageString.length();i++)
  {
    message[i] = messageString[i];
  }
  uint16_t address=strtoul(addressString.c_str(),NULL,8);
  Serial.print("Address: ");
  Serial.println(address);
  Serial.print("Response timeout: ");
  Serial.println(responseTimeout);
  Serial.print("Message: ");
  Serial.println(message);  
  recvdTextMessage="";
  recvdTextMessageCompelete=false;


  RF24NetworkHeader header(/*to node*/ address);
  bool ok = network.write(header,message,sizeof(message));
  if (ok)
   {
    Serial.println("Sent ok.");
    readResponse(responseTimeout);
   }
  else
    Serial.println("Sent failed.");
  
  digitalWrite(PROGRESS_PIN,false);
}



void readResponse(int responseTimeout)
{
  long startTime = millis();
  long currentTime=startTime;
  Serial.println("READ_RESPONSE_STARTED");
  bool gotResponse = false;
  while(currentTime - startTime < responseTimeout)
  {
    currentTime = millis();
    network.update();
    while ( network.available() ) {
        RF24NetworkHeader header;        
        memset(message, 0, MESSAGE_MAX_SIZE);
        network.read(header,&message,sizeof(message));
        Serial.println(message);
        if (hasTerminalCharacter(message))
        {
          gotResponse=true;
        }
    }
    if(gotResponse)break;
    delay(50);
  }
  Serial.println("READ_RESPONSE_ENDED");
}

bool hasTerminalCharacter(char message[])
{
  char terminal_char=';';
  return (String(message)).indexOf(terminal_char) >=0 ? true:false;
}

Grandchild node

#include "Arduino.h"
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(7,8);                // nRF24L01(+) radio attached using Getting Started board 
RF24Network network(radio);      // Network uses that radio
const uint16_t this_node = 011;    // Address of our node in Octal format ( 04,031, etc)
const uint16_t master_node_address = 01;   // Address of the other node in Octal format



#define BUTTON_PIN 2
bool isButtonClicked=false;
//network
#define MESSAGE_MAX_SIZE 42
char message[MESSAGE_MAX_SIZE];

void setup() {
  Serial.begin(9600);
  Serial.println("Sensor System ready");
  //network
  SPI.begin();
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);          
  radio.setAddressWidth(5);                  
  radio.setDataRate(RF24_2MBPS);//RF24_2MBPS        
  radio.setCRCLength(RF24_CRC_16 );              
  radio.setAutoAck(true);    
  network.begin(/*channel*/ 90, /*node address*/ this_node);
  pinMode(BUTTON_PIN, INPUT);    
}

void loop() {
  updateButtonStatus();
  handleCommands();  
}

void handleCommands()
{
  network.update(); 
  if ( network.available() ) {     
    RF24NetworkHeader header;        
    memset(message, 0, MESSAGE_MAX_SIZE);
    network.read(header,&message,sizeof(message));
    Serial.print("Recvd command:[");
    Serial.print(message);
    Serial.println("]");
    String cmd = message;
    if (cmd.startsWith("readbutton"))
    {
      String buttonStatus = readIsButtonPressed();
      handleResponse(master_node_address,buttonStatus);
    }
  }
}


void handleResponse(int address,String response)
{
  RF24NetworkHeader header(/*to node*/ address);
  response.toCharArray(message,MESSAGE_MAX_SIZE);
  Serial.print("Sending Response: [");
  Serial.print(message);
  Serial.print("]");
  bool ok = network.write(header,message,sizeof(message));  
  if (ok)
    Serial.println(" -> ok.");
  else
    Serial.println(" -> failed.");

  delay(50);
}


void updateButtonStatus()
{
  if (digitalRead(BUTTON_PIN) == HIGH) {         
    isButtonClicked = true;
    Serial.println("Button Pressed");
  } 
}

String readIsButtonPressed()
{
  if (isButtonClicked)
  {
      isButtonClicked = false;
      return "yes";
  }  
  return "no";  
}

jremington:
What have you done with the advice you have already received?

it did not apply to my case as i am looking for RF24 Network addressing

I think the simplest thing is to work out the decimal equivalent of the octal number that you need.
For example octal 01 is decimal 1
Octal 011 is decimal 9 (8 + 1)
Octal 0111 is decimal 73 (64 + 8 + 1)

When, for example the code calls for

const uint16_t this_node = 011;

it would be exactly the same if it was

const uint16_t this_node = 9;

...R

It did not work

Moreover this scenario happened:

  • Node 00 sends to Node 01 successfully
  • Trying to send from Node 00 to Node 011, fails
  • Now sending from Node 00 to Node 01 fails as well.

boshkash1151:
It did not work

You will forgive me for saying I have no idea what "it" is as you have not posted the latest versions of your programs.

...R

Robin2:
You will forgive me for saying I have no idea what "it" is as you have not posted the latest versions of your programs.

...R

Sorry for that, I followed your advice of using integer addresses, the updated code is:

Master Node

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


#define MESSAGE_MAX_SIZE 42

RF24 radio(7,8);                    // nRF24L01(+) radio attached using Getting Started board
RF24Network network(radio);          // Network uses that radio
const uint16_t this_node = 0;        // Address of our node in Octal format
const uint16_t other_node = 1;       // Address of the other node in Octal format
char message[MESSAGE_MAX_SIZE];

#define PROGRESS_PIN 10

void setup(void)
{
  Serial.begin(9600);
  Serial.println("RF24Network Master Started");
  pinMode(PROGRESS_PIN,OUTPUT);
  SPI.begin();
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);         
  radio.setAddressWidth(5);                 
  radio.setDataRate(RF24_2MBPS);       
  radio.setCRCLength(RF24_CRC_16 );             
  radio.setAutoAck(true);     
  network.begin(/*channel*/ 90, /*node address*/ this_node);
}

void loop() {
}



String recvdTextMessage;
boolean recvdTextMessageCompelete;
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    recvdTextMessage += inChar;
    if (inChar == ';') {
      recvdTextMessageCompelete = true;
      sendMessage();
    }
  }
}
//message format [RECIEVER_ADDRESS]:[RESPONSE_TIMEOUT_LIMIT]:[MESSAGE]
void sendMessage(){
  digitalWrite(PROGRESS_PIN,true);
  delay(250);
  network.update();
  char message[MESSAGE_MAX_SIZE];
 
  String trimmedMessage = recvdTextMessage.substring(0,recvdTextMessage.length()-1);
  int addressIndex = trimmedMessage.indexOf(":");
  String addressString = trimmedMessage.substring(0,addressIndex);
  String portion = trimmedMessage.substring(addressIndex+1);
  int responseTimeoutIndex = portion.indexOf(":");
  int responseTimeout = portion.substring(0,responseTimeoutIndex).toInt();
  String messageString = portion.substring(responseTimeoutIndex+1);
  memset(message, 0, MESSAGE_MAX_SIZE);
  for(int i =0;i<messageString.length();i++)
  {
    message[i] = messageString[i];
  }
  
  Serial.print("Address: ");
  Serial.println(addressString);
  Serial.print("Response timeout: ");
  Serial.println(responseTimeout);
  Serial.print("Message: ");
  Serial.println(message); 
  recvdTextMessage="";
  recvdTextMessageCompelete=false;


  RF24NetworkHeader header(/*to node*/ addressString.toInt());
  bool ok = network.write(header,message,sizeof(message));
  if (ok)
   {
    Serial.println("Sent ok.");
    readResponse(responseTimeout);
   }
  else
    Serial.println("Sent failed.");
 
  digitalWrite(PROGRESS_PIN,false);
}



void readResponse(int responseTimeout)
{
  long startTime = millis();
  long currentTime=startTime;
  Serial.println("READ_RESPONSE_STARTED");
  bool gotResponse = false;
  while(currentTime - startTime < responseTimeout)
  {
    currentTime = millis();
    network.update();
    while ( network.available() ) {
        RF24NetworkHeader header;       
        memset(message, 0, MESSAGE_MAX_SIZE);
        network.read(header,&message,sizeof(message));
        Serial.println(message);
        if (hasTerminalCharacter(message))
        {
          gotResponse=true;
        }
    }
    if(gotResponse)break;
    delay(50);
  }
  Serial.println("READ_RESPONSE_ENDED");
}

bool hasTerminalCharacter(char message[])
{
  char terminal_char=';';
  return (String(message)).indexOf(terminal_char) >=0 ? true:false;
}

Grandchild Node

#include "Arduino.h"
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(7,8);                // nRF24L01(+) radio attached using Getting Started board
RF24Network network(radio);      // Network uses that radio
const uint16_t this_node = 9;    // Address of our node in Octal format ( 04,031, etc)
const uint16_t master_node_address = 0;   // Address of the other node in Octal format



#define BUTTON_PIN 2
bool isButtonClicked=false;
//network
#define MESSAGE_MAX_SIZE 42
char message[MESSAGE_MAX_SIZE];

void setup() {
  Serial.begin(9600);
  Serial.println("Sensor System ready");
  //network
  SPI.begin();
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);         
  radio.setAddressWidth(5);                 
  radio.setDataRate(RF24_2MBPS);//RF24_2MBPS       
  radio.setCRCLength(RF24_CRC_16 );             
  radio.setAutoAck(true);   
  network.begin(/*channel*/ 90, /*node address*/ this_node);
  pinMode(BUTTON_PIN, INPUT);   
}

void loop() {
  updateButtonStatus();
  handleCommands(); 
}

void handleCommands()
{
  network.update();
  if ( network.available() ) {     
    RF24NetworkHeader header;       
    memset(message, 0, MESSAGE_MAX_SIZE);
    network.read(header,&message,sizeof(message));
    Serial.print("Recvd command:[");
    Serial.print(message);
    Serial.println("]");
    String cmd = message;
    if (cmd.startsWith("readbutton"))
    {
      String buttonStatus = readIsButtonPressed();
      handleResponse(master_node_address,buttonStatus);
    }
  }
}


void handleResponse(int address,String response)
{
  RF24NetworkHeader header(/*to node*/ address);
  response.toCharArray(message,MESSAGE_MAX_SIZE);
  Serial.print("Sending Response: [");
  Serial.print(message);
  Serial.print("]");
  bool ok = network.write(header,message,sizeof(message)); 
  if (ok)
    Serial.println(" -> ok.");
  else
    Serial.println(" -> failed.");

  delay(50);
}


void updateButtonStatus()
{
  if (digitalRead(BUTTON_PIN) == HIGH) {         
    isButtonClicked = true;
    Serial.println("Button Pressed");
  }
}

String readIsButtonPressed()
{
  if (isButtonClicked)
  {
      isButtonClicked = false;
      return "yes";
  } 
  return "no"; 
}

I don't have anything else to suggest. I have never used the Network library and it would be a lot of trouble to set up a test example so I could learn it.

It seems that you have not posted the code of the node that is working.

...R

here is the middle node' code it acts as a relay

#include "Arduino.h"
#include "dht.h"
#include <OneWire.h> 


//network

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(7,8);                // nRF24L01(+) radio attached using Getting Started board 
RF24Network network(radio);      // Network uses that radio
const uint16_t this_node = 01;    // Address of our node in Octal format ( 04,031, etc)
const uint16_t master_node_address = 00;   // Address of the other node in Octal format





//network
#define MESSAGE_MAX_SIZE 42
char message[MESSAGE_MAX_SIZE];



void setup() {
  Serial.begin(9600);
  pinMode(PUMP_PIN, OUTPUT);
  Serial.println("Rack Cooling System ready");
  //network
  SPI.begin();
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);          
  radio.setAddressWidth(5);                  
  radio.setDataRate(RF24_2MBPS);//RF24_2MBPS        
  radio.setCRCLength(RF24_CRC_16 );              
  radio.setAutoAck(true);    
  network.begin(/*channel*/ 90, /*node address*/ this_node);
    
    
}


void loop() {

  network.update(); 
  handleAllCommands();
}

void handleAllCommands()
{
  if ( network.available() ) {     // Is there anything ready for us?

    RF24NetworkHeader header;        // If so, grab it and print it out
    memset(message, 0, MESSAGE_MAX_SIZE);
    network.read(header,&message,sizeof(message));
    Serial.print("Recvd command:[");
    Serial.print(message);
    Serial.println("]");    
  }
}

Sorry, but nothing I have read in the Network library documentation suggests that it cares whether the node addresses are entered in hex, decimal, octal, or binary as long as the number represents the appropriate octal value.

Hopefully someone who has some experience with the Network library will come along.

Is it really necessary to use the Network library? Can you describe the project you are trying to implement.

...R

I have a couple of sensors / servos / etc.. each are attached to one arduino nano and there is a master node connected to a PC through serial connection which sends commands and receives responses from the nodes.

I do not have a specific number of nodes I add them whenever i need one

I do not know exactly what happened, the nodes were acting weird specially after sending a message from the base node to the grandchild node. I re-uploaded the same code to all the nodes and they worked !!!

I really need someone to explain what happened. Could the code uploaded to the nodes have been corrupted?

Thanks

boshkash1151:
I really need someone to explain what happened. Could the code uploaded to the nodes have been corrupted?

The most likely explanation is that the code on the Arduinos was not the code you thought it was.

I have a couple of sensors / servos / etc.. each are attached to one arduino nano

If all the slaves are within range of the master then there is no need to use the Network library. Just get the master to poll each of them in turn.

...R

Robin2:
The most likely explanation is that the code on the Arduinos was not the code you thought it was.

Could it be a data corruption due to power outage for example.

Robin2:
If all the slaves are within range of the master then there is no need to use the Network library. Just get the master to poll each of them in turn.

...R

Not all slaves are within range of the master node. I even use some dummy nodes as a relay

Thanks a lot for your help in resolving my issue