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

SX128x_Receive_Interrupt.ino (4205B)

      1 /*
      2    RadioLib SX128x Receive with Interrupts Example
      3 
      4    This example listens for LoRa transmissions and tries to
      5    receive them. Once a packet is received, an interrupt is
      6    triggered. To successfully receive data, the following
      7    settings have to be the same on both transmitter
      8    and receiver:
      9     - carrier frequency
     10     - bandwidth
     11     - spreading factor
     12     - coding rate
     13     - sync word
     14 
     15    Other modules from SX128x family can also be used.
     16 
     17    For default module settings, see the wiki page
     18    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx128x---lora-modem
     19 
     20    For full API reference, see the GitHub Pages
     21    https://jgromes.github.io/RadioLib/
     22 */
     23 
     24 // include the library
     25 #include <RadioLib.h>
     26 
     27 // SX1280 has the following connections:
     28 // NSS pin:   10
     29 // DIO1 pin:  2
     30 // NRST pin:  3
     31 // BUSY pin:  9
     32 SX1280 radio = new Module(10, 2, 3, 9);
     33 
     34 // or using RadioShield
     35 // https://github.com/jgromes/RadioShield
     36 //SX1280 radio = RadioShield.ModuleA;
     37 
     38 void setup() {
     39   Serial.begin(9600);
     40 
     41   // initialize SX1280 with default settings
     42   Serial.print(F("[SX1280] Initializing ... "));
     43   int state = radio.begin();
     44   if (state == RADIOLIB_ERR_NONE) {
     45     Serial.println(F("success!"));
     46   } else {
     47     Serial.print(F("failed, code "));
     48     Serial.println(state);
     49     while (true);
     50   }
     51 
     52   // set the function that will be called
     53   // when new packet is received
     54   radio.setDio1Action(setFlag);
     55 
     56   // start listening for LoRa packets
     57   Serial.print(F("[SX1280] Starting to listen ... "));
     58   state = radio.startReceive();
     59   if (state == RADIOLIB_ERR_NONE) {
     60     Serial.println(F("success!"));
     61   } else {
     62     Serial.print(F("failed, code "));
     63     Serial.println(state);
     64     while (true);
     65   }
     66 
     67   // if needed, 'listen' mode can be disabled by calling
     68   // any of the following methods:
     69   //
     70   // radio.standby()
     71   // radio.sleep()
     72   // radio.transmit();
     73   // radio.receive();
     74   // radio.readData();
     75   // radio.scanChannel();
     76 }
     77 
     78 // flag to indicate that a packet was received
     79 volatile bool receivedFlag = false;
     80 
     81 // disable interrupt when it's not needed
     82 volatile bool enableInterrupt = true;
     83 
     84 // this function is called when a complete packet
     85 // is received by the module
     86 // IMPORTANT: this function MUST be 'void' type
     87 //            and MUST NOT have any arguments!
     88 #if defined(ESP8266) || defined(ESP32)
     89   ICACHE_RAM_ATTR
     90 #endif
     91 void setFlag(void) {
     92   // check if the interrupt is enabled
     93   if(!enableInterrupt) {
     94     return;
     95   }
     96 
     97   // we got a packet, set the flag
     98   receivedFlag = true;
     99 }
    100 
    101 void loop() {
    102   // check if the flag is set
    103   if(receivedFlag) {
    104     // disable the interrupt service routine while
    105     // processing the data
    106     enableInterrupt = false;
    107 
    108     // reset flag
    109     receivedFlag = false;
    110 
    111     // you can read received data as an Arduino String
    112     String str;
    113     int state = radio.readData(str);
    114 
    115     // you can also read received data as byte array
    116     /*
    117       byte byteArr[8];
    118       int state = radio.readData(byteArr, 8);
    119     */
    120 
    121     if (state == RADIOLIB_ERR_NONE) {
    122       // packet was successfully received
    123       Serial.println(F("[SX1280] Received packet!"));
    124 
    125       // print data of the packet
    126       Serial.print(F("[SX1280] Data:\t\t"));
    127       Serial.println(str);
    128 
    129       // print RSSI (Received Signal Strength Indicator)
    130       Serial.print(F("[SX1280] RSSI:\t\t"));
    131       Serial.print(radio.getRSSI());
    132       Serial.println(F(" dBm"));
    133 
    134       // print SNR (Signal-to-Noise Ratio)
    135       Serial.print(F("[SX1280] SNR:\t\t"));
    136       Serial.print(radio.getSNR());
    137       Serial.println(F(" dB"));
    138 
    139       // print the Frequency Error
    140       // of the last received packet
    141       Serial.print(F("[SX1280] Frequency Error:\t"));
    142       Serial.print(radio.getFrequencyError());
    143       Serial.println(F(" Hz"));
    144 
    145     } else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
    146       // packet was received, but is malformed
    147       Serial.println(F("CRC error!"));
    148 
    149     } else {
    150       // some other error occurred
    151       Serial.print(F("failed, code "));
    152       Serial.println(state);
    153 
    154     }
    155 
    156     // put module back to listen mode
    157     radio.startReceive();
    158 
    159     // we're ready to receive more packets,
    160     // enable interrupt service routine
    161     enableInterrupt = true;
    162   }
    163 
    164 }