RadioLib/examples/RF69/RF69_Receive_AES/RF69_Receive_AES.ino

93 wiersze
2.3 KiB
Arduino
Czysty Zwykły widok Historia

2018-07-14 09:09:09 +00:00
/*
2019-02-08 14:58:29 +00:00
RadioLib RF69 Receive with AES Example
2018-07-23 10:42:22 +00:00
This example receives packets using RF69 FSK radio module.
Packets are decrypted using hardware AES.
NOTE: When using address filtering, the address byte is NOT encrypted!
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 09:09:09 +00:00
// include the library
2019-02-08 14:58:29 +00:00
#include <RadioLib.h>
2018-07-14 09:09:09 +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 09:09:09 +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 09:09:09 +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 09:09:09 +00:00
}
// set AES key that will be used to decrypt the packet
// NOTE: the key must be exactly 16 bytes long!
uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2019-06-02 13:02:56 +00:00
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
radio.setAESKey(key);
2018-07-14 09:09:09 +00:00
// enable AES encryption
radio.enableAES();
2018-07-14 09:09:09 +00:00
// AES encryption can also be disabled
/*
radio.disableAES();
2018-07-14 09:09:09 +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 09:09:09 +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 09:09:09 +00:00
*/
2021-11-14 10:40:31 +00:00
if (state == RADIOLIB_ERR_NONE) {
2018-07-14 09:09:09 +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 09:09:09 +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 09:09:09 +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 09:09:09 +00:00
}
}