noam76

client

Sep 27th, 2021
1,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. https://randomnerdtutorials.com/nrf24l01-2-4ghz-rf-transceiver-module-with-arduino/
  2.  
  3. // nrf24_client
  4.  
  5. #include <SPI.h>
  6. #include <RH_NRF24.h>
  7.  
  8. // Singleton instance of the radio driver
  9. RH_NRF24 nrf24;
  10. // RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
  11. // RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
  12. // RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini
  13.  
  14. void setup()
  15. {
  16.   Serial.begin(9600);
  17.   while (!Serial)
  18.     ; // wait for serial port to connect. Needed for Leonardo only
  19.   if (!nrf24.init())
  20.     Serial.println("init failed");
  21.   // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  22.   if (!nrf24.setChannel(1))
  23.     Serial.println("setChannel failed");
  24.   if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
  25.     Serial.println("setRF failed");    
  26. }
  27.  
  28.  
  29. void loop()
  30. {
  31.   Serial.println("Sending to nrf24_server");
  32.   // Send a message to nrf24_server
  33.   uint8_t data[] = "Hello World!";
  34.   nrf24.send(data, sizeof(data));
  35.  
  36.   nrf24.waitPacketSent();
  37.   // Now wait for a reply
  38.   uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  39.   uint8_t len = sizeof(buf);
  40.  
  41.   if (nrf24.waitAvailableTimeout(500))
  42.   {
  43.     // Should be a reply message for us now  
  44.     if (nrf24.recv(buf, &len))
  45.     {
  46.       Serial.print("got reply: ");
  47.       Serial.println((char*)buf);
  48.     }
  49.     else
  50.     {
  51.       Serial.println("recv failed");
  52.     }
  53.   }
  54.   else
  55.   {
  56.     Serial.println("No reply, is nrf24_server running?");
  57.   }
  58.   delay(400);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment