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 |
nRF24_Receive_Interrupt.ino (3903B)
1 /* 2 RadioLib nRF24 Receive Example 3 4 This example listens for FSK transmissions using nRF24 2.4 GHz radio module. 5 Once a packet is received, an interrupt is triggered. 6 To successfully receive data, the following settings have to be the same 7 on both transmitter and receiver: 8 - carrier frequency 9 - data rate 10 - transmit pipe on transmitter must match receive pipe 11 on receiver 12 13 For default module settings, see the wiki page 14 https://github.com/jgromes/RadioLib/wiki/Default-configuration#nrf24 15 16 For full API reference, see the GitHub Pages 17 https://jgromes.github.io/RadioLib/ 18 */ 19 20 // include the library 21 #include <RadioLib.h> 22 23 // nRF24 has the following connections: 24 // CS pin: 10 25 // IRQ pin: 2 26 // CE pin: 3 27 nRF24 radio = new Module(10, 2, 3); 28 29 // or using RadioShield 30 // https://github.com/jgromes/RadioShield 31 //nRF24 radio = RadioShield.ModuleA; 32 33 void setup() { 34 Serial.begin(9600); 35 36 // initialize nRF24 with default settings 37 Serial.print(F("[nRF24] Initializing ... ")); 38 int state = radio.begin(); 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 // set receive pipe 0 address 48 // NOTE: address width in bytes MUST be equal to the 49 // width set in begin() or setAddressWidth() 50 // methods (5 by default) 51 Serial.print(F("[nRF24] Setting address for receive pipe 0 ... ")); 52 byte addr[] = {0x01, 0x23, 0x45, 0x67, 0x89}; 53 state = radio.setReceivePipe(0, addr); 54 if(state == RADIOLIB_ERR_NONE) { 55 Serial.println(F("success!")); 56 } else { 57 Serial.print(F("failed, code ")); 58 Serial.println(state); 59 while(true); 60 } 61 62 // set the function that will be called 63 // when new packet is received 64 radio.setIrqAction(setFlag); 65 66 // start listening 67 Serial.print(F("[nRF24] Starting to listen ... ")); 68 state = radio.startReceive(); 69 if (state == RADIOLIB_ERR_NONE) { 70 Serial.println(F("success!")); 71 } else { 72 Serial.print(F("failed, code ")); 73 Serial.println(state); 74 while (true); 75 } 76 77 // if needed, 'listen' mode can be disabled by calling 78 // any of the following methods: 79 // 80 // radio.standby() 81 // radio.sleep() 82 // radio.transmit(); 83 // radio.receive(); 84 // radio.readData(); 85 } 86 87 // flag to indicate that a packet was received 88 volatile bool receivedFlag = false; 89 90 // disable interrupt when it's not needed 91 volatile bool enableInterrupt = true; 92 93 // this function is called when a complete packet 94 // is received by the module 95 // IMPORTANT: this function MUST be 'void' type 96 // and MUST NOT have any arguments! 97 #if defined(ESP8266) || defined(ESP32) 98 ICACHE_RAM_ATTR 99 #endif 100 void setFlag(void) { 101 // check if the interrupt is enabled 102 if(!enableInterrupt) { 103 return; 104 } 105 106 // we got a packet, set the flag 107 receivedFlag = true; 108 } 109 110 void loop() { 111 // check if the flag is set 112 if(receivedFlag) { 113 // disable the interrupt service routine while 114 // processing the data 115 enableInterrupt = false; 116 117 // reset flag 118 receivedFlag = false; 119 120 // you can read received data as an Arduino String 121 String str; 122 int state = radio.readData(str); 123 124 // you can also read received data as byte array 125 /* 126 byte byteArr[8]; 127 int state = radio.readData(byteArr, 8); 128 */ 129 130 if (state == RADIOLIB_ERR_NONE) { 131 // packet was successfully received 132 Serial.println(F("[nRF24] Received packet!")); 133 134 // print data of the packet 135 Serial.print(F("[nRF24] Data:\t\t")); 136 Serial.println(str); 137 138 } else { 139 // some other error occurred 140 Serial.print(F("[nRF24] Failed, code ")); 141 Serial.println(state); 142 143 } 144 145 // put module back to listen mode 146 radio.startReceive(); 147 148 // we're ready to receive more packets, 149 // enable interrupt service routine 150 enableInterrupt = true; 151 } 152 153 }