RadioLib/examples/RF69/RF69_Receive_Address/RF69_Receive_Address.ino

122 wiersze
3.2 KiB
Arduino
Czysty Zwykły widok Historia

2018-07-14 08:00:31 +00:00
/*
2019-02-08 14:58:29 +00:00
RadioLib RF69 Receive with Address Example
2018-07-23 10:42:22 +00:00
This example receives packets using RF69 FSK radio module.
Packets can have 1-byte address of the destination node.
After setting node (or broadcast) address, this node will
automatically filter out any packets that do not contain
either node address or broadcast address.
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/
2018-07-23 10:42:22 +00:00
*/
2018-07-14 08:00:31 +00:00
// include the library
2019-02-08 14:58:29 +00:00
#include <RadioLib.h>
2018-07-14 08:00:31 +00:00
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-07-14 08:00:31 +00:00
void setup() {
Serial.begin(9600);
// initialize RF69 with default settings
Serial.print(F("[RF69] Initializing ... "));
int state = radio.begin();
2021-11-14 10:40:31 +00:00
if (state == RADIOLIB_ERR_NONE) {
2018-07-14 08:00:31 +00:00
Serial.println(F("success!"));
} else {
2018-07-23 10:42:22 +00:00
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
2018-07-14 08:00:31 +00:00
}
// set node address
2019-05-24 12:14:10 +00:00
// NOTE: calling this method will automatically enable
2018-07-14 08:00:31 +00:00
// address filtering (node address only)
Serial.print(F("[RF69] Setting node address ... "));
state = radio.setNodeAddress(0x02);
2021-11-14 10:40:31 +00:00
if (state == RADIOLIB_ERR_NONE) {
2018-07-14 08:00:31 +00:00
Serial.println(F("success!"));
} else {
2018-07-23 10:42:22 +00:00
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
2018-07-14 08:00:31 +00:00
}
// set broadcast address
2020-03-22 07:11:32 +00:00
// NOTE: calling this method will automatically enable
// address filtering (node or broadcast address)
2018-07-14 08:00:31 +00:00
Serial.print(F("[RF69] Setting broadcast address ... "));
state = radio.setBroadcastAddress(0xFF);
2021-11-14 10:40:31 +00:00
if (state == RADIOLIB_ERR_NONE) {
2018-07-14 08:00:31 +00:00
Serial.println(F("success!"));
} else {
2018-07-23 10:42:22 +00:00
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
2018-07-14 08:00:31 +00:00
}
// address filtering can also be disabled
// NOTE: calling this method will also erase previously set
// node and broadcast address
/*
2018-07-23 10:42:22 +00:00
Serial.print(F("[RF69] Disabling address filtering ... "));
state == radio.disableAddressFiltering();
2021-11-14 10:40:31 +00:00
if(state == RADIOLIB_ERR_NONE) {
2018-08-18 14:16:41 +00:00
Serial.println(F("success!"));
2018-07-23 10:42:22 +00:00
} else {
2018-08-18 14:16:41 +00:00
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
2018-07-23 10:42:22 +00:00
}
2018-07-14 08:00:31 +00:00
*/
}
void loop() {
Serial.print(F("[RF69] Waiting for incoming transmission ... "));
// you can receive data as an Arduino String
String str;
int state = radio.receive(str);
2018-07-14 08:00:31 +00:00
// you can also receive data as byte array
/*
2018-07-23 10:42:22 +00:00
byte byteArr[8];
int state = radio.receive(byteArr, 8);
2018-07-14 08:00:31 +00:00
*/
2021-11-14 10:40:31 +00:00
if (state == RADIOLIB_ERR_NONE) {
2018-07-14 08:00:31 +00:00
// packet was successfully received
Serial.println(F("success!"));
// print the data of the packet
Serial.print(F("[RF69] Data:\t\t"));
Serial.println(str);
2018-07-23 10:42:22 +00:00
2021-11-14 10:40:31 +00:00
} else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
2018-07-14 08:00:31 +00:00
// timeout occurred while waiting for a packet
Serial.println(F("timeout!"));
2018-07-23 10:42:22 +00:00
2021-11-14 10:40:31 +00:00
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
2018-07-14 08:00:31 +00:00
// packet was received, but is malformed
Serial.println(F("CRC error!"));
2018-07-23 10:42:22 +00:00
2019-06-02 13:02:56 +00:00
} else {
// some other error occurred
Serial.print(F("failed, code "));
Serial.println(state);
2018-07-14 08:00:31 +00:00
}
}