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_FSK_Modem.ino (5054B)

      1 /*
      2    RadioLib SX126x FSK Modem Example
      3 
      4    This example shows how to use FSK modem in SX126x chips.
      5 
      6    NOTE: The sketch below is just a guide on how to use
      7          FSK modem, so this code should not be run directly!
      8          Instead, modify the other examples to use FSK
      9          modem and use the appropriate configuration
     10          methods.
     11 
     12    For default module settings, see the wiki page
     13    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---fsk-modem
     14 
     15    For full API reference, see the GitHub Pages
     16    https://jgromes.github.io/RadioLib/
     17 */
     18 
     19 // include the library
     20 #include <RadioLib.h>
     21 
     22 // SX1262 has the following connections:
     23 // NSS pin:   10
     24 // DIO1 pin:  2
     25 // NRST pin:  3
     26 // BUSY pin:  9
     27 SX1262 radio = new Module(10, 2, 3, 9);
     28 
     29 // or using RadioShield
     30 // https://github.com/jgromes/RadioShield
     31 //SX1262 radio = RadioShield.ModuleA;
     32 
     33 // or using CubeCell
     34 //SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE);
     35 
     36 void setup() {
     37   Serial.begin(9600);
     38 
     39   // initialize SX1262 FSK modem with default settings
     40   Serial.print(F("[SX1262] Initializing ... "));
     41   int state = radio.beginFSK();
     42   if (state == RADIOLIB_ERR_NONE) {
     43     Serial.println(F("success!"));
     44   } else {
     45     Serial.print(F("failed, code "));
     46     Serial.println(state);
     47     while (true);
     48   }
     49 
     50   // if needed, you can switch between LoRa and FSK modes
     51   //
     52   // radio.begin()       start LoRa mode (and disable FSK)
     53   // radio.beginFSK()    start FSK mode (and disable LoRa)
     54 
     55   // the following settings can also
     56   // be modified at run-time
     57   state = radio.setFrequency(433.5);
     58   state = radio.setBitRate(100.0);
     59   state = radio.setFrequencyDeviation(10.0);
     60   state = radio.setRxBandwidth(250.0);
     61   state = radio.setOutputPower(10.0);
     62   state = radio.setCurrentLimit(100.0);
     63   state = radio.setDataShaping(RADIOLIB_SHAPING_1_0);
     64   uint8_t syncWord[] = {0x01, 0x23, 0x45, 0x67,
     65                         0x89, 0xAB, 0xCD, 0xEF};
     66   state = radio.setSyncWord(syncWord, 8);
     67   if (state != RADIOLIB_ERR_NONE) {
     68     Serial.print(F("Unable to set configuration, code "));
     69     Serial.println(state);
     70     while (true);
     71   }
     72 
     73   // FSK modem on SX126x can handle the sync word setting in bits, not just
     74   // whole bytes. The value used is left-justified.
     75   // This makes same result as radio.setSyncWord(syncWord, 8):
     76   state = radio.setSyncBits(syncWord, 64);
     77   // This will use 0x012 as sync word (12 bits only):
     78   state = radio.setSyncBits(syncWord, 12);
     79 
     80   // FSK modem allows advanced CRC configuration
     81   // Default is CCIT CRC16 (2 bytes, initial 0x1D0F, polynomial 0x1021, inverted)
     82   // Set CRC to IBM CRC (2 bytes, initial 0xFFFF, polynomial 0x8005, non-inverted)
     83   state = radio.setCRC(2, 0xFFFF, 0x8005, false);
     84   // set CRC length to 0 to disable CRC
     85 
     86   #warning "This sketch is just an API guide! Read the note at line 6."
     87 }
     88 
     89 void loop() {
     90   // FSK modem can use the same transmit/receive methods
     91   // as the LoRa modem, even their interrupt-driven versions
     92 
     93   // transmit FSK packet
     94   int state = radio.transmit("Hello World!");
     95   /*
     96     byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
     97                       0x89, 0xAB, 0xCD, 0xEF};
     98     int state = radio.transmit(byteArr, 8);
     99   */
    100   if (state == RADIOLIB_ERR_NONE) {
    101     Serial.println(F("[SX1262] Packet transmitted successfully!"));
    102   } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
    103     Serial.println(F("[SX1262] Packet too long!"));
    104   } else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
    105     Serial.println(F("[SX1262] Timed out while transmitting!"));
    106   } else {
    107     Serial.println(F("[SX1262] Failed to transmit packet, code "));
    108     Serial.println(state);
    109   }
    110 
    111   // receive FSK packet
    112   String str;
    113   state = radio.receive(str);
    114   /*
    115     byte byteArr[8];
    116     int state = radio.receive(byteArr, 8);
    117   */
    118   if (state == RADIOLIB_ERR_NONE) {
    119     Serial.println(F("[SX1262] Received packet!"));
    120     Serial.print(F("[SX1262] Data:\t"));
    121     Serial.println(str);
    122   } else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
    123     Serial.println(F("[SX1262] Timed out while waiting for packet!"));
    124   } else {
    125     Serial.println(F("[SX1262] Failed to receive packet, code "));
    126     Serial.println(state);
    127   }
    128 
    129   // FSK modem has built-in address filtering system
    130   // it can be enabled by setting node address, broadcast
    131   // address, or both
    132   //
    133   // to transmit packet to a particular address,
    134   // use the following methods:
    135   //
    136   // radio.transmit("Hello World!", address);
    137   // radio.startTransmit("Hello World!", address);
    138 
    139   // set node address to 0x02
    140   state = radio.setNodeAddress(0x02);
    141   // set broadcast address to 0xFF
    142   state = radio.setBroadcastAddress(0xFF);
    143   if (state != RADIOLIB_ERR_NONE) {
    144     Serial.println(F("[SX1262] Unable to set address filter, code "));
    145     Serial.println(state);
    146   }
    147 
    148   // address filtering can also be disabled
    149   // NOTE: calling this method will also erase previously set
    150   //       node and broadcast address
    151   /*
    152     state = radio.disableAddressFiltering();
    153     if (state != RADIOLIB_ERR_NONE) {
    154       Serial.println(F("Unable to remove address filter, code "));
    155     }
    156   */
    157 }