PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

nrf51_server.pde 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // nrf51_server.pde
  2. // -*- mode: C++ -*-
  3. // Example sketch showing how to create a simple messageing server
  4. // with the RH_NRF51 class. RH_NRF51 class does not provide for addressing or
  5. // reliability, so you should only use RH_NRF51 if you do not need the higher
  6. // level messaging abilities.
  7. // It is designed to work with the other example nrf51_client
  8. // Tested on RedBearLabs nRF51822 and BLE Nano kit, built with Arduino 1.6.4.
  9. // See http://redbearlab.com/getting-started-nrf51822/
  10. // for how to set up your Arduino build environment
  11. #include <RH_NRF51.h>
  12. // Singleton instance of the radio driver
  13. RH_NRF51 nrf51;
  14. void setup()
  15. {
  16. delay(1000); // Wait for serial port etc to be ready
  17. Serial.begin(9600);
  18. while (!Serial)
  19. ; // wait for serial port to connect. Needed for Leonardo only
  20. if (!nrf51.init())
  21. Serial.println("init failed");
  22. // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  23. if (!nrf51.setChannel(1))
  24. Serial.println("setChannel failed");
  25. if (!nrf51.setRF(RH_NRF51::DataRate2Mbps, RH_NRF51::TransmitPower0dBm))
  26. Serial.println("setRF failed");
  27. }
  28. void loop()
  29. {
  30. if (nrf51.available())
  31. {
  32. // Should be a message for us now
  33. uint8_t buf[RH_NRF51_MAX_MESSAGE_LEN];
  34. uint8_t len = sizeof(buf);
  35. if (nrf51.recv(buf, &len))
  36. {
  37. // NRF51::printBuffer("request: ", buf, len);
  38. Serial.print("got request: ");
  39. Serial.println((char*)buf);
  40. // Send a reply
  41. uint8_t data[] = "And hello back to you";
  42. nrf51.send(data, sizeof(data));
  43. nrf51.waitPacketSent();
  44. Serial.println("Sent a reply");
  45. }
  46. else
  47. {
  48. Serial.println("recv failed");
  49. }
  50. }
  51. }