| 1 | /** |
| 2 | * See documentation at https://nRF24.github.io/RF24 |
| 3 | * See License information at root directory of this library |
| 4 | * written in 2014 by tong67 (https://github.com/tong67) |
| 5 | * Updated 2020 by 2bndy5 (http://github.com/2bndy5) for the |
| 6 | * SpenceKonde ATTinyCore (https://github.com/SpenceKonde/ATTinyCore) |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * The RF24 library uses the [ATTinyCore by |
| 11 | * SpenceKonde](https://github.com/SpenceKonde/ATTinyCore) |
| 12 | * |
| 13 | * This sketch is a duplicate of the ManualAcknowledgements.ino example |
| 14 | * (without all the Serial input/output code), and it demonstrates |
| 15 | * a ATTiny25/45/85 or ATTiny24/44/84 driving the nRF24L01 transceiver using |
| 16 | * the RF24 class to communicate with another node. |
| 17 | * |
| 18 | * A simple example of sending data from 1 nRF24L01 transceiver to another |
| 19 | * with manually transmitted (non-automatic) Acknowledgement (ACK) payloads. |
| 20 | * This example still uses ACK packets, but they have no payloads. Instead the |
| 21 | * acknowledging response is sent with `write()`. This tactic allows for more |
| 22 | * updated acknowledgement payload data, where actual ACK payloads' data are |
| 23 | * outdated by 1 transmission because they have to loaded before receiving a |
| 24 | * transmission. |
| 25 | * |
| 26 | * This example was written to be used on 2 devices acting as "nodes". |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * ********** Hardware configuration (& schematics) ******************* |
| 31 | * |
| 32 | * When direct use of 3V does not work (UNO boards tend to have poor 3V supply), |
| 33 | * use 5V with LED (1.8V ~ 2.2V drop) instead. |
| 34 | * For low power consumption solutions floating pins (SCK and MOSI) should be |
| 35 | * pulled HIGH or LOW with 10K resistors. |
| 36 | * |
| 37 | * ATTiny25/45/85 Pin map with CE_PIN 3 and CSN_PIN 4 |
| 38 | * ^^ |
| 39 | * +-\/-+ // |
| 40 | * PB5 1|o |8 Vcc --- nRF24L01 VCC --- |<|--- 5V |
| 41 | * nRF24L01 CE --- PB3 2| |7 PB2 --- nRF24L01 SCK LED |
| 42 | * nRF24L01 CSN --- PB4 3| |6 PB1 --- nRF24L01 MOSI |
| 43 | * nRF24L01 GND --- GND 4| |5 PB0 --- nRF24L01 MISO |
| 44 | * +----+ |
| 45 | * |
| 46 | * ATTiny25/45/85 Pin map with CE_PIN 3 and CSN_PIN 3 => PB3 and PB4 are |
| 47 | * free to use for other purposes. This "3 pin solution" is from |
| 48 | * Ralph Doncaster (AKA NerdRalph) which is outlined on his blog at |
| 49 | * http://nerdralph.blogspot.ca/2014/01/nrf24l01-control-with-3-attiny85-pins.html |
| 50 | * Original RC combination was 1K/100nF. 22K/10nF combination worked better. |
| 51 | * |
| 52 | * For best settle time delay value to use for RF24::csDelay in RF24::csn(), use |
| 53 | * the examples/rf24_ATTiny/timingSearch3pin/timingSearch3pin.ino sketch. |
| 54 | * |
| 55 | * This configuration is enabled in the RF24 library when CE_PIN and |
| 56 | * CSN_PIN parameters to the constructor are equal. Notice (in the schematic |
| 57 | * below) that these pins aren't directly to the ATTiny85. Because the CE pin |
| 58 | * is always HIGH, the power consumption is higher than it would be for the |
| 59 | * typical 5 pins solution. |
| 60 | * ^^ |
| 61 | * +-\/-+ nRF24L01 CE --------| // |
| 62 | * PB5 1|o |8 Vcc --- nRF24L01 VCC -------x----------x--|<|-- 5V |
| 63 | * PB3 2| |7 PB2 --- nRF24L01 SCK ---|<|---x-[22k]--| LED |
| 64 | * PB4 3| |6 PB1 --- nRF24L01 MOSI 1n4148 | |
| 65 | * nRF24L01 GND -x- GND 4| |5 PB0 --- nRF24L01 MISO | |
| 66 | * | +----+ | |
| 67 | * |-----------------------------------------||----x-- nRF24L01 CSN |
| 68 | * 10nF |
| 69 | * |
| 70 | * ATTiny24/44/84 Pin map with CE_PIN 8 and CSN_PIN 7 & assuming 1.9V to 3V on VCC |
| 71 | * Schematic provided and successfully tested by |
| 72 | * Carmine Pastore (https://github.com/Carminepz) |
| 73 | * |
| 74 | * +-\/-+ |
| 75 | * nRF24L01 VCC ---- VCC 1|o |14 GND --- nRF24L01 GND |
| 76 | * PB0 2| |13 AREF |
| 77 | * PB1 3| |12 PA1 |
| 78 | * PB3 4| |11 PA2 --- nRF24L01 CE |
| 79 | * PB2 5| |10 PA3 --- nRF24L01 CSN |
| 80 | * PA7 6| |9 PA4 --- nRF24L01 SCK |
| 81 | * nRF24L01 MOSI --- PA6 7| |8 PA5 --- nRF24L01 MISO |
| 82 | * +----+ |
| 83 | */ |
| 84 | |
| 85 | #include "SPI.h" |
| 86 | #include "RF24.h" |
| 87 | |
| 88 | // CE and CSN are configurable, specified values for ATTiny85 as connected above |
| 89 | #define CE_PIN 3 |
| 90 | #define CSN_PIN 4 |
| 91 | //#define CSN_PIN 3 // uncomment for ATTiny85 3 pins solution |
| 92 | |
| 93 | // instantiate an object for the nRF24L01 transceiver |
| 94 | RF24 radio(CE_PIN, CSN_PIN); |
| 95 | |
| 96 | // Let these addresses be used for the pair |
| 97 | uint8_t address[][6] = { "1Node", "2Node" }; |
| 98 | // It is very helpful to think of an address as a path instead of as |
| 99 | // an identifying device destination |
| 100 | |
| 101 | // to use different addresses on a pair of radios, we need a variable to |
| 102 | // uniquely identify which address this radio will use to transmit |
| 103 | bool radioNumber = 1; // 0 uses address[0] to transmit, 1 uses address[1] to transmit |
| 104 | |
| 105 | // Used to control whether this node is sending or receiving |
| 106 | bool role = false; // true = TX node, false = RX node |
| 107 | |
| 108 | // For this example, we'll be using a payload containing |
| 109 | // a string & an integer number that will be incremented |
| 110 | // on every successful transmission. |
| 111 | // Make a data structure to store the entire payload of different datatypes |
| 112 | struct PayloadStruct { |
| 113 | char message[7]; // only using 6 characters for TX & RX payloads |
| 114 | uint8_t counter; |
| 115 | }; |
| 116 | PayloadStruct payload; |
| 117 | |
| 118 | void setup() { |
| 119 | |
| 120 | // append a NULL terminating character for printing as a c-string |
| 121 | payload.message[6] = 0; |
| 122 | |
| 123 | // initialize the transceiver on the SPI bus |
| 124 | if (!radio.begin()) { |
| 125 | while (1) {} // hold in infinite loop |
| 126 | } |
| 127 | |
| 128 | // Set the PA Level low to try preventing power supply related problems |
| 129 | // because these examples are likely run with nodes in close proximity to |
| 130 | // each other. |
| 131 | radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default. |
| 132 | |
| 133 | // save on transmission time by setting the radio to only transmit the |
| 134 | // number of bytes we need to transmit a float |
| 135 | radio.setPayloadSize(sizeof(payload)); // char[7] & uint8_t datatypes occupy 8 bytes |
| 136 | |
| 137 | // set the TX address of the RX node into the TX pipe |
| 138 | radio.openWritingPipe(address[radioNumber]); // always uses pipe 0 |
| 139 | |
| 140 | // set the RX address of the TX node into a RX pipe |
| 141 | radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1 |
| 142 | |
| 143 | if (role) { |
| 144 | // setup the TX node |
| 145 | |
| 146 | memcpy(payload.message, "Hello ", 6); // set the outgoing message |
| 147 | radio.stopListening(); // put radio in TX mode |
| 148 | } else { |
| 149 | // setup the RX node |
| 150 | |
| 151 | memcpy(payload.message, "World ", 6); // set the outgoing message |
| 152 | radio.startListening(); // put radio in RX mode |
| 153 | } |
| 154 | } // setup() |
| 155 | |
| 156 | void loop() { |
| 157 | |
| 158 | if (role) { |
| 159 | // This device is a TX node |
| 160 | |
| 161 | bool report = radio.write(&payload, sizeof(payload)); // transmit & save the report |
| 162 | |
| 163 | if (report) { |
| 164 | // transmission successful; wait for response and print results |
| 165 | |
| 166 | radio.startListening(); // put in RX mode |
| 167 | unsigned long start_timeout = millis(); // timer to detect no response |
| 168 | while (!radio.available()) { // wait for response or timeout |
| 169 | if (millis() - start_timeout > 200) // only wait 200 ms |
| 170 | break; |
| 171 | } |
| 172 | radio.stopListening(); // put back in TX mode |
| 173 | |
| 174 | // print summary of transactions |
| 175 | if (radio.available()) { // is there a payload received? |
| 176 | |
| 177 | PayloadStruct received; |
| 178 | radio.read(&received, sizeof(received)); // get payload from RX FIFO |
| 179 | payload.counter = received.counter; // save incoming counter for next outgoing counter |
| 180 | } |
| 181 | } // report |
| 182 | |
| 183 | // to make this example readable in the serial monitor |
| 184 | delay(1000); // slow transmissions down by 1 second |
| 185 | |
| 186 | } else { |
| 187 | // This device is a RX node |
| 188 | |
| 189 | if (radio.available()) { // is there a payload? |
| 190 | |
| 191 | PayloadStruct received; |
| 192 | radio.read(&received, sizeof(received)); // get incoming payload |
| 193 | payload.counter = received.counter + 1; // increment incoming counter for next outgoing response |
| 194 | |
| 195 | // transmit response & save result to `report` |
| 196 | radio.stopListening(); // put in TX mode |
| 197 | |
| 198 | radio.writeFast(&payload, sizeof(payload)); // load response to TX FIFO |
| 199 | radio.txStandBy(150); // keep retrying for 150 ms |
| 200 | |
| 201 | radio.startListening(); // put back in RX mode |
| 202 | } |
| 203 | } // role |
| 204 | } // loop |