acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

SX126x_Receive.ino (2771B)

      1 /*
      2    RadioLib SX126x Receive Example
      3 
      4    This example listens for LoRa transmissions using SX126x Lora modules.
      5    To successfully receive data, the following settings have to be the same
      6    on both transmitter and receiver:
      7     - carrier frequency
      8     - bandwidth
      9     - spreading factor
     10     - coding rate
     11     - sync word
     12     - preamble length
     13 
     14    Other modules from SX126x family can also be used.
     15 
     16    For default module settings, see the wiki page
     17    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem
     18 
     19    For full API reference, see the GitHub Pages
     20    https://jgromes.github.io/RadioLib/
     21 */
     22 
     23 // include the library
     24 #include <RadioLib.h>
     25 
     26 // SX1262 has the following connections:
     27 // NSS pin:   10
     28 // DIO1 pin:  2
     29 // NRST pin:  3
     30 // BUSY pin:  9
     31 SX1262 radio = new Module(10, 2, 3, 9);
     32 
     33 // or using RadioShield
     34 // https://github.com/jgromes/RadioShield
     35 //SX1262 radio = RadioShield.ModuleA;
     36 
     37 // or using CubeCell
     38 //SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE);
     39 
     40 void setup() {
     41   Serial.begin(9600);
     42 
     43   // initialize SX1262 with default settings
     44   Serial.print(F("[SX1262] Initializing ... "));
     45   int state = radio.begin();
     46   if (state == RADIOLIB_ERR_NONE) {
     47     Serial.println(F("success!"));
     48   } else {
     49     Serial.print(F("failed, code "));
     50     Serial.println(state);
     51     while (true);
     52   }
     53 }
     54 
     55 void loop() {
     56   Serial.print(F("[SX1262] Waiting for incoming transmission ... "));
     57 
     58   // you can receive data as an Arduino String
     59   // NOTE: receive() is a blocking method!
     60   //       See example ReceiveInterrupt for details
     61   //       on non-blocking reception method.
     62   String str;
     63   int state = radio.receive(str);
     64 
     65   // you can also receive data as byte array
     66   /*
     67     byte byteArr[8];
     68     int state = radio.receive(byteArr, 8);
     69   */
     70 
     71   if (state == RADIOLIB_ERR_NONE) {
     72     // packet was successfully received
     73     Serial.println(F("success!"));
     74 
     75     // print the data of the packet
     76     Serial.print(F("[SX1262] Data:\t\t"));
     77     Serial.println(str);
     78 
     79     // print the RSSI (Received Signal Strength Indicator)
     80     // of the last received packet
     81     Serial.print(F("[SX1262] RSSI:\t\t"));
     82     Serial.print(radio.getRSSI());
     83     Serial.println(F(" dBm"));
     84 
     85     // print the SNR (Signal-to-Noise Ratio)
     86     // of the last received packet
     87     Serial.print(F("[SX1262] SNR:\t\t"));
     88     Serial.print(radio.getSNR());
     89     Serial.println(F(" dB"));
     90 
     91   } else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
     92     // timeout occurred while waiting for a packet
     93     Serial.println(F("timeout!"));
     94 
     95   } else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
     96     // packet was received, but is malformed
     97     Serial.println(F("CRC error!"));
     98 
     99   } else {
    100     // some other error occurred
    101     Serial.print(F("failed, code "));
    102     Serial.println(state);
    103 
    104   }
    105 }