rf24-pio/examples/pingpair/pingpair.pde

218 lines
5.1 KiB
Plaintext
Raw Normal View History

2011-03-19 03:32:34 +00:00
/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
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.
*/
/**
* Example RF Radio Ping Pair
*
2011-04-04 03:28:54 +00:00
* This is an example of how to use the RF24 class. Write this sketch to two different nodes,
* connect the role_pin to ground on one. 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.
2011-03-19 03:32:34 +00:00
*/
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 8 & 9
RF24 radio(8,9);
2011-04-02 05:56:46 +00:00
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
2011-07-09 05:15:37 +00:00
// Leave open to be the 'ping' transmitter
const int role_pin = 7;
2011-03-19 03:32:34 +00:00
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes
2011-03-19 03:32:34 +00:00
// in this system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
2011-03-19 03:32:34 +00:00
//
// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;
2011-03-19 03:32:34 +00:00
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
2011-03-19 03:32:34 +00:00
// The role of the current running sketch
role_e role;
void setup(void)
{
//
// Role
2011-03-19 03:32:34 +00:00
//
2011-07-09 05:15:37 +00:00
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
2011-07-09 05:15:37 +00:00
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_ping_out;
2011-04-02 05:56:46 +00:00
else
role = role_pong_back;
2011-03-19 03:32:34 +00:00
//
// Print preamble
//
2011-07-09 05:15:37 +00:00
2011-06-28 04:06:08 +00:00
Serial.begin(57600);
2011-03-19 03:32:34 +00:00
printf_begin();
printf("\n\rRF24/examples/pingpair/\n\r");
2011-03-19 03:32:34 +00:00
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Setup and configure rf radio
//
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
radio.begin();
2011-06-28 13:44:41 +00:00
// optionally, increase the delay between retries & # of retries
2011-07-09 05:15:37 +00:00
radio.setRetries(15,15);
2011-06-28 13:44:41 +00:00
// optionally, use a high channel to avoid WiFi chatter
radio.setChannel(110);
2011-06-30 04:38:17 +00:00
// optionally, increase the CRC length for improved reliability
radio.setCRCLength(RF24_CRC_16);
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(8);
2011-03-19 03:32:34 +00:00
//
// Open pipes to other nodes for communication
2011-03-19 03:32:34 +00:00
//
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
2011-07-09 05:15:37 +00:00
if ( role == role_ping_out )
{
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
2011-03-19 03:32:34 +00:00
else
{
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
2011-03-19 03:32:34 +00:00
//
// Start listening
//
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
radio.startListening();
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
//
// Dump the configuration of the rf unit for debugging
//
2011-07-09 05:15:37 +00:00
radio.printDetails();
2011-03-19 03:32:34 +00:00
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
2011-03-19 03:32:34 +00:00
//
2011-07-09 05:15:37 +00:00
if (role == role_ping_out)
2011-03-19 03:32:34 +00:00
{
// First, stop listening so we can talk.
radio.stopListening();
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// Take the time, and send it. This will block until complete
unsigned long time = millis();
printf("Now sending %lu...",time);
2011-07-09 05:15:37 +00:00
radio.write( &time, sizeof(unsigned long) );
2011-03-19 03:32:34 +00:00
// Now, continue listening
radio.startListening();
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 250 )
timeout = true;
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// Describe the results
if ( timeout )
{
printf("Failed, response timed out.\n\r");
}
else
{
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
radio.read( &got_time, sizeof(unsigned long) );
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// Spew it
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
}
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// Try again 1s later
delay(1000);
}
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
//
// Pong back role. Receive each packet, dump it out, and send it back
2011-03-19 03:32:34 +00:00
//
2011-07-09 05:15:37 +00:00
if ( role == role_pong_back )
2011-03-19 03:32:34 +00:00
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
unsigned long got_time;
bool done = false;
2011-03-19 03:32:34 +00:00
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// Spew it
printf("Got payload %lu...",got_time);
}
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// First, stop listening so we can talk
radio.stopListening();
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// Send the final one back.
2011-07-09 05:15:37 +00:00
radio.write( &got_time, sizeof(unsigned long) );
2011-03-19 03:32:34 +00:00
printf("Sent response.\n\r");
2011-07-09 05:15:37 +00:00
2011-03-19 03:32:34 +00:00
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
}
2011-06-28 13:44:41 +00:00
// vim:cin:ai:sts=2 sw=2 ft=cpp