Pages: [1]   Go Down
Author Topic: RF24Network and nRF24L01+ (send a string)  (Read 2301 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi to all!

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

https://maniacbug.wordpress.com/2012/03/30/rf24network/

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 smiley-wink
Meanwhile I've a problem with a part of it:

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

The reference (http://maniacbug.github.com/RF24Network/classRF24Network.html#a6b90e07f0f89d64af07b5da0b31e3c87) says:
Code:
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:

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

On serial output I obtain:

Code:
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
Logged

France
Offline Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

can you try with :
Code:
return network.write(header,myString,sizeof(myString));


Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

can you try with :
Code:
return network.write(header,myString,sizeof(myString));




Nope. I obtain:

Code:
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:
Code:
/**
 * 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:
Code:
/**
 * 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:
Code:
/**
 * 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:
Code:
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:

Code:
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?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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...

Code:
---------------------------------
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
Logged

France
Offline Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
God Member
*****
Karma: 17
Posts: 569
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
char * sendData = "1234567890";

Code:
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.

Code:
char * receivedData = "           "; //see how I have to give it a blank string. it doesn't work without that.

Code:
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.
Logged

France
Offline Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

That not bad but you could use :

Code:
char message[10];

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

so from may be with :
Code:
char message[]="0123456789";

in the TX file and :

Code:
char message[10];

in the RX file wil help you.
Logged

Pages: [1]   Go Up
Jump to: