Advertisement
noam76

server

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