How to send strings using NRF24 library for arduino mega

7,259 views
Skip to first unread message

Shiu Kumar

unread,
Jan 2, 2013, 8:32:14 AM1/2/13
to nrf24-...@googlegroups.com
Hello

I am using Arduino Mega 2560 with NRF24L01+ module. The ping pair time example works fine giving an average round trip time of 768 microseconds.

But for my research, I require sending strings. I am new to using the NFR24 library. Can anyone provide some hints or codes for sending and receiving strings as there are no examples given.

Mike McCauley

unread,
Jan 2, 2013, 8:16:51 PM1/2/13
to nrf24-...@googlegroups.com, Shiu Kumar
Hello,
If you have NUL terminated strings you could do it something like this:

char* msg = "this is the message";

if (!nrf24.send((uint8_t*)msg, strlen(msg)))
Serial.println("send failed");

Make sure you dont exceed the max message length.
On the receive side, you may need to add NUL to the end of the message if you
plan to use print, str* or similar functions with the received string.


Cheers.

--
Mike McCauley mi...@open.com.au
Open System Consultants Pty. Ltd
9 Bulbul Place Currumbin Waters QLD 4223 Australia http://www.open.com.au
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Radiator: the most portable, flexible and configurable RADIUS server
anywhere. SQL, proxy, DBM, files, LDAP, NIS+, password, NT, Emerald,
Platypus, Freeside, TACACS+, PAM, external, Active Directory, EAP, TLS,
TTLS, PEAP, TNC, WiMAX, RSA, Vasco, Yubikey, MOTP, HOTP, TOTP,
DIAMETER etc. Full source on Unix, Windows, MacOSX, Solaris, VMS, NetWare etc.

Shiu Kumar

unread,
Jan 2, 2013, 9:32:47 PM1/2/13
to nrf24-...@googlegroups.com
Given are the codes that i have for sending and receiving a string (Hello World).

The sender gives me waitPacketSent failed while the receiver on displays initialized.

Can someone tell me what is wrong.

Sender Code:

#include <NRF24.h>
#include <SPI.h>

// Singleton instance of the radio
NRF24 nrf24;

void setup() 
{
  Serial.begin(9600);

  if (!nrf24.init())
    Serial.println("NRF24 init failed");

  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");

  if (!nrf24.setThisAddress((uint8_t*)"clie1", 5))
    Serial.println("setThisAddress failed");

  if (!nrf24.setPayloadSize(sizeof(11)))
    Serial.println("setPayloadSize failed");

  if (!nrf24.setRF(NRF24::NRF24DataRate2Mbps, NRF24::NRF24TransmitPower0dBm))
    Serial.println("setRF failed");    

  Serial.println("initialised");
}

void loop()
{
  // Send some data to the server
  if (!nrf24.setTransmitAddress((uint8_t*)"serv1", 5))
    Serial.println("setTransmitAddress failed");

  char* msg = "Hello World";
  
  if (!nrf24.send((uint8_t*)msg,strlen(msg)))
      Serial.println("send failed");  
  if (!nrf24.waitPacketSent())
      Serial.println("waitPacketSent failed"); 
  
  Serial.println(time);   
  delay(2000);
}



Receiver Code:

#include <NRF24.h>
#include <SPI.h>

// Singleton instance of the radio
NRF24 nrf24;

void setup() 
{
  Serial.begin(9600);
  if (!nrf24.init())
    Serial.println("NRF24 init failed");

 if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");

  if (!nrf24.setThisAddress((uint8_t*)"serv1", 5))
    Serial.println("setThisAddress failed");

  if (!nrf24.setPayloadSize(sizeof(11)))
    Serial.println("setPayloadSize failed");

  if (!nrf24.setRF(NRF24::NRF24DataRate2Mbps, NRF24::NRF24TransmitPower0dBm))
    Serial.println("setRF failed");    

  Serial.println("initialised");
}

void loop()
{
  nrf24.waitAvailable();
  // ping_client sends us an unsigned long containing its timestamp
  unsigned long data;
  char data[11];
  uint8_t len;
  if (!nrf24.recv((uint8_t*)data, &len))
    Serial.println("read failed");
   Serial.println(data);
}


.........................................................................................................................................................................................................................

Mike McCauley

unread,
Jan 2, 2013, 10:35:52 PM1/2/13
to nrf24-...@googlegroups.com

Hello,

 

before you call nrf24.recv((uint8_t*)data, &len)

you must ensure that len is set to the amount of space available in the data buffer.

After the call, len will be set to the actual amount of data copied.

 

so, you must have something like this (untested)

 

  char data[11];

  uint8_t len = 10;

  if (!nrf24.recv((uint8_t*)data, &len))

    Serial.println("read failed");

 

// terminate the string

data[len] = 0;

Serial.println(data);

 

Cheers.

--

Shiu Kumar

unread,
Jan 3, 2013, 1:21:32 AM1/3/13
to nrf24-...@googlegroups.com
Hello.

Thanks a lot. Finally I managed to get the code working for sending and receiving strings.

However, the code works without specifying the len size (uint8_t len = 10;) and there was no need to terminate the data (data[len] = 0;).

Just a question, what is the purpose of the '&' before data in the examples for the following line of codes:
nrf24.recv((uint8_t*)&data, &len)

Thanks again as the information provided was very useful.

Cheers

Mike McCauley

unread,
Jan 3, 2013, 1:35:11 AM1/3/13
to nrf24-...@googlegroups.com
On Wednesday, January 02, 2013 10:21:32 PM Shiu Kumar wrote:
> Hello.
>
> Thanks a lot. Finally I managed to get the code working for sending and
> receiving strings.
>
> However, the code works without specifying the len size (uint8_t len = 10;)
> and there was no need to terminate the data (data[len] = 0;).

Thats probably a happy accident.
If you understand how this works you will see that it is correct to terminate
the string, since the NUL termination is not transmitted.

>
> Just a question, what is the purpose of the '&' before data in the examples
> for the following line of codes:
> nrf24.recv((uint8_t*)&data, &len)

& takes the address of a variable.
If data is an array, you should not need to take its address.

Perhaps its time to study your C programming?

Cheers.
> > Mike McCauley mi...@open.com.au <javascript:>

Thien Tran

unread,
Dec 2, 2013, 6:51:08 AM12/2/13
to nrf24-...@googlegroups.com
Hello Shiu Kumar
thanks for sharing
I used your codes to test,however the receiver displays "He"
Please,show me wrong in this code:

sender

#include <NRF24.h>
#include <SPI.h>

// Singleton instance of the radio
NRF24 nrf24;

void setup() 
{
  Serial.begin(9600);

  if (!nrf24.init())
    Serial.println("NRF24 init failed");

  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");

  if (!nrf24.setThisAddress((uint8_t*)"clie1", 5))
    Serial.println("setThisAddress failed");

  if (!nrf24.setPayloadSize(sizeof(11)))
    Serial.println("setPayloadSize failed");

  if (!nrf24.setRF(NRF24::NRF24DataRate2Mbps, NRF24::NRF24TransmitPower0dBm))
    Serial.println("setRF failed");    

  Serial.println("initialised");
}

void loop()
{
  // Send some data to the server
  if (!nrf24.setTransmitAddress((uint8_t*)"serv1", 5))
    Serial.println("setTransmitAddress failed");

  char* msg = "Hello World";
  
  if (!nrf24.send((uint8_t*)msg,sizeof(msg)))
      Serial.println("send failed");  
  if (!nrf24.waitPacketSent())
      Serial.println("waitPacketSent failed"); 
  
  Serial.println("send");   
  delay(2000);
}

Receiver

#include <NRF24.h>
#include <SPI.h>

// Singleton instance of the radio
NRF24 nrf24;

void setup() 
{
  Serial.begin(9600);
  if (!nrf24.init())
    Serial.println("NRF24 init failed");

 if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");

  if (!nrf24.setThisAddress((uint8_t*)"serv1", 5))
    Serial.println("setThisAddress failed");

  if (!nrf24.setPayloadSize(sizeof(11)))
    Serial.println("setPayloadSize failed");

  if (!nrf24.setRF(NRF24::NRF24DataRate2Mbps, NRF24::NRF24TransmitPower0dBm))
    Serial.println("setRF failed");    

  Serial.println("initialised");
}

void loop()
{
  nrf24.waitAvailable();
  // ping_client sends us an unsigned long containing its timestamp
  char data[11];
  uint8_t len = sizeof(data);
  if (!nrf24.recv((uint8_t*)&data, &len))
    Serial.println("read failed");
//data[len] = 0;
   Serial.println(data);
}

Colin Cooper

unread,
Dec 2, 2013, 7:56:40 AM12/2/13
to nrf24-...@googlegroups.com

in sender: nrf24.setPayloadSize(sizeof(11))

sizeof(11) is the storagesize of the int 11 (i.e. probably 2)-- just set it to the size e.g. 11

--
You received this message because you are subscribed to the Google Groups "NRF24-Arduino" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nrf24-arduin...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Arpit Gupta

unread,
Feb 20, 2014, 12:50:57 PM2/20/14
to nrf24-...@googlegroups.com
hi
shiu kumar
i tried the code written  by you but nothing gets printed.
can you provide me the working codes fore sending and receiving strings.
it will be a great help.
thanks in advance

Mike McCauley

unread,
Feb 20, 2014, 3:00:26 PM2/20/14
to nrf24-...@googlegroups.com
Hello,

your first step should be to use the examples provided to test your hardware.
If they are not working properly, you probably have an electrrical connection
problem.

Cheers.
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com

川崎春と

unread,
Oct 30, 2015, 9:19:44 PM10/30/15
to NRF24-Arduino
Excelent code, is very useful thanks, but I have a problem, the menssage "Hello World" when is received , show only the 2 first letters "He" and other symbols, I don't find the error, thanks can you help me, thanks a lot, good code.


Mike McCauley

unread,
Oct 30, 2015, 9:23:51 PM10/30/15
to nrf24-...@googlegroups.com
Hello,

This library is now obsolete and is replaced by RadioHead

http://www.airspayce.com/mikem/arduino/RadioHead/

Cheers.
Reply all
Reply to author
Forward
0 new messages