Advertisement
rozman50

NRF24L01 - RECEIVER

Feb 26th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include <SPI.h>
  3. #include <RF24.h>
  4.  
  5. // This is just the way the RF24 library works:
  6. // Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
  7. RF24 radio(7, 8);
  8.  
  9. byte addresses[][6] = { "1Node", "2Node" };
  10.  
  11. // -----------------------------------------------------------------------------
  12. // SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
  13. // -----------------------------------------------------------------------------
  14. void setup() {
  15. Serial.begin(9600);
  16. Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
  17.  
  18. // Initiate the radio object
  19. radio.begin();
  20.  
  21. // Set the transmit power to lowest available to prevent power supply related issues
  22. radio.setPALevel(RF24_PA_LOW);
  23. // radio.setPALevel(RF24_PA_MIN);
  24.  
  25. // Set the speed of the transmission to the quickest available
  26. radio.setDataRate(RF24_250KBPS);
  27. // radio.setDataRate(RF24_2MBPS);
  28.  
  29. // Use a channel unlikely to be used by Wifi, Microwave ovens etc
  30. radio.setChannel(124);
  31.  
  32. // Open a writing and reading pipe on each radio, with opposite addresses
  33. radio.openWritingPipe(addresses[0]);
  34. radio.openReadingPipe(1, addresses[1]);
  35.  
  36. // Start the radio listening for data
  37. radio.startListening();
  38. }
  39.  
  40. // -----------------------------------------------------------------------------
  41. // We are LISTENING on this device only (although we do transmit a response)
  42. // -----------------------------------------------------------------------------
  43. void loop() {
  44.  
  45. // This is what we receive from the other device (the transmitter)
  46. unsigned char data;
  47.  
  48. // Is there any data for us to get?
  49. if (radio.available()) {
  50.  
  51. /*
  52. // Go and read the data and put it into that variable
  53. while (radio.available()) {
  54. radio.read(&data, sizeof(char));
  55. }
  56. */
  57.  
  58. char preberi[32];
  59.  
  60. while(radio.available()) {
  61. radio.read(&preberi, sizeof(preberi));
  62. }
  63. // No more data to get so send it back but add 1 first just for kicks
  64. // First, stop listening so we can talk
  65. radio.stopListening();
  66.  
  67. /*
  68. data++;
  69. radio.write(&data, sizeof(char));
  70. */
  71.  
  72. preberi[5] = '0';
  73.  
  74. radio.write(&preberi, sizeof(preberi));
  75.  
  76. // Now, resume listening so we catch the next packets.
  77. radio.startListening();
  78.  
  79. // Tell the user what we sent back (the random numer + 1)
  80. Serial.print("Sent response ");
  81. // Serial.println(data);
  82. Serial.println(preberi);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement