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_Transmit_FHSS.ino (5404B)

      1 /*
      2    RadioLib SX127x Transmit with Frequency Hopping Example
      3 
      4    This example transmits packets using SX1278 LoRa radio module.
      5    Each packet contains up to 256 bytes of data, in the form of:
      6     - Arduino String
      7     - null-terminated char array (C-string)
      8     - arbitrary binary data (byte array)
      9 
     10    Other modules from SX127x/RFM9x family can also be used.
     11 
     12    For default module settings, see the wiki page
     13    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
     14 
     15    For full API reference, see the GitHub Pages
     16    https://jgromes.github.io/RadioLib/
     17 
     18    SX127x supports FHSS or Frequency Hopping Spread Spectrum.
     19    Once a hopping period is set and a transmission is started, the radio
     20    will begin triggering interrupts every hop period where the radio frequency
     21    is changed to the next channel.
     22 */
     23 
     24 #include <RadioLib.h>
     25 
     26 // SX1278 has the following connections:
     27 // NSS pin:   10
     28 // DIO0 pin:  2
     29 // RESET pin: 9
     30 // DIO1 pin:  3
     31 SX1278 radio = new Module(10, 2, 9, 3);
     32 
     33 // or using RadioShield
     34 // https://github.com/jgromes/RadioShield
     35 //SX1278 radio = RadioShield.ModuleA;
     36 
     37 // flag to indicate that a packet was received
     38 volatile bool transmittedFlag = false;
     39 
     40 // flag to indicate frequency must be changed
     41 volatile bool fhssChangeFlag = false;
     42 
     43 // the channel frequencies can be generated randomly or hard coded
     44 // NOTE: The frequency list MUST be the same on both sides!
     45 float channels[] = { 433.0, 433.4, 433.2, 433.6, 434.0, 433.8 };
     46 int numberOfChannels = sizeof(channels) / sizeof(float);
     47 
     48 // counter to keep track of how many frequency hops were performed
     49 int hopsCompleted = 0;
     50 
     51 // counter that increments with each sent packet
     52 int packetCounter = 0;
     53 
     54 // save transmission state between loops
     55 int transmissionState = RADIOLIB_ERR_NONE;
     56 
     57 // this is the packet that will be sent
     58 String longPacket = "Let's create a really long packet to trigger \
     59 lots of hop interrupts. A packet can be up to 256 bytes long. \
     60 This packet is 222 bytes so using sf = 9, bw = 125, timeOnAir is \
     61 1488ms. 1488ms / (9*4.10ms) = 40 hops. Counter: ";
     62 
     63 // this function is called when a complete packet
     64 // is transmitted by the module
     65 // IMPORTANT: this function MUST be 'void' type
     66 //            and MUST NOT have any arguments!
     67 #if defined(ESP8266) || defined(ESP32)
     68   ICACHE_RAM_ATTR
     69 #endif
     70 void setTxFlag(void) {
     71   transmittedFlag = true;
     72 }
     73 
     74 // this function is called when FhssChangeChannel interrupt occurs
     75 // (at the beginning of each transmission)
     76 // IMPORTANT: this function MUST be 'void' type
     77 //            and MUST NOT have any arguments!
     78 #if defined(ESP8266) || defined(ESP32)
     79   ICACHE_RAM_ATTR
     80 #endif
     81 void setFHSSFlag(void) {
     82   fhssChangeFlag = true;
     83 }
     84 
     85 void setup() {
     86   Serial.begin(9600);
     87 
     88   // begin radio on home channel
     89   Serial.print(F("[SX1278] Initializing ... "));
     90   int state = radio.begin(channels[0]);
     91   if (state == RADIOLIB_ERR_NONE) {
     92     Serial.println(F("success!"));
     93   } else {
     94     Serial.print(F("failed, code "));
     95     Serial.println(state);
     96     while (true);
     97   }
     98 
     99   // set hop period in symbols
    100   // this will also enable FHSS
    101   state = radio.setFHSSHoppingPeriod(9);
    102   if (state == RADIOLIB_ERR_NONE) {
    103     Serial.println(F("success!"));
    104   } else {
    105     Serial.print(F("failed, code "));
    106     Serial.println(state);
    107     while (true);
    108   }
    109 
    110   // set the function to call when transmission is finished
    111   radio.setDio0Action(setTxFlag);
    112 
    113   // set the function to call when we need to hcange frequency
    114   radio.setDio1Action(setFHSSFlag);
    115 
    116   // start transmitting the first packet
    117   Serial.print(F("[SX1278] Sending first packet ... "));
    118   String packet = longPacket + packetCounter;
    119   transmissionState = radio.startTransmit(packet);
    120 }
    121 
    122 void loop() {
    123   // check if the transmission flag is set
    124   if (transmittedFlag == true) {
    125     // reset flag
    126     transmittedFlag = false;
    127 
    128     if (transmissionState == RADIOLIB_ERR_NONE) {
    129       // packet was successfully sent
    130       Serial.println(F("transmission finished!"));
    131 
    132     } else {
    133       Serial.print(F("failed, code "));
    134       Serial.println(transmissionState);
    135 
    136     }
    137 
    138     // The channel is automatically reset to 0 upon completion
    139     Serial.print(F("[SX1278] Radio is on channel: "));
    140     Serial.println(radio.getFHSSChannel());
    141 
    142     // print the number of hops it took
    143     Serial.print(F("[SX1278] Hops completed: "));
    144     Serial.println(hopsCompleted);
    145 
    146     // reset the counter
    147     hopsCompleted = 0;
    148 
    149     // return to home channel before the next transaction
    150     radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
    151 
    152     // wait a second before transmitting again
    153     delay(1000);
    154 
    155     // increment the packet counter
    156     packetCounter++;
    157 
    158     // send another packet
    159     Serial.print(F("[SX1278] Sending another packet ... "));
    160     String packet = longPacket + packetCounter;
    161     transmissionState = radio.startTransmit(packet);
    162   }
    163 
    164   // check if we need to do another frequency hop
    165   if (fhssChangeFlag == true) {
    166     // we do, change it now
    167     int state = radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
    168     if (state != RADIOLIB_ERR_NONE) {
    169       Serial.print(F("[SX1278] Failed to change frequency, code "));
    170       Serial.println(state);
    171     }
    172 
    173     // increment the counter
    174     hopsCompleted++;
    175 
    176     // clear the FHSS interrupt
    177     radio.clearFHSSInt();
    178 
    179     // we're ready to do another hop, clear the flag
    180     fhssChangeFlag = false;
    181   }
    182 }