RadioLib/examples/RF69/RF69_Receive_Interrupt/RF69_Receive_Interrupt.ino

128 wiersze
3.1 KiB
Arduino
Czysty Zwykły widok Historia

2019-02-08 14:58:29 +00:00
/*
2019-02-10 10:11:54 +00:00
RadioLib RF69 Receive with Interrupts Example
2018-09-22 15:05:51 +00:00
2019-02-08 14:58:29 +00:00
This example listens for FSK transmissions and tries to
receive them. Once a packet is received, an interrupt is
2019-02-10 10:31:15 +00:00
triggered.
2019-06-02 13:02:56 +00:00
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#rf69sx1231
2019-06-02 13:02:56 +00:00
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
2019-02-08 14:58:29 +00:00
*/
2019-02-10 10:11:54 +00:00
// include the library
2019-02-08 14:58:29 +00:00
#include <RadioLib.h>
2019-06-02 13:02:56 +00:00
// RF69 has the following connections:
2019-12-27 12:17:01 +00:00
// CS pin: 10
2019-06-02 13:02:56 +00:00
// DIO0 pin: 2
2019-12-27 12:17:01 +00:00
// RESET pin: 3
RF69 radio = new Module(10, 2, 3);
2019-06-02 13:02:56 +00:00
// or using RadioShield
// https://github.com/jgromes/RadioShield
//RF69 radio = RadioShield.ModuleA;
2018-09-22 15:05:51 +00:00
void setup() {
Serial.begin(9600);
2019-05-24 12:14:10 +00:00
2018-09-22 15:05:51 +00:00
// initialize RF69 with default settings
2019-02-10 10:11:54 +00:00
Serial.print(F("[RF69] Initializing ... "));
int state = radio.begin();
2021-11-14 10:40:31 +00:00
if (state == RADIOLIB_ERR_NONE) {
2018-09-22 15:05:51 +00:00
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
}
2019-05-24 12:14:10 +00:00
// set the function that will be called
2018-09-22 15:05:51 +00:00
// when new packet is received
2023-06-21 20:15:54 +00:00
radio.setPacketReceivedAction(setFlag);
2019-05-24 12:14:10 +00:00
2018-09-22 15:05:51 +00:00
// start listening for packets
2019-02-10 10:11:54 +00:00
Serial.print(F("[RF69] Starting to listen ... "));
state = radio.startReceive();
2021-11-14 10:40:31 +00:00
if (state == RADIOLIB_ERR_NONE) {
2018-09-22 15:05:51 +00:00
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
}
// if needed, 'listen' mode can be disabled by calling
// any of the following methods:
//
// radio.standby()
// radio.sleep()
// radio.transmit();
// radio.receive();
// radio.readData();
2018-09-22 15:05:51 +00:00
}
// flag to indicate that a packet was received
volatile bool receivedFlag = false;
// this function is called when a complete packet
// is received by the module
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
2018-09-22 15:05:51 +00:00
void setFlag(void) {
// we got a packet, set the flag
receivedFlag = true;
}
void loop() {
// check if the flag is set
if(receivedFlag) {
// reset flag
receivedFlag = false;
2019-05-24 12:14:10 +00:00
2018-09-22 15:05:51 +00:00
// you can read received data as an Arduino String
String str;
int state = radio.readData(str);
2019-05-24 12:14:10 +00:00
2018-09-22 15:05:51 +00:00
// you can also read received data as byte array
/*
byte byteArr[8];
int numBytes = radio.getPacketLength();
int state = radio.readData(byteArr, numBytes);
2018-09-22 15:05:51 +00:00
*/
2019-05-24 12:14:10 +00:00
2021-11-14 10:40:31 +00:00
if (state == RADIOLIB_ERR_NONE) {
2018-09-22 15:05:51 +00:00
// packet was successfully received
2019-05-24 12:14:10 +00:00
Serial.println(F("[RF69] Received packet!"));
2018-09-22 15:05:51 +00:00
// print data of the packet
Serial.print(F("[RF69] Data:\t\t"));
2018-09-22 15:05:51 +00:00
Serial.println(str);
2019-06-02 13:02:56 +00:00
// print RSSI (Received Signal Strength Indicator)
// of the last received packet
Serial.print(F("[RF69] RSSI:\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));
2021-11-14 10:40:31 +00:00
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
2019-06-02 13:02:56 +00:00
// packet was received, but is malformed
Serial.println(F("CRC error!"));
} else {
// some other error occurred
Serial.print(F("failed, code "));
Serial.println(state);
2018-09-22 15:05:51 +00:00
}
// put module back to listen mode
radio.startReceive();
2018-09-22 15:05:51 +00:00
}
}