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_Receive_FHSS.ino (5060B)
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 receivedFlag = 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 // this function is called when a complete packet 52 // is received by the module 53 // IMPORTANT: this function MUST be 'void' type 54 // and MUST NOT have any arguments! 55 #if defined(ESP8266) || defined(ESP32) 56 ICACHE_RAM_ATTR 57 #endif 58 void setRxFlag(void) { 59 receivedFlag = true; 60 } 61 62 // this function is called when FhssChangeChannel interrupt occurs 63 // (at the beginning of each transmission) 64 // IMPORTANT: this function MUST be 'void' type 65 // and MUST NOT have any arguments! 66 #if defined(ESP8266) || defined(ESP32) 67 ICACHE_RAM_ATTR 68 #endif 69 void setFHSSFlag(void) { 70 fhssChangeFlag = true; 71 } 72 73 void setup() { 74 Serial.begin(9600); 75 76 // begin radio on home channel 77 Serial.print(F("[SX1278] Initializing ... ")); 78 int state = radio.begin(channels[0]); 79 if (state == RADIOLIB_ERR_NONE) { 80 Serial.println(F("success!")); 81 } else { 82 Serial.print(F("failed, code ")); 83 Serial.println(state); 84 while (true); 85 } 86 87 // set hop period in symbols 88 // this will also enable FHSS 89 state = radio.setFHSSHoppingPeriod(9); 90 if (state == RADIOLIB_ERR_NONE) { 91 Serial.println(F("success!")); 92 } else { 93 Serial.print(F("failed, code ")); 94 Serial.println(state); 95 while (true); 96 } 97 98 // set the function to call when reception is finished 99 radio.setDio0Action(setRxFlag); 100 101 // set the function to call when we need to hcange frequency 102 radio.setDio1Action(setFHSSFlag); 103 104 // start listening for LoRa packets 105 Serial.print(F("[SX1278] Starting to listen ... ")); 106 state = radio.startReceive(); 107 if (state == RADIOLIB_ERR_NONE) { 108 Serial.println(F("success!")); 109 } else { 110 Serial.print(F("failed, code ")); 111 Serial.println(state); 112 while (true); 113 } 114 } 115 116 void loop() { 117 // check if the reception flag is set 118 if (receivedFlag == true) { 119 // you can read received data as an Arduino String 120 String str; 121 int state = radio.readData(str); 122 123 // you can also read received data as byte array 124 /* 125 byte byteArr[8]; 126 int state = radio.readData(byteArr, 8); 127 */ 128 129 if (state == RADIOLIB_ERR_NONE) { 130 // packet was successfully received 131 Serial.println(F("[SX1278] Received packet!")); 132 133 // print data of the packet 134 Serial.print(F("[SX1278] Data:\t\t")); 135 Serial.println(str); 136 137 } else if (state == RADIOLIB_ERR_CRC_MISMATCH) { 138 // packet was received, but is malformed 139 Serial.println(F("[SX1278] CRC error!")); 140 141 } else { 142 // some other error occurred 143 Serial.print(F("[SX1278] Failed, code ")); 144 Serial.println(state); 145 146 } 147 148 // print the number of hops it took 149 Serial.print(F("[SX1278] Hops completed: ")); 150 Serial.println(hopsCompleted); 151 152 // reset the counter 153 hopsCompleted = 0; 154 155 // put the module back to listen mode 156 radio.startReceive(); 157 158 // we're ready to receive more packets, clear the flag 159 receivedFlag = false; 160 } 161 162 // check if we need to do another frequency hop 163 if (fhssChangeFlag == true) { 164 // we do, change it now 165 int state = radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]); 166 if (state != RADIOLIB_ERR_NONE) { 167 Serial.print(F("[SX1278] Failed to change frequency, code ")); 168 Serial.println(state); 169 } 170 171 // increment the counter 172 hopsCompleted++; 173 174 // clear the FHSS interrupt 175 radio.clearFHSSInt(); 176 177 // we're ready to do another hop, clear the flag 178 fhssChangeFlag = false; 179 } 180 }