Skip to content

Commit

Permalink
Update many of the example files
Browse files Browse the repository at this point in the history
- Updated gettingStarted examples to demonstrate proper pipe/address
handling by assigning a unique id to each node
- Updated gettingstarted_call_response to demonstrate 'proper' use of
ack payloads
- Added gettingstarted_handling_data example
- Added pingpair_irq_simple to demonstrate bidirectional communication
via interrupts
- Updated standard pingpair_irq example
- Use Serial.println(F()); instead of printf
  • Loading branch information
TMRh20 committed Dec 23, 2014
1 parent 350e00a commit d05a356
Show file tree
Hide file tree
Showing 9 changed files with 493 additions and 174 deletions.
147 changes: 76 additions & 71 deletions examples/GettingStarted/GettingStarted.ino
Original file line number Diff line number Diff line change
@@ -1,71 +1,65 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
//2014 - TMRh20 - Updated along with Optimized RF24 Library fork
*/

/**
* Example for Getting Started with nRF24L01+ radios.
*
* This is an example of how to use the RF24 class to communicate on a basic level. Write this sketch to two
* different nodes. Put one of the nodes into 'transmit' mode by connecting with the serial monitor and
* sending a 'T'. The ping node sends the current time to the pong node, which responds by sending the value
* back. The ping node can then see how long the whole cycle took.
* Note: For a more efficient call-response scenario see the GettingStarted_CallResponse.ino example.
* Note: When switching between sketches, the radio may need to be powered down to clear settings that are not "un-set" otherwise
*/

/*
* Getting Started example sketch for nRF24L01+ radios
* This is a very basic example of how to send data from one node to another
* Updated: Dec 2014 by TMRh20
*/

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"

// Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8
/****************** User Config ***************************/
/*** Set this radio as radio number 0 or 1 ***/
bool radioNumber = 0;

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7,8);
/**********************************************************/

byte addresses[][6] = {"1Node","2Node"};


// Set up roles to simplify testing
boolean role; // The main role variable, holds the current role identifier
boolean role_ping_out = 1, role_pong_back = 0; // The two different roles.
// Used to control whether this node is sending or receiving
bool role = 0;

void setup() {


Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/GettingStarted/\n\r");
printf("*** PRESS 'T' to begin transmitting to the other node\n\r");

// Setup and configure rf radio
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
Serial.println(F("RF24/examples/GettingStarted"));
Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));

radio.startListening(); // Start listening
radio.printDetails(); // Dump the configuration of the rf unit for debugging
}
radio.begin();

void loop(void){

// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_LOW);

// Open a writing and reading pipe on each radio, with opposite addresses
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}

// Start the radio listening for data
radio.startListening();
}

/****************** Ping Out Role ***************************/
if (role == role_ping_out) {
void loop() {


/****************** Ping Out Role ***************************/
if (role == 1) {

radio.stopListening(); // First, stop listening so we can talk.


printf("Now sending \n\r");
Serial.println(F("Now sending"));

unsigned long time = micros(); // Take the time, and send it. This will block until complete
if (!radio.write( &time, sizeof(unsigned long) )){ printf("failed.\n\r"); }
if (!radio.write( &time, sizeof(unsigned long) )){
Serial.println(F("failed"));
}

radio.startListening(); // Now, continue listening

Expand All @@ -80,58 +74,69 @@ void loop(void){
}

if ( timeout ){ // Describe the results
printf("Failed, response timed out.\n\r");
Serial.println(F("Failed, response timed out."));
}else{
unsigned long got_time; // Grab the response, compare, and send to debugging spew
radio.read( &got_time, sizeof(unsigned long) );

unsigned long time = micros();

// Spew it
printf("Sent %lu, Got response %lu, round-trip delay: %lu microseconds\n\r",time,got_time,micros()-got_time);
Serial.print(F("Sent "));
Serial.print(time);
Serial.print(F(", Got response "));
Serial.print(got_time);
Serial.print(F(", Round-trip delay "));
Serial.print(time-got_time);
Serial.println(F(" microseconds"));
}

// Try again 1s later
delay(1000);
}



/****************** Pong Back Role ***************************/

if ( role == role_pong_back )
if ( role == 0 )
{
unsigned long got_time;

if( radio.available()){
unsigned long got_time; // Variable for the received timestamp
// Variable for the received timestamp
while (radio.available()) { // While there is data ready
radio.read( &got_time, sizeof(unsigned long) ); // Get the payload
}
}

radio.stopListening(); // First, stop listening so we can talk
radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
radio.startListening(); // Now, resume listening so we catch the next packets.
printf("Sent response %lu \n\r", got_time);
Serial.print(F("Sent response "));
Serial.println(got_time);
}
}




/****************** Change Roles via Serial Commands ***************************/

if ( Serial.available() )
{
char c = toupper(Serial.read());
if ( c == 'T' && role == role_pong_back )
{
printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");

role = role_ping_out; // Become the primary transmitter (ping out)
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);

}
else if ( c == 'R' && role == role_ping_out )
{
printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");

role = role_pong_back; // Become the primary receiver (pong back)
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
if ( c == 'T' && role == 0 ){
Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
role = 1; // Become the primary transmitter (ping out)

}else
if ( c == 'R' && role == 1 ){
Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
role = 0; // Become the primary receiver (pong back)
radio.startListening();

}
}
}


} // Loop

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
Parts derived from examples by J. Coliz <maniacbug@ymail.com>
Dec 2014 - TMRh20 - Updated
Derived from examples by J. Coliz <maniacbug@ymail.com>
*/
/**
* Example for efficient call-response using ack-payloads
Expand All @@ -13,12 +13,16 @@
*/

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//#include "printf.h"

// Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8
/****************** User Config ***************************/
/*** Set this radio as radio number 0 or 1 ***/
bool radioNumber = 0;

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7,8);
/**********************************************************/
// Topology
byte addresses[][6] = {"1Node","2Node"}; // Radio pipe addresses for the 2 nodes to communicate.

Expand All @@ -33,24 +37,28 @@ byte counter = 1; // A

void setup(){

Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/GettingStarted/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
printf("*** PRESS 'T' to begin transmitting to the other node\n\r");

Serial.begin(115200);
Serial.println(F("RF24/examples/GettingStarted_CallResponse"));
Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
//printf_begin();
// Setup and configure radio

radio.begin();
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.enableAckPayload(); // Allow optional ack payloads
radio.setRetries(0,15); // Smallest time between retries, max no. of retries
radio.setPayloadSize(1); // Here we are sending 1-byte payloads to test the call-response speed
radio.openWritingPipe(addresses[1]); // Both radios listen on the same pipes by default, and switch when writing
radio.openReadingPipe(1,addresses[0]); // Open a reading pipe on address 0, pipe 1
radio.startListening(); // Start listening
radio.powerUp();
radio.printDetails(); // Dump the configuration of the rf unit for debugging

radio.enableAckPayload(); // Allow optional ack payloads
radio.enableDynamicAck();

if(radioNumber){
radio.openWritingPipe(addresses[1]); // Both radios listen on the same pipes by default, but opposite addresses
radio.openReadingPipe(1,addresses[0]); // Open a reading pipe on address 0, pipe 1
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
radio.startListening(); // Start listening

radio.writeAckPayload(1,&counter,1); // Pre-load an ack-paylod into the FIFO buffer for pipe 1
//radio.printDetails();
}

void loop(void) {
Expand All @@ -63,21 +71,31 @@ void loop(void) {
byte gotByte; // Initialize a variable for the incoming response

radio.stopListening(); // First, stop listening so we can talk.
printf("Now sending %d as payload. ",counter); // Use a simple byte counter as payload
Serial.print(F("Now sending ")); // Use a simple byte counter as payload
Serial.println(counter);

unsigned long time = micros(); // Record the current microsecond count

if ( radio.write(&counter,1) ){ // Send the counter variable to the other radio
if(!radio.available()){ // If nothing in the buffer, we got an ack but it is blank
printf("Got blank response. round-trip delay: %lu microseconds\n\r",micros()-time);
Serial.print(F("Got blank response. round-trip delay: "));
Serial.print(micros()-time);
Serial.println(F(" microseconds"));
}else{
while(radio.available() ){ // If an ack with payload was received
radio.read( &gotByte, 1 ); // Read it, and display the response time
printf("Got response %d, round-trip delay: %lu microseconds\n\r",gotByte,micros()-time);
unsigned long timer = micros();

Serial.print(F("Got response "));
Serial.print(gotByte);
Serial.print(F(" round-trip delay: "));
Serial.print(timer-time);
Serial.println(F(" microseconds"));
counter++; // Increment the counter variable
}
}

}else{ printf("Sending failed.\n\r"); } // If no ack response, sending failed
}else{ Serial.println(F("Sending failed.")); } // If no ack response, sending failed

delay(1000); // Try again later
}
Expand All @@ -90,9 +108,10 @@ void loop(void) {
while( radio.available(&pipeNo)){ // Read all available payloads
radio.read( &gotByte, 1 );
// Since this is a call-response. Respond directly with an ack payload.
// Ack payloads are much more efficient than switching to transmit mode to respond to a call
gotByte += 1; // Ack payloads are much more efficient than switching to transmit mode to respond to a call
radio.writeAckPayload(pipeNo,&gotByte, 1 ); // This can be commented out to send empty payloads.
printf("Sent response %d \n\r", gotByte);
Serial.print(F("Loaded next response "));
Serial.println(gotByte);
}
}

Expand All @@ -103,22 +122,18 @@ void loop(void) {
if ( Serial.available() )
{
char c = toupper(Serial.read());
if ( c == 'T' && role == role_pong_back )
{
printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");

role = role_ping_out; // Change roles (ping out)
radio.openWritingPipe(addresses[0]); // Open different pipes when writing. Write on pipe 0, address 0
radio.openReadingPipe(1,addresses[1]); // Read on pipe 1, as address 1
}
else if ( c == 'R' && role == role_ping_out )
{
printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");

role = role_pong_back; // Become the primary receiver (pong back)
radio.openWritingPipe(addresses[1]); // Since only two radios involved, both listen on the same addresses and pipe numbers in RX mode
radio.openReadingPipe(1,addresses[0]); // then switch pipes & addresses to transmit.
radio.startListening(); // Need to start listening after opening new reading pipes
if ( c == 'T' && role == role_pong_back ){
Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
role = role_ping_out; // Become the primary transmitter (ping out)
counter = 1;
}else
if ( c == 'R' && role == role_ping_out ){
Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
role = role_pong_back; // Become the primary receiver (pong back)
radio.startListening();
counter = 1;
radio.writeAckPayload(1,&counter,1);

}
}
}
Loading

1 comment on commit d05a356

@TMRh20
Copy link
Member Author

@TMRh20 TMRh20 commented on d05a356 Dec 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#37

Please sign in to comment.