Advertisement
miszczo

Untitled

Jan 21st, 2022
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.78 KB | None | 0 0
  1. // include the library
  2. #include <RadioLib.h>
  3.  
  4. typedef struct
  5. {
  6.   float freq;
  7.   float bw;
  8.   uint8_t sf;
  9.   uint8_t cr;
  10.   uint8_t syncWord;
  11.   int8_t power;
  12.   uint16_t preambleLength;
  13.   uint8_t gain;
  14. } radio_conf_t;
  15.  
  16. radio_conf_t radio_conf = {468.0, // frequency
  17.                            125.0, // bandwitch
  18.                            9,     // spreading factor
  19.                            7,     // coding rate denominator
  20.                            RADIOLIB_SX127X_SYNC_WORD,
  21.                            17,  // current limit  0 - disable
  22.                            8,  // preamble length
  23.                            0}; //gain, 0 - AGC
  24.  
  25.  
  26. // SX1278 connections:
  27. // SDI pin: PA7
  28. // SDO pin: PA6
  29. // SCLK pin: PA5
  30. // NSS pin: PA4
  31. // DIO0 pin: PA3
  32. // RESET pin: PA2
  33. // DIO1 pin: PA1
  34. SX1278 radio = new Module(PA4, PA3, PA2, PA1);
  35.  
  36. // flag to indicate that a packet was received
  37. volatile bool receivedFlag = false;
  38.  
  39. // disable interrupt when it's not needed
  40. volatile bool enableInterrupt = true;
  41.  
  42.  
  43. // this function is called when a complete packet
  44. // is received by the module
  45. // IMPORTANT: this function MUST be 'void' type
  46. //            and MUST NOT have any arguments!
  47. void setFlag(void)
  48. {
  49.   // check if the interrupt is enabledz
  50.   if (!enableInterrupt)
  51.   {
  52.     return;
  53.   }
  54.  
  55.   // we got a packet, set the flag
  56.   receivedFlag = true;
  57. }
  58.  
  59.  
  60.  
  61. void setup() {
  62.   SerialUSB.begin();
  63.  
  64.   delay(10000);
  65.  
  66.   // initialize SX1278 with default settings
  67.   SerialUSB.print(F("[SX1278] Initializing ... "));
  68.   // int state = radio.begin(radio_conf.freq, radio_conf.bw, radio_conf.sf,
  69.   //                         radio_conf.cr, radio_conf.syncWord, radio_conf.power,
  70.   //                         radio_conf.preambleLength, radio_conf.gain);
  71.    int state =radio.begin();
  72.  
  73.  
  74.   if (state == RADIOLIB_ERR_NONE)
  75.   {
  76.     SerialUSB.println(F("success!"));
  77.   }
  78.   else
  79.   {
  80.     SerialUSB.print(F("failed, code "));
  81.     SerialUSB.println(state);
  82.     while (true)
  83.       ;
  84.   }
  85.  
  86.   // set the function that will be called
  87.   // when new packet is received
  88.   radio.setDio0Action(setFlag);
  89.  
  90.   // start listening for LoRa packets
  91.   SerialUSB.print(F("[SX1272] Starting to listen ... "));
  92.   state = radio.startReceive();
  93.   if (state == RADIOLIB_ERR_NONE)
  94.   {
  95.     SerialUSB.println(F("success!"));
  96.   }
  97.   else
  98.   {
  99.     SerialUSB.print(F("failed, code "));
  100.     SerialUSB.println(state);
  101.     while (true)
  102.       ;
  103.   }
  104.  
  105.   SerialUSB.print("Frequency = ");
  106.   SerialUSB.println(radio_conf.freq);
  107.   SerialUSB.print("Bandwitch = ");
  108.   SerialUSB.println(radio_conf.bw);
  109.   SerialUSB.print("Spreading factor = ");
  110.   SerialUSB.println(radio_conf.sf);
  111.   SerialUSB.print("Coding rate = ");
  112.   SerialUSB.println(radio_conf.cr);
  113.   SerialUSB.print("Synch word = ");
  114.   SerialUSB.println(radio_conf.syncWord);
  115.   SerialUSB.print("Current limit = ");
  116.   SerialUSB.println(radio_conf.power);
  117.   SerialUSB.print("Preamble length = ");
  118.   SerialUSB.println(radio_conf.preambleLength);
  119.   SerialUSB.print("Gain = ");
  120.   SerialUSB.println(radio_conf.gain);
  121. }
  122.  
  123. void loop() {
  124.   // check if the flag is set
  125.   if (receivedFlag)
  126.   {
  127.     // disable the interrupt service routine while
  128.     // processing the data
  129.     enableInterrupt = false;
  130.  
  131.     // reset flag
  132.     receivedFlag = false;
  133.  
  134.     // you can read received data as an Arduino String
  135.     String str;
  136.     int state = radio.readData(str);
  137.  
  138.     // you can also read received data as byte array
  139.     /*
  140.       byte byteArr[8];
  141.       int state = radio.readData(byteArr, 8);
  142.     */
  143.  
  144.     if (state == RADIOLIB_ERR_NONE)
  145.     {
  146.       // packet was successfully received
  147.       SerialUSB.println(F("[SX1278] Received packet!"));
  148.  
  149.       // print data of the packet
  150.       SerialUSB.print(F("[SX1278] Data:\t\t"));
  151.       SerialUSB.println(str);
  152.  
  153.       // print RSSI (Received Signal Strength Indicator)
  154.       SerialUSB.print(F("[SX1278] RSSI:\t\t"));
  155.       SerialUSB.print(radio.getRSSI());
  156.       SerialUSB.println(F(" dBm"));
  157.  
  158.       // print SNR (Signal-to-Noise Ratio)
  159.       SerialUSB.print(F("[SX1278] SNR:\t\t"));
  160.       SerialUSB.print(radio.getSNR());
  161.       SerialUSB.println(F(" dB"));
  162.  
  163.       // print frequency error
  164.       SerialUSB.print(F("[SX1278] Frequency error:\t"));
  165.       SerialUSB.print(radio.getFrequencyError());
  166.       SerialUSB.println(F(" Hz"));
  167.     }
  168.     else if (state == RADIOLIB_ERR_CRC_MISMATCH)
  169.     {
  170.       // packet was received, but is malformed
  171.       SerialUSB.println(F("[SX1278] CRC error!"));
  172.     }
  173.     else
  174.     {
  175.       // some other error occurred
  176.       SerialUSB.print(F("[SX1278] Failed, code "));
  177.       SerialUSB.println(state);
  178.     }
  179.  
  180.     // put module back to listen mode
  181.     radio.startReceive();
  182.  
  183.     // we're ready to receive more packets,
  184.     // enable interrupt service routine
  185.     enableInterrupt = true;
  186.   }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement