Optimized high speed nRF24L01+ driver class documentation v1.4.10
TMRh20 2020 - Optimized fork of the nRF24L01+ driver
Loading...
Searching...
No Matches
examples/old_backups/pingpair_dyn/pingpair_dyn.ino

This is an example of how to use payloads of a varying (dynamic) size on Arduino.

1/*
2 * Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
7 */
8
15#include <SPI.h>
16#include "nRF24L01.h"
17#include "RF24.h"
18#include "printf.h"
19
20// Hardware configuration
21RF24 radio(7, 8); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
22
23// Radio pipe addresses for the 2 nodes to communicate.
24const uint64_t addresses[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
25
26/************************* Role management ****************************/
27// Set up role. This sketch uses the same software for all the nodes in this
28// system. Doing so greatly simplifies testing.
29
30// The role_pin is a digital input pin used to set the role of this radio.
31// Connect the role_pin to GND to be the 'pong' receiver
32// Leave the role_pin open to be the 'ping' transmitter
33const short role_pin = 5; // use pin 5
34typedef enum { role_ping_out = 1,
35 role_pong_back } role_e; // The various roles supported by this sketch
36const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back" }; // The debug-friendly names of those roles
37role_e role; // The role of the current running sketch
38
39
40// variables used for changing the payload size dynamically (used when role == role_ping_out)
41const int min_payload_size = 4;
42const int max_payload_size = 32;
43const int payload_size_increment = 1;
44int send_payload_size = min_payload_size;
45
46char receive_payload[max_payload_size + 1]; // +1 to allow room for a terminating NULL char
47
48void setup(void) {
49 pinMode(role_pin, INPUT); // set up the role pin
50 digitalWrite(role_pin, HIGH);
51 delay(20); // Just to get a solid reading on the role pin
52
53 // read the role_pin, establish our role
54 if (digitalRead(role_pin)) {
55 role = role_ping_out;
56 } else {
57 role = role_pong_back;
58 }
59
60 Serial.begin(115200);
61 printf_begin(); // needed for printDetails()
62
63 // Print preamble
64 Serial.println(F("RF24/examples/pingpair_dyn/"));
65 Serial.print(F("ROLE: "));
66 Serial.println(role_friendly_name[role]);
67
68 // Setup and configure rf radio
69 radio.begin();
70 radio.enableDynamicPayloads(); // Enable dynamic payloads
71 radio.setRetries(5, 15); // delay between retries = 5 * 250 + 250 = 1500 microseconds, number of retries = 15
72
73 // Open a writing and reading pipe on each radio, with opposite addresses
74 if (role == role_ping_out) {
75 radio.openWritingPipe(addresses[0]);
76 radio.openReadingPipe(1, addresses[1]);
77 } else {
78 radio.openWritingPipe(addresses[1]);
79 radio.openReadingPipe(1, addresses[0]);
80 }
81
82 radio.startListening(); // Start listening
83 radio.printDetails(); // Dump the configuration of the rf unit for debugging
84}
85
86void loop() {
87
88
89 /****************** Ping Out Role ***************************/
90
91 if (role == role_ping_out) {
92 // The payload will always be the same, what will change is how much of it we send.
93 static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
94
95 radio.stopListening(); // First, stop listening so we can talk.
96
97 // Send the payload
98 Serial.print(F("Now sending length "));
99 Serial.println(send_payload_size);
100 radio.write(send_payload, send_payload_size); // This will block until complete
101
102 radio.startListening(); // Now, continue listening
103
104 unsigned long started_waiting_at = millis(); // Start a timer for measuring timeout
105 bool timeout = false;
106 while (!radio.available() && !timeout) // Wait until we get a response or timeout is reached
107 {
108 if (millis() - started_waiting_at > 500) // Only wait for 500 milliseconds
109 timeout = true;
110 }
111
112 // Describe the results
113 if (timeout) {
114 Serial.println(F("Failed, response timed out."));
115 } else {
116 // Grab the response and print it
117
118 uint8_t len = radio.getDynamicPayloadSize(); // get payload's length
119
120 // If an illegal payload size was detected, all RX payloads will be flushed
121 if (!len)
122 return;
123
124 radio.read(receive_payload, len);
125
126 // Use payload as a C-string (for easy printing)
127 receive_payload[len] = 0; // put a NULL terminating zero at the end
128
129 // Spew it
130 Serial.print(F("Got response size="));
131 Serial.print(len);
132 Serial.print(F(" value="));
133 Serial.println(receive_payload);
134 }
135
136 send_payload_size += payload_size_increment; // Update size for next time.
137 if (send_payload_size > max_payload_size) // if payload length is larger than the radio can handle
138 send_payload_size = min_payload_size; // reset the payload length
139
140 delay(1000); // Try again 1s later
141 }
142
143
144 /****************** Pong Back Role ***************************/
145 // Receive each packet, send it back, and dump it out
146
147 if (role == role_pong_back) {
148 while (radio.available()) // if there is data ready
149 {
150
151 uint8_t len = radio.getDynamicPayloadSize(); // Fetch the the payload size
152
153 // If an illegal payload size was detected, all RX payloads will be flushed
154 if (!len)
155 continue;
156
157 radio.read(receive_payload, len);
158
159 // Use payload as a C-string (for easy printing)
160 receive_payload[len] = 0; // put a NULL terminating zero at the end
161
162 // Spew it
163 Serial.print(F("Got response size="));
164 Serial.print(len);
165 Serial.print(F(" value="));
166 Serial.println(receive_payload);
167
168 radio.stopListening(); // First, stop listening so we can talk
169
170 // Send a reply that the packet was received
171 //
172 // You will have better luck delivering your message if
173 // you wait for the other node to start listening first
174 delay(20);
175 radio.write(receive_payload, len);
176 Serial.println(F("Sent response."));
177
178 radio.startListening(); // Now, resume listening so we catch the next packets.
179 }
180 }
181} // loop
182// vim:cin:ai:sts=2 sw=2 ft=cpp
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
Definition RF24.h:116
#define delay(millisec)
#define INPUT
#define pinMode(pin, direction)
#define HIGH
#define digitalWrite(pin, value)
#define millis()
void printf_begin(void)
Definition printf.h:33