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.

102 lines
3.1KB

  1. // rf69_server.pde
  2. // -*- mode: C++ -*-
  3. // Example sketch showing how to create a simple messageing server
  4. // with the RH_RF69 class. RH_RF69 class does not provide for addressing or
  5. // reliability, so you should only use RH_RF69 if you do not need the higher
  6. // level messaging abilities.
  7. // It is designed to work with the other example rf69_client
  8. // Demonstrates the use of AES encryption, setting the frequency and modem
  9. // configuration.
  10. // Tested on Moteino with RFM69 http://lowpowerlab.com/moteino/
  11. // Tested on miniWireless with RFM69 www.anarduino.com/miniwireless
  12. // Tested on Teensy 3.1 with RF69 on PJRC breakout board
  13. #include <SPI.h>
  14. #include <RH_RF69.h>
  15. //For Teensy 3.x and T4.x the following format is required to operate correctly
  16. //This is a limitation of the RadioHead radio drivers
  17. #define RFM69_RST 3
  18. #define RFM69_CS 10
  19. #define RFM69_INT digitalPinToInterrupt(2) // only pins 0, 1, 2 allowed
  20. // Singleton instance of the radio driver
  21. RH_RF69 rf69(RFM69_CS,RFM69_INT);
  22. //----- END TEENSY CONFIG
  23. //RH_RF69 rf69(15, 16); // For RF69 on PJRC breakout board with Teensy 3.1
  24. //RH_RF69 rf69(4, 2); // For MoteinoMEGA https://lowpowerlab.com/shop/moteinomega
  25. void setup()
  26. {
  27. Serial.begin(9600);
  28. //For Teensy 3.x and T4.x the following format is required to operate correctly
  29. //pinMode(LED, OUTPUT);
  30. pinMode(RFM69_RST, OUTPUT);
  31. digitalWrite(RFM69_RST, LOW);
  32. Serial.println("Teensy RFM69 Server Example!");
  33. Serial.println();
  34. // manual reset
  35. digitalWrite(RFM69_RST, HIGH);
  36. delay(10);
  37. digitalWrite(RFM69_RST, LOW);
  38. delay(10);
  39. //----- END TEENSY CONFIG
  40. if (!rf69.init())
  41. Serial.println("init failed");
  42. // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
  43. // No encryption
  44. if (!rf69.setFrequency(433.0))
  45. Serial.println("setFrequency failed");
  46. // If you are using a high power RF69, you *must* set a Tx power in the
  47. // range 14 to 20 like this:
  48. rf69.setTxPower(14);
  49. // The encryption key has to be the same as the one in the client
  50. uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  51. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  52. rf69.setEncryptionKey(key);
  53. #if 0
  54. // For compat with RFM69 Struct_send
  55. rf69.setModemConfig(RH_RF69::GFSK_Rb250Fd250);
  56. rf69.setPreambleLength(3);
  57. uint8_t syncwords[] = { 0x2d, 0x64 };
  58. rf69.setSyncWords(syncwords, sizeof(syncwords));
  59. rf69.setEncryptionKey((uint8_t*)"thisIsEncryptKey");
  60. #endif
  61. }
  62. void loop()
  63. {
  64. if (rf69.available())
  65. {
  66. // Should be a message for us now
  67. uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
  68. uint8_t len = sizeof(buf);
  69. if (rf69.recv(buf, &len))
  70. {
  71. // RH_RF69::printBuffer("request: ", buf, len);
  72. Serial.print("got request: ");
  73. Serial.println((char*)buf);
  74. // Serial.print("RSSI: ");
  75. // Serial.println(rf69.lastRssi(), DEC);
  76. // Send a reply
  77. uint8_t data[] = "And hello back to you";
  78. rf69.send(data, sizeof(data));
  79. rf69.waitPacketSent();
  80. Serial.println("Sent a reply");
  81. }
  82. else
  83. {
  84. Serial.println("recv failed");
  85. }
  86. }
  87. }