No Description

sx1276-lora.cxx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Author: Jon Trulson <jtrulson@ics.com>
  3. * Copyright (c) 2015 Intel Corporation.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <iostream>
  27. #include <signal.h>
  28. #include "sx1276.h"
  29. using namespace std;
  30. int shouldRun = true;
  31. void sig_handler(int signo)
  32. {
  33. if (signo == SIGINT)
  34. shouldRun = false;
  35. }
  36. int main(int argc, char **argv)
  37. {
  38. signal(SIGINT, sig_handler);
  39. //! [Interesting]
  40. cout << "Specify an argument to go into receive mode. Default is transmit"
  41. << endl;
  42. bool rx = false;
  43. if (argc > 1)
  44. rx = true;
  45. // Instantiate an SX1276 using default parameters
  46. upm::SX1276 *sensor = new upm::SX1276();
  47. // 915Mhz
  48. sensor->setChannel(915000000);
  49. // LORA configuration (rx and tx must be configured the same):
  50. // Tx output power = 14 dBm
  51. // LORA bandwidth = 125000 (can also be 250K and 500K)
  52. // LORA spreading factor = 7
  53. // LORA coding rate = 1 (4/5)
  54. // LORA preamble len = 8
  55. // LORA symbol timeout = 5
  56. // LORA fixed payload = false
  57. // LORA IQ inversion = false
  58. // LORA (rx) continuous Rx mode = true
  59. sensor->setTxConfig(sensor->MODEM_LORA, 14, 0, 125000,
  60. 7, 1, 8, false, true, false, 0, false);
  61. sensor->setRxConfig(sensor->MODEM_LORA, 125000, 7,
  62. 1, 0, 8, 5, false, 0, true, false, 0, false, true);
  63. int count = 0;
  64. int buflen = 64;
  65. char buffer[buflen];
  66. while (shouldRun)
  67. {
  68. if (!rx)
  69. {
  70. snprintf(buffer, buflen, "Ping %d", count++);
  71. cout << "Sending..." << std::string(buffer) << endl;
  72. sensor->sendStr(string(buffer), 3000);
  73. sensor->setSleep();
  74. sleep(1);
  75. }
  76. else
  77. {
  78. // receiving
  79. cout << "Attempting to receive..." << endl;
  80. int rv;
  81. if (rv = sensor->setRx(3000))
  82. {
  83. cout << "setRx returned " << rv << endl;
  84. }
  85. else
  86. {
  87. cout << "Received Buffer: " << sensor->getRxBufferStr() << endl;
  88. }
  89. // go back to sleep when done
  90. sensor->setSleep();
  91. usleep(5000);
  92. }
  93. }
  94. //! [Interesting]
  95. cout << "Exiting..." << endl;
  96. delete sensor;
  97. return 0;
  98. }