2 Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
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.
10 * Example RF Radio Ping Star Group
12 * This sketch is a more complex example of using the RF24 library for Arduino.
13 * Deploy this on up to six nodes. Set one as the 'pong receiver' by tying the
14 * role_pin low, and the others will be 'ping transmit' units. The ping units
15 * unit will send out the value of millis() once a second. The pong unit will
16 * respond back with a copy of the value. Each ping unit can get that response
17 * back, and determine how long the whole cycle took.
19 * This example requires a bit more complexity to determine which unit is which.
20 * The pong receiver is identified by having its role_pin tied to ground.
21 * The ping senders are further differentiated by a byte in eeprom.
31 // Hardware configuration
34 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
38 // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
39 // Leave open to be the 'pong' receiver.
40 const int role_pin = 7;
46 // Radio pipe addresses for the nodes to communicate. Only ping nodes need
47 // dedicated pipes in this topology. Each ping node has a talking pipe
48 // that it will ping into, and a listening pipe that it will listen for
49 // the pong. The pong node listens on all the ping node talking pipes
50 // and sends the pong back on the sending node's specific listening pipe.
52 const uint64_t talking_pipes[5] = { 0xF0F0F0F0D2LL, 0xF0F0F0F0C3LL, 0xF0F0F0F0B4LL, 0xF0F0F0F0A5LL, 0xF0F0F0F096LL };
53 const uint64_t listening_pipes[5] = { 0x3A3A3A3AD2LL, 0x3A3A3A3AC3LL, 0x3A3A3A3AB4LL, 0x3A3A3A3AA5LL, 0x3A3A3A3A96LL };
58 // Set up role. This sketch uses the same software for all the nodes
59 // in this system. Doing so greatly simplifies testing. The hardware itself specifies
62 // This is done through the role_pin
65 // The various roles supported by this sketch
66 typedef enum { role_invalid = 0, role_ping_out, role_pong_back } role_e;
68 // The debug-friendly names of those roles
69 const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
71 // The role of the current running sketch
78 // Where in EEPROM is the address stored?
79 const uint8_t address_at_eeprom_location = 0;
81 // What is our address (SRAM cache of the address from EEPROM)
82 // Note that zero is an INVALID address. The pong back unit takes address
83 // 1, and the rest are 2-6
92 // set up the role pin
93 pinMode(role_pin, INPUT);
94 digitalWrite(role_pin,HIGH);
95 delay(20); // Just to get a solid reading on the role pin
97 // read the address pin, establish our role
98 if ( digitalRead(role_pin) )
101 role = role_pong_back;
107 if ( role == role_pong_back )
111 // Read the address from EEPROM
112 uint8_t reading = EEPROM.read(address_at_eeprom_location);
114 // If it is in a valid range for node addresses, it is our
116 if ( reading >= 2 && reading <= 6 )
117 node_address = reading;
119 // Otherwise, it is invalid, so set our address AND ROLE to 'invalid'
133 printf("\n\rRF24/examples/starping/\n\r");
134 printf("ROLE: %s\n\r",role_friendly_name[role]);
135 printf("ADDRESS: %i\n\r",node_address);
138 // Setup and configure rf radio
144 // Open pipes to other nodes for communication
147 // The pong node listens on all the ping node talking pipes
148 // and sends the pong back on the sending node's specific listening pipe.
149 if ( role == role_pong_back )
151 radio.openReadingPipe(1,talking_pipes[0]);
152 radio.openReadingPipe(2,talking_pipes[1]);
153 radio.openReadingPipe(3,talking_pipes[2]);
154 radio.openReadingPipe(4,talking_pipes[3]);
155 radio.openReadingPipe(5,talking_pipes[4]);
158 // Each ping node has a talking pipe that it will ping into, and a listening
159 // pipe that it will listen for the pong.
160 if ( role == role_ping_out )
162 // Write on our talking pipe
163 radio.openWritingPipe(talking_pipes[node_address-2]);
164 // Listen on our listening pipe
165 radio.openReadingPipe(1,listening_pipes[node_address-2]);
172 radio.startListening();
175 // Dump the configuration of the rf unit for debugging
178 radio.printDetails();
181 // Prompt the user to assign a node address if we don't have one
184 if ( role == role_invalid )
186 printf("\n\r*** NO NODE ADDRESS ASSIGNED *** Send 1 through 6 to assign an address\n\r");
193 // Ping out role. Repeatedly send the current time
196 if (role == role_ping_out)
198 // First, stop listening so we can talk.
199 radio.stopListening();
201 // Take the time, and send it. This will block until complete
202 unsigned long time = millis();
203 printf("Now sending %lu...",time);
204 radio.write( &time, sizeof(unsigned long) );
206 // Now, continue listening
207 radio.startListening();
209 // Wait here until we get a response, or timeout (250ms)
210 unsigned long started_waiting_at = millis();
211 bool timeout = false;
212 while ( ! radio.available() && ! timeout )
213 if (millis() - started_waiting_at > 250 )
216 // Describe the results
219 printf("Failed, response timed out.\n\r");
223 // Grab the response, compare, and send to debugging spew
224 unsigned long got_time;
225 radio.read( &got_time, sizeof(unsigned long) );
228 printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
231 // Try again 1s later
236 // Pong back role. Receive each packet, dump it out, and send it back
239 if ( role == role_pong_back )
241 // if there is data ready
243 if ( radio.available(&pipe_num) )
245 // Dump the payloads until we've gotten everything
246 unsigned long got_time;
250 // Fetch the payload, and see if this was the last one.
251 done = radio.read( &got_time, sizeof(unsigned long) );
254 printf("Got payload %lu from node %i...",got_time,pipe_num+1);
257 // First, stop listening so we can talk
258 radio.stopListening();
260 // Open the correct pipe for writing
261 radio.openWritingPipe(listening_pipes[pipe_num-1]);
263 // Retain the low 2 bytes to identify the pipe for the spew
264 uint16_t pipe_id = listening_pipes[pipe_num-1] & 0xffff;
266 // Send the final one back.
267 radio.write( &got_time, sizeof(unsigned long) );
268 printf("Sent response to %04x.\n\r",pipe_id);
270 // Now, resume listening so we catch the next packets.
271 radio.startListening();
276 // Listen for serial input, which is how we set the address
278 if (Serial.available())
280 // If the character on serial input is in a valid range...
281 char c = Serial.read();
282 if ( c >= '1' && c <= '6' )
285 EEPROM.write(address_at_eeprom_location,c-'0');
287 // And we are done right now (no easy way to soft reset)
288 printf("\n\rManually reset address to: %c\n\rPress RESET to continue!",c);
293 // vim:ai:ci sts=2 sw=2 ft=cpp