RadioLib/examples/SX1231/SX1231_Receive/SX1231_Receive.ino

84 wiersze
2.1 KiB
Arduino
Czysty Zwykły widok Historia

2018-07-13 13:36:01 +00:00
/*
2019-02-08 14:58:29 +00:00
RadioLib SX1231 Receive Example
2018-07-23 10:42:33 +00:00
This example receives packets using SX1231 FSK radio module.
2019-02-06 18:39:44 +00:00
NOTE: SX1231 offers the same features as RF69 and has the same
interface. Please see RF69 examples for examples on AES,
address filtering, interrupts and settings.
2019-06-02 13:03:10 +00:00
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
2018-07-23 10:42:33 +00:00
*/
2018-07-13 13:36:01 +00:00
// include the library
2019-02-08 14:58:29 +00:00
#include <RadioLib.h>
2018-07-13 13:36:01 +00:00
2019-06-02 13:03:10 +00:00
// SX1231 has the following connections:
2019-12-27 12:17:14 +00:00
// CS pin: 10
2019-06-02 13:03:10 +00:00
// DIO0 pin: 2
2019-12-27 12:17:14 +00:00
// RESET pin: 3
2019-06-02 13:03:10 +00:00
SX1231 rf = new Module(10, 2, 3);
// or using RadioShield
// https://github.com/jgromes/RadioShield
//SX1231 rf = RadioShield.ModuleA;
2018-07-13 13:36:01 +00:00
void setup() {
Serial.begin(9600);
// initialize SX1231 with default settings
Serial.print(F("[SX1231] Initializing ... "));
// carrier frequency: 434.0 MHz
// bit rate: 48.0 kbps
// Rx bandwidth: 125.0 kHz
// frequency deviation: 50.0 kHz
// output power: 13 dBm
2019-02-06 18:39:44 +00:00
// sync word: 0x2D01
2019-12-27 12:17:14 +00:00
int state = rf.begin();
2018-07-23 10:42:33 +00:00
if (state == ERR_NONE) {
2018-07-13 13:36:01 +00:00
Serial.println(F("success!"));
} else {
2018-07-23 10:42:33 +00:00
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
2018-07-13 13:36:01 +00:00
}
}
void loop() {
Serial.print(F("[SX1231] Waiting for incoming transmission ... "));
// you can receive data as an Arduino String
String str;
2018-07-23 10:42:33 +00:00
int state = rf.receive(str);
2018-07-13 13:36:01 +00:00
// you can also receive data as byte array
/*
2018-07-23 10:42:33 +00:00
byte byteArr[8];
int state = rf.receive(byteArr, 8);
2018-07-13 13:36:01 +00:00
*/
2018-07-23 10:42:33 +00:00
if (state == ERR_NONE) {
2018-07-13 13:36:01 +00:00
// packet was successfully received
Serial.println(F("success!"));
// print the data of the packet
Serial.print(F("[SX1231] Data:\t\t"));
Serial.println(str);
2018-07-23 10:42:33 +00:00
} else if (state == ERR_RX_TIMEOUT) {
2018-07-13 13:36:01 +00:00
// timeout occurred while waiting for a packet
Serial.println(F("timeout!"));
2018-07-23 10:42:33 +00:00
} else if (state == ERR_CRC_MISMATCH) {
2018-07-13 13:36:01 +00:00
// packet was received, but is malformed
Serial.println(F("CRC error!"));
2018-07-23 10:42:33 +00:00
2019-06-02 13:03:10 +00:00
} else {
// some other error occurred
Serial.print(F("failed, code "));
Serial.println(state);
2018-07-13 13:36:01 +00:00
}
}