RF24Network and nRF24L01+ (send a string)

Hi to all!

I'm trying to comunicate with two nRF24L01+ using the library read on the following site:

The example in the library works: (http://maniacbug.github.com/RF24Network/meshping_8pde-example.html). Well, I cannot fully understand all the code, ok... I'll learn it :wink:
Meanwhile I've a problem with a part of it:

network.write(header,&message,sizeof(unsigned long));

The reference (http://maniacbug.github.com/RF24Network/classRF24Network.html#a6b90e07f0f89d64af07b5da0b31e3c87) says:

bool RF24Network::write	(	RF24NetworkHeader & 	header,
const void * 	message,
size_t 	len 
)		
Send a message.

Parameters:
[in,out]	header	The header (envelope) of this message. The critical thing to fill in is the to_node field so we know where to send the message. It is then updated with the details of the actual header sent.
message	Pointer to memory where the message is located
len	The size of the message
Returns:
Whether the message was successfully received

But I would like to send a string, not a number. Something like this:

  myString = "hello";
  printf_P(PSTR("String %s\n\r"), myString);
  return network.write(header,&myString,sizeof(myString));

On serial output I obtain:

6000: APP Sending 6000 to 00...
String hello
6003: APP Send ok
6006: APP Received 256 from 00

Seems 256 is a return_code or whatever, but not the "hello" message I would like to send. Could someone help me? Thank's!

Simon

can you try with :

return network.write(header,myString,sizeof(myString));

Grag38:
can you try with :

return network.write(header,myString,sizeof(myString));

Nope. I obtain:

16000: APP Sending 16000 to 00...
String hello
16003: APP Send ok
16006: APP Received 25960 from 00

But I've surely identified another problem. The receiving funtion saves the result to a long int. This is not good.

original send function:

/**
 * Send a 'T' message, the current time
 */
bool send_T(uint16_t to)
{
  RF24NetworkHeader header(/*to node*/ to, /*type*/ 'T' /*Time*/);
  
  // The 'T' message that we send is just a ulong, containing the time
  unsigned long message = millis();
  printf_P(PSTR("---------------------------------\n\r"));
  printf_P(PSTR("%lu: APP Sending %lu to 0%o...\n\r"),millis(),message,to);
  return network.write(header,&message,sizeof(unsigned long));
}

original receiving function:

/**
 * Handle a 'T' message
 *
 * Add the node to the list of active nodes
 */
void handle_T(RF24NetworkHeader& header)
{
  // The 'T' message is just a ulong, containing the time
  unsigned long message;
  network.read(header,&message,sizeof(unsigned long));
  printf_P(PSTR("%lu: APP Received %lu from 0%o\n\r"),millis(),message,header.from_node);

  // If this message is from ourselves or the base, don't bother adding it to the active nodes.
  if ( header.from_node != this_node || header.from_node > 00 )
    add_node(header.from_node);
}

The 'handle_T' must be corrected as well.

My modified version:

send:

/**
 * Send a 'T' message, the current time
 */
bool send_T(uint16_t to)
{
  RF24NetworkHeader header(/*to node*/ to, /*type*/ 'T' /*Time*/);
  
  // The 'T' message that we send is just a ulong, containing the time
  unsigned long message = millis();
  printf_P(PSTR("---------------------------------\n\r"));
  printf_P(PSTR("%lu: APP Sending %lu to 0%o...\n\r"),millis(),message,to);
  
  // simo
  myString = "hello";
  
  printf_P(PSTR("Stringa %s\n\r"), myString);
  
  return network.write(header,myString,sizeof(myString));
  
  //return network.write(header,&message,sizeof(unsigned long));
}

receive:

void handle_T(RF24NetworkHeader& header)
{
  // The 'T' message is just a ulong, containing the time
  unsigned long message;
  
  //network.read(header,&message,sizeof(unsigned long));
  //printf_P(PSTR("%lu: APP Received %lu from 0%o\n\r"),millis(),message,header.from_node);
  
  //simo
  char *receivedString;
  network.read(header,receivedString,sizeof(receivedString));
  printf_P(PSTR("%lu: APP Received %s from 0%o\n\r"),millis(),receivedString,header.from_node);

  // If this message is from ourselves or the base, don't bother adding it to the active nodes.
  if ( header.from_node != this_node || header.from_node > 00 )
    add_node(header.from_node);
}

Result:

2000: APP Sending 2000 to 00...
Stringa hello
2003: APP Send ok
12829108005: APP Received  from 00
---------------------------------
4000: APP Sending 4000 to 00...
Stringa hello
4003: APP Send ok
12829108205: APP Received  from 00

Now the receiving variable is empty?

Waitwaitwait, now it works!

Instead using a new char variable defined locally on the function, I've used the same variable used globally on the sketch, also 'myString'. Now it's so:

receiving function:

void handle_T(RF24NetworkHeader& header)
{
  // The 'T' message is just a ulong, containing the time
  unsigned long message;
  
  //network.read(header,&message,sizeof(unsigned long));
  //printf_P(PSTR("%lu: APP Received %lu from 0%o\n\r"),millis(),message,header.from_node);
  
  //simo
  network.read(header,myString,sizeof(myString));
  printf_P(PSTR("%lu: APP Received %s from 0%o\n\r"),millis(),myString,header.from_node);

  // If this message is from ourselves or the base, don't bother adding it to the active nodes.
  if ( header.from_node != this_node || header.from_node > 00 )
    add_node(header.from_node);
}

and...

---------------------------------
122000: APP Sending 122000 to 00...
Stringa hello
122003: APP Send ok
122007: APP Received hello from 00
---------------------------------

Great! It works! I'll test it a little bit. Thank's!

Simon

Nice ! You got it.

Have pleasure working with these marvellous modules that are NRF24. I use them until 80 meters far and it works great !

Grag38:
Nice ! You got it.

Have pleasure working with these marvellous modules that are NRF24. I use them until 80 meters far and it works great !

Actually I'm using these modules (I have 6 of them, 2 in test) without external antenna, downgraded to 250KB speed.
Could you explain me a little bit how have you designed your network?
I would like to have a central station that could receive sensor status from the leaves (and this now works) but the same time that could send messages to them in case I would like to turn on/off a relay or whatever.

Thank's!

Simon

I'm new to this myself, but having a similar problem.

With your 'fix' above, it appears you are running the TX and RX on the same MCU? If so then aren't you bypassing the RX anyway and just printing out the myString char that you defined globally as 'hello'?

I'm having a similar issue, where I seem to be having trouble with what should be the simplest thing, sending a string.

I have one MCU as TX and the other as RX. I'm attempting to send a string and it just doesn't work how I'd expect.

char * sendData = "1234567890";
void loop(void)
{
  // 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;

    Serial.print("Sending...");
    RF24NetworkHeader header(/*to node*/ other_node);
    bool ok = network.write(header,sendData,strlen(sendData));
    Serial.print("Sending - ");
    Serial.print(sendData);
    Serial.print(" - ");
    Serial.print(strlen(sendData));
    Serial.print(" bytes long. ");
    Serial.println();
    if (ok)
      Serial.println("ok.");
    else
      Serial.println("failed.");
  }

At the RX node I find that I get nothing unless I define the receivedData as a blank string, i.e. 10 spaces. That won't adapt to whatever string I happen to send though. If I don't set receivedData to something at the start then I get only 1 or 2 characters through and usually not what I'm expecting.

char * receivedData = "           "; //see how I have to give it a blank string. it doesn't work without that.
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;;
    network.read(header,receivedData,strlen(receivedData);
    Serial.print("Data Received - ");
    Serial.print(receivedData);
    Serial.print(" - Size ");
    Serial.print(strlen(receivedData));
    Serial.println();
  }
}

This should be so simple, but I'm having a severe headache now.

When you define : char *message="0123456789";
It only create a pointer of a char.

That not bad but you could use :

char message[10];

that will create a 10 space memory for the variable message.

so from may be with :

char message[]="0123456789";

in the TX file and :

 char message[10];

in the RX file wil help you.