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