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

SX127x_FSK_Modem.ino (6673B)

      1 /*
      2    RadioLib SX127x FSK Modem Example
      3 
      4    This example shows how to use FSK modem in SX127x 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#sx127xrfm9x---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 // SX1278 has the following connections:
     23 // NSS pin:   10
     24 // DIO0 pin:  2
     25 // RESET pin: 9
     26 // DIO1 pin:  3
     27 SX1278 radio = new Module(10, 2, 9, 3);
     28 
     29 // or using RadioShield
     30 // https://github.com/jgromes/RadioShield
     31 //SX1278 fsk = RadioShield.ModuleA;
     32 
     33 void setup() {
     34   Serial.begin(9600);
     35 
     36   // initialize SX1278 FSK modem with default settings
     37   Serial.print(F("[SX1278] Initializing ... "));
     38   int state = radio.beginFSK();
     39   if (state == RADIOLIB_ERR_NONE) {
     40     Serial.println(F("success!"));
     41   } else {
     42     Serial.print(F("failed, code "));
     43     Serial.println(state);
     44     while (true);
     45   }
     46 
     47   // if needed, you can switch between LoRa and FSK modes
     48   //
     49   // radio.begin()       start LoRa mode (and disable FSK)
     50   // radio.beginFSK()    start FSK mode (and disable LoRa)
     51 
     52   // the following settings can also
     53   // be modified at run-time
     54   state = radio.setFrequency(433.5);
     55   state = radio.setBitRate(100.0);
     56   state = radio.setFrequencyDeviation(10.0);
     57   state = radio.setRxBandwidth(250.0);
     58   state = radio.setOutputPower(10.0);
     59   state = radio.setCurrentLimit(100);
     60   state = radio.setDataShaping(RADIOLIB_SHAPING_0_5);
     61   uint8_t syncWord[] = {0x01, 0x23, 0x45, 0x67,
     62                         0x89, 0xAB, 0xCD, 0xEF};
     63   state = radio.setSyncWord(syncWord, 8);
     64   if (state != RADIOLIB_ERR_NONE) {
     65     Serial.print(F("Unable to set configuration, code "));
     66     Serial.println(state);
     67     while (true);
     68   }
     69 
     70   // FSK modulation can be changed to OOK
     71   // NOTE: When using OOK, the maximum bit rate is only 32.768 kbps!
     72   //       Also, data shaping changes from Gaussian filter to
     73   //       simple filter with cutoff frequency. Make sure to call
     74   //       setDataShapingOOK() to set the correct shaping!
     75   state = radio.setOOK(true);
     76   state = radio.setDataShapingOOK(1);
     77   if (state != RADIOLIB_ERR_NONE) {
     78     Serial.print(F("Unable to change modulation, code "));
     79     Serial.println(state);
     80     while (true);
     81   }
     82 
     83   #warning "This sketch is just an API guide! Read the note at line 6."
     84 }
     85 
     86 void loop() {
     87   // FSK modem can use the same transmit/receive methods
     88   // as the LoRa modem, even their interrupt-driven versions
     89   // NOTE: FSK modem maximum packet length is 63 bytes!
     90 
     91   // transmit FSK packet
     92   int state = radio.transmit("Hello World!");
     93   /*
     94     byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
     95                       0x89, 0xAB, 0xCD, 0xEF};
     96     int state = radio.transmit(byteArr, 8);
     97   */
     98   if (state == RADIOLIB_ERR_NONE) {
     99     Serial.println(F("[SX1278] Packet transmitted successfully!"));
    100   } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
    101     Serial.println(F("[SX1278] Packet too long!"));
    102   } else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
    103     Serial.println(F("[SX1278] Timed out while transmitting!"));
    104   } else {
    105     Serial.println(F("[SX1278] Failed to transmit packet, code "));
    106     Serial.println(state);
    107   }
    108 
    109   // receive FSK packet
    110   String str;
    111   state = radio.receive(str);
    112   /*
    113     byte byteArr[8];
    114     int state = radio.receive(byteArr, 8);
    115   */
    116   if (state == RADIOLIB_ERR_NONE) {
    117     Serial.println(F("[SX1278] Received packet!"));
    118     Serial.print(F("[SX1278] Data:\t"));
    119     Serial.println(str);
    120   } else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
    121     Serial.println(F("[SX1278] Timed out while waiting for packet!"));
    122   } else {
    123     Serial.println(F("[SX1278] Failed to receive packet, code "));
    124     Serial.println(state);
    125   }
    126 
    127   // FSK modem has built-in address filtering system
    128   // it can be enabled by setting node address, broadcast
    129   // address, or both
    130   //
    131   // to transmit packet to a particular address,
    132   // use the following methods:
    133   //
    134   // radio.transmit("Hello World!", address);
    135   // radio.startTransmit("Hello World!", address);
    136 
    137   // set node address to 0x02
    138   state = radio.setNodeAddress(0x02);
    139   // set broadcast address to 0xFF
    140   state = radio.setBroadcastAddress(0xFF);
    141   if (state != RADIOLIB_ERR_NONE) {
    142     Serial.println(F("[SX1278] Unable to set address filter, code "));
    143     Serial.println(state);
    144   }
    145 
    146   // address filtering can also be disabled
    147   // NOTE: calling this method will also erase previously set
    148   //       node and broadcast address
    149   /*
    150     state = radio.disableAddressFiltering();
    151     if (state != RADIOLIB_ERR_NONE) {
    152       Serial.println(F("Unable to remove address filter, code "));
    153     }
    154   */
    155 
    156   // FSK modem supports direct data transmission
    157   // in this mode, SX127x directly transmits any data
    158   // sent to DIO1 (data) and DIO2 (clock)
    159 
    160   // activate direct mode transmitter
    161   state = radio.transmitDirect();
    162   if (state != RADIOLIB_ERR_NONE) {
    163     Serial.println(F("[SX1278] Unable to start direct transmission mode, code "));
    164     Serial.println(state);
    165   }
    166 
    167   // using the direct mode, it is possible to transmit
    168   // FM notes with Arduino tone() function
    169 
    170   // it is recommended to set data shaping to 0
    171   // (no shaping) when transmitting audio
    172   state = radio.setDataShaping(0.0);
    173   if (state != RADIOLIB_ERR_NONE) {
    174     Serial.println(F("[SX1278] Unable to set data shaping, code "));
    175     Serial.println(state);
    176   }
    177 
    178   // transmit FM tone at 1000 Hz for 1 second, then 500 Hz for 1 second
    179   // (DIO2 is connected to Arduino pin 4)
    180   // Note: tone() function is not available on ESP32, Arduino Due and CubeCell
    181   //       on these platforms, the following will do nothing
    182   #if !defined(RADIOLIB_TONE_UNSUPPORTED)
    183   tone(4, 1000);
    184   delay(1000);
    185   tone(4, 500);
    186   delay(1000);
    187   noTone(4);
    188   #endif
    189 
    190   // NOTE: after calling transmitDirect(), SX127x will start
    191   // transmitting immediately! This signal can jam other
    192   // devices at the same frequency, it is up to the user
    193   // to disable it with standby() method!
    194 
    195   // direct mode transmissions can also be received
    196   // as bit stream on DIO1 (data) and DIO2 (clock)
    197   state = radio.receiveDirect();
    198   if (state != RADIOLIB_ERR_NONE) {
    199     Serial.println(F("[SX1278] Unable to start direct reception mode, code "));
    200     Serial.println(state);
    201   }
    202 
    203   // NOTE: you will not be able to send or receive packets
    204   // while direct mode is active! to deactivate it, call method
    205   // radio.packetMode()
    206 }