PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nrf24_server.pde 1.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // nrf24_server.pde
  2. // -*- mode: C++ -*-
  3. // Example sketch showing how to create a simple messageing server
  4. // with the RH_NRF24 class. RH_NRF24 class does not provide for addressing or
  5. // reliability, so you should only use RH_NRF24 if you do not need the higher
  6. // level messaging abilities.
  7. // It is designed to work with the other example nrf24_client
  8. // Tested on Uno with Sparkfun NRF25L01 module
  9. // Tested on Anarduino Mini (http://www.anarduino.com/mini/) with RFM73 module
  10. // Tested on Arduino Mega with Sparkfun WRL-00691 NRF25L01 module
  11. #include <SPI.h>
  12. #include <RH_NRF24.h>
  13. // Singleton instance of the radio driver
  14. RH_NRF24 nrf24;
  15. // RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
  16. // RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
  17. // RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini
  18. void setup()
  19. {
  20. Serial.begin(9600);
  21. while (!Serial)
  22. ; // wait for serial port to connect. Needed for Leonardo only
  23. if (!nrf24.init())
  24. Serial.println("init failed");
  25. // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  26. if (!nrf24.setChannel(1))
  27. Serial.println("setChannel failed");
  28. if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
  29. Serial.println("setRF failed");
  30. }
  31. void loop()
  32. {
  33. if (nrf24.available())
  34. {
  35. // Should be a message for us now
  36. uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  37. uint8_t len = sizeof(buf);
  38. if (nrf24.recv(buf, &len))
  39. {
  40. // NRF24::printBuffer("request: ", buf, len);
  41. Serial.print("got request: ");
  42. Serial.println((char*)buf);
  43. // Send a reply
  44. uint8_t data[] = "And hello back to you";
  45. nrf24.send(data, sizeof(data));
  46. nrf24.waitPacketSent();
  47. Serial.println("Sent a reply");
  48. }
  49. else
  50. {
  51. Serial.println("recv failed");
  52. }
  53. }
  54. }