| 1 | /* |
| 2 | * See documentation at https://nRF24.github.io/RF24 |
| 3 | * See License information at root directory of this library |
| 4 | * Author: Brendan Doherty (2bndy5) |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * A simple example of sending data from 1 nRF24L01 transceiver to another. |
| 9 | * |
| 10 | * This example was written to be used on 2 devices acting as "nodes". |
| 11 | * Use the Serial Monitor to change each node's behavior. |
| 12 | */ |
| 13 | #include <SPI.h> |
| 14 | #include "printf.h" |
| 15 | #include "RF24.h" |
| 16 | |
| 17 | #define CE_PIN 7 |
| 18 | #define CSN_PIN 8 |
| 19 | // instantiate an object for the nRF24L01 transceiver |
| 20 | RF24 radio(CE_PIN, CSN_PIN); |
| 21 | |
| 22 | // Let these addresses be used for the pair |
| 23 | uint8_t address[][6] = { "1Node", "2Node" }; |
| 24 | // It is very helpful to think of an address as a path instead of as |
| 25 | // an identifying device destination |
| 26 | |
| 27 | // to use different addresses on a pair of radios, we need a variable to |
| 28 | // uniquely identify which address this radio will use to transmit |
| 29 | bool radioNumber = 1; // 0 uses address[0] to transmit, 1 uses address[1] to transmit |
| 30 | |
| 31 | // Used to control whether this node is sending or receiving |
| 32 | bool role = false; // true = TX role, false = RX role |
| 33 | |
| 34 | // For this example, we'll be using a payload containing |
| 35 | // a single float number that will be incremented |
| 36 | // on every successful transmission |
| 37 | float payload = 0.0; |
| 38 | |
| 39 | void setup() { |
| 40 | |
| 41 | Serial.begin(115200); |
| 42 | while (!Serial) { |
| 43 | // some boards need to wait to ensure access to serial over USB |
| 44 | } |
| 45 | |
| 46 | // initialize the transceiver on the SPI bus |
| 47 | if (!radio.begin()) { |
| 48 | Serial.println(F("radio hardware is not responding!!")); |
| 49 | while (1) {} // hold in infinite loop |
| 50 | } |
| 51 | |
| 52 | // print example's introductory prompt |
| 53 | Serial.println(F("RF24/examples/GettingStarted")); |
| 54 | |
| 55 | // To set the radioNumber via the Serial monitor on startup |
| 56 | Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'")); |
| 57 | while (!Serial.available()) { |
| 58 | // wait for user input |
| 59 | } |
| 60 | char input = Serial.parseInt(); |
| 61 | radioNumber = input == 1; |
| 62 | Serial.print(F("radioNumber = ")); |
| 63 | Serial.println((int)radioNumber); |
| 64 | |
| 65 | // role variable is hardcoded to RX behavior, inform the user of this |
| 66 | Serial.println(F("*** PRESS 'T' to begin transmitting to the other node")); |
| 67 | |
| 68 | // Set the PA Level low to try preventing power supply related problems |
| 69 | // because these examples are likely run with nodes in close proximity to |
| 70 | // each other. |
| 71 | radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default. |
| 72 | |
| 73 | // save on transmission time by setting the radio to only transmit the |
| 74 | // number of bytes we need to transmit a float |
| 75 | radio.setPayloadSize(sizeof(payload)); // float datatype occupies 4 bytes |
| 76 | |
| 77 | // set the TX address of the RX node for use on the TX pipe (pipe 0) |
| 78 | radio.stopListening(address[radioNumber]); // put radio in TX mode |
| 79 | |
| 80 | // set the RX address of the TX node into a RX pipe |
| 81 | radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1 |
| 82 | |
| 83 | // additional setup specific to the node's RX role |
| 84 | if (!role) { |
| 85 | radio.startListening(); // put radio in RX mode |
| 86 | } |
| 87 | |
| 88 | // For debugging info |
| 89 | // printf_begin(); // needed only once for printing details |
| 90 | // radio.printDetails(); // (smaller) function that prints raw register values |
| 91 | // radio.printPrettyDetails(); // (larger) function that prints human readable data |
| 92 | |
| 93 | } // setup |
| 94 | |
| 95 | void loop() { |
| 96 | |
| 97 | if (role) { |
| 98 | // This device is a TX node |
| 99 | |
| 100 | unsigned long start_timer = micros(); // start the timer |
| 101 | bool report = radio.write(&payload, sizeof(float)); // transmit & save the report |
| 102 | unsigned long end_timer = micros(); // end the timer |
| 103 | |
| 104 | if (report) { |
| 105 | Serial.print(F("Transmission successful! ")); // payload was delivered |
| 106 | Serial.print(F("Time to transmit = ")); |
| 107 | Serial.print(end_timer - start_timer); // print the timer result |
| 108 | Serial.print(F(" us. Sent: ")); |
| 109 | Serial.println(payload); // print payload sent |
| 110 | payload += 0.01; // increment float payload |
| 111 | } else { |
| 112 | Serial.println(F("Transmission failed or timed out")); // payload was not delivered |
| 113 | } |
| 114 | |
| 115 | // to make this example readable in the serial monitor |
| 116 | delay(1000); // slow transmissions down by 1 second |
| 117 | |
| 118 | } else { |
| 119 | // This device is a RX node |
| 120 | |
| 121 | uint8_t pipe; |
| 122 | if (radio.available(&pipe)) { // is there a payload? get the pipe number that received it |
| 123 | uint8_t bytes = radio.getPayloadSize(); // get the size of the payload |
| 124 | radio.read(&payload, bytes); // fetch payload from FIFO |
| 125 | Serial.print(F("Received ")); |
| 126 | Serial.print(bytes); // print the size of the payload |
| 127 | Serial.print(F(" bytes on pipe ")); |
| 128 | Serial.print(pipe); // print the pipe number |
| 129 | Serial.print(F(": ")); |
| 130 | Serial.println(payload); // print the payload's value |
| 131 | } |
| 132 | } // role |
| 133 | |
| 134 | if (Serial.available()) { |
| 135 | // change the role via the serial monitor |
| 136 | |
| 137 | char c = toupper(Serial.read()); |
| 138 | if (c == 'T' && !role) { |
| 139 | // Become the TX node |
| 140 | |
| 141 | role = true; |
| 142 | Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK")); |
| 143 | radio.stopListening(); |
| 144 | |
| 145 | } else if (c == 'R' && role) { |
| 146 | // Become the RX node |
| 147 | |
| 148 | role = false; |
| 149 | Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK")); |
| 150 | radio.startListening(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } // loop |