Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method AKTT::controller() should not be called statically in /home2/walltech/public_html/wp-includes/class-wp-hook.php on line 298
Transmitting Strings with the nRF24L01 – WΛLLTΞCH

Hello! A while back I saw some people doing some awesome things with nRF24L01 radio transceivers and wanted to get in on the action. I got some tiny smd modules and tried to get them to transmit an analog reading between two arduinos, and found that the examples didn’t actually send any data! I looked for projects online, but the sending was buried in project code, it wasn’t posted, it was full of errors, or it just wasn’t worth using. Finally, Mike Rankin, the guy who builds the OLED watch displays, built a little nRF24L01 setup and shared the code with me. After some cleanup, I could transmit an analog reading from one arduino and read it on another and light LEDs along with the voltage level.

IMAG0489

The transmitter

IMAG0470The receiver

Transmitting Strings

For a project I’m doing, I needed a link between an Arduino UNO and a YUN, the last of which is very picky about what you hook up to it, so serial, bluetooth, and everything else was out of the question. These radios hook up by SPI, a solid data connection, so figured it could work. It did, and very well. After hooking them together, I could transmit numbers just fine, but what I really needed was text. I spent ages trying to figure out how to send strings over the connection, but no one anywhere had done it and had code posted. What I did find however, were to halves of programs that I could combine into one. One half was the code I had written to transmit numbers that I slightly modified, and the other was a snippet that could convert strings into characters, and send each character’s binary representation across as 8 bits, or a complete byte in a packet to be reassembled on the other side when an end character is received. This worked, and very well too. Here’s my complete code, the only one on the web to transmit strings 🙂

Transmitting Arduino

/**
 *
 * Pins:
 * Hardware SPI:
 * MISO -> 12
 * MOSI -> 11
 * SCK -> 13
 *
 * Configurable:
 * CE -> 8
 * CSN -> 7
 */

#include "SPI.h"
#include "Mirf.h"
#include "nRF24L01.h"
#include "MirfHardwareSpiDriver.h"

// converts a float into a char 
// and sends it via nRF24L01
void transmit( float v)
{
  byte c; 
  char buf[10];
  
  dtostrf(v,9,3,buf);

  for( int i=0 ; i (PUT LESS THAN SIGN HERE) 8 ; i++ )
  { 
    c = buf[i];
    Mirf.send(&c);
    while( Mirf.isSending() ) ;
  }
}

// sends a string via the nRF24L01
void transmit(const char *string)
{
  byte c; 
  
  for( int i=0 ; string[i]!=0x00 ; i++ )
  { 
    c = string[i];
    Mirf.send(&c);
    while( Mirf.isSending() ) ;
  }
}

// send a CR/LF sequence via the nRF24L01
void transmitlf(void)
{
  byte c;
  
  c = '\r';
  Mirf.send(&c);
    while( Mirf.isSending() ) ;
  
  c = '\n';
  Mirf.send(&c);
    while( Mirf.isSending() ) ;
}

void setup()
{
  Serial.begin(9600);
  /*
   * Setup pins / SPI.
   */
   
  /* To change CE / CSN Pins:
   * 
   * Mirf.csnPin = 9;
   * Mirf.cePin = 7;
   */
  
  Mirf.cePin = 8;
  Mirf.csnPin = 7;
  
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  
  /*
   * Configure reciving address.
   */
   
  Mirf.setRADDR((byte *)"serv1");
  
  /*
   * Set the payload length to sizeof(unsigned long) the
   * return type of millis().
   *
   * NB: payload on client and server must be the same.
   */
   
  Mirf.payload = 1;
  
  /*
   * Write channel and payload config then power up reciver.
   */
   
   
  Mirf.config();
}

void loop() 
{

  transmit("Hello World!~");

 
  // ... just take your time
  delay(1000);
}

File
RADIOSEND

 

Receiving Arduino

/**
 * Pins:
 * Hardware SPI:
 * MISO -> 12
 * MOSI -> 11
 * SCK -> 13
 *
 * Configurable:
 * CE -> 8
 * CSN -> 7
 */

#include "SPI.h"
#include "Mirf.h"
#include "nRF24L01.h"
#include "MirfHardwareSpiDriver.h"

String message;

void setup(){
  Serial.begin(9600);
  /*
   * Setup pins / SPI.
   */
   
  /* To change CE / CSN Pins:
   * 
   * Mirf.csnPin = 9;
   * Mirf.cePin = 7;
   */
  
  Mirf.cePin = 8;
  Mirf.csnPin = 9;
  
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  
  /*
   * Configure reciving address.
   */
   
  Mirf.setRADDR((byte *)"serv1");
  
  /*
   * Set the payload length to sizeof(unsigned long) the
   * return type of millis().
   *
   * NB: payload on client and server must be the same.
   */
   
  Mirf.payload = 1;
  
  /*
   * Write channel and payload config then power up reciver.
   */
   
  Mirf.config();
}

void loop(){
   byte c; 
   
  // is there any data pending? 
  if(Mirf.dataReady())
  {
     // well, get it
     Mirf.getData(&c);

    // ... and write it out to the PC
     char letter = char(c);
     if(letter != '~')
     {
       message = String(message + letter);
     }
     
     else 
     {
       Serial.println(message);
       message = "";
     }
  }
} 

File

RADIORECIEVE

Transmitting Strings with the nRF24L01