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