Initial commit of Arduino libraries

This commit is contained in:
Sam
2025-05-23 10:47:41 +10:00
commit 5bfce5fc3e
2476 changed files with 1108481 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
/*
RadioLib SX127x Blocking Channel Activity Detection Example
This example scans the current LoRa channel and detects
valid LoRa preambles. Preamble is the first part of
LoRa transmission, so this can be used to check
if the LoRa channel is free, or if you should start
receiving a message.
Other modules from SX127x/RFM9x family can also be used.
Using blocking CAD is not recommended, as it will lead
to significant amount of timeouts, inefficient use of processor
time and can some miss packets!
Instead, interrupt CAD is recommended.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
}
void loop() {
Serial.print(F("[SX1278] Scanning channel for LoRa preamble ... "));
// start scanning current channel
int state = radio.scanChannel();
if (state == RADIOLIB_PREAMBLE_DETECTED) {
// LoRa preamble was detected
Serial.println(F("detected preamble!"));
} else if (state == RADIOLIB_CHANNEL_FREE) {
// no preamble was detected, channel is free
Serial.println(F("channel is free!"));
}
// wait 100 ms before new scan
delay(100);
}

View File

@@ -0,0 +1,129 @@
/*
RadioLib SX127x Channel Activity Detection with Interrupts Example
This example scans the current LoRa channel and detects
valid LoRa preambles. Preamble is the first part of
LoRa transmission, so this can be used to check
if the LoRa channel is free, or if you should start
receiving a message.
Other modules from SX127x/RFM9x family can also be used.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
// flag to indicate that a preamble was not detected
volatile bool timeoutFlag = false;
// flag to indicate that a preamble was detected
volatile bool detectedFlag = false;
// this function is called when no preamble
// is detected within timeout period
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
void setFlagTimeout(void) {
// we timed out, set the flag
timeoutFlag = true;
}
// this function is called when LoRa preamble
// is detected within timeout period
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
void setFlagDetected(void) {
// we got a preamble, set the flag
detectedFlag = true;
}
void setup() {
// Serial port speed must be high enough for this example
Serial.begin(115200);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set the function that will be called
// when LoRa preamble is not detected within CAD timeout period
radio.setDio0Action(setFlagTimeout, RISING);
// set the function that will be called
// when LoRa preamble is detected
radio.setDio1Action(setFlagDetected, RISING);
// start scanning the channel
Serial.print(F("[SX1278] Starting scan for LoRa preamble ... "));
state = radio.startChannelScan();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}
}
void loop() {
// check if we need to restart channel activity detection
if(detectedFlag || timeoutFlag) {
// check if we got a preamble
if(detectedFlag) {
// LoRa preamble was detected
Serial.println(F("[SX1278] Preamble detected!"));
} else {
// nothing was detected
Serial.println(F("[SX1278] Channel free!"));
}
// start scanning the channel
Serial.print(F("[SX1278] Starting scan for LoRa preamble ... "));
// start scanning current channel
int state = radio.startChannelScan();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}
// reset flags
timeoutFlag = false;
detectedFlag = false;
}
}

View File

@@ -0,0 +1,208 @@
/*
RadioLib SX127x Receive after Channel Activity Detection Example
This example scans the current LoRa channel and detects
valid LoRa preambles. Preamble is the first part of
LoRa transmission, so this can be used to check
if the LoRa channel is free, or if you should start
receiving a message. If a preamble is detected,
the module will switch to receive mode and receive the packet.
For most use-cases, it should be enough to just use the
interrupt-driven reception described in the example
"SX127x_Receive_Interrupt".
Other modules from SX127x/RFM9x family can also be used.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
// flag to indicate that a preamble was not detected
volatile bool timeoutFlag = false;
// flag to indicate that a preamble was detected
volatile bool detectedFlag = false;
// flag to indicate if we are currently receiving
bool receiving = false;
// this function is called when no preamble
// is detected within timeout period
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
void setFlagTimeout(void) {
// we timed out, set the flag
timeoutFlag = true;
}
// this function is called when LoRa preamble
// is detected within timeout period
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
void setFlagDetected(void) {
// we got a preamble, set the flag
detectedFlag = true;
}
void setup() {
// Serial port speed must be high enough for this example
Serial.begin(115200);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set the function that will be called
// when LoRa preamble is not detected within CAD timeout period
// or when a packet is received
radio.setDio0Action(setFlagTimeout, RISING);
// set the function that will be called
// when LoRa preamble is detected
radio.setDio1Action(setFlagDetected, RISING);
// start scanning the channel
Serial.print(F("[SX1278] Starting scan for LoRa preamble ... "));
state = radio.startChannelScan();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}
}
void loop() {
// check if we need to restart channel activity detection
if(detectedFlag || timeoutFlag) {
int state = RADIOLIB_ERR_NONE;
// check ongoing reception
if(receiving) {
// DIO triggered while reception is ongoing
// that means we got a packet
// reset flags first
detectedFlag = false;
timeoutFlag = false;
// you can read received data as an Arduino String
String str;
state = radio.readData(str);
// you can also read received data as byte array
/*
byte byteArr[8];
state = radio.readData(byteArr, 8);
*/
if (state == RADIOLIB_ERR_NONE) {
// packet was successfully received
Serial.println(F("[SX1278] Received packet!"));
// print data of the packet
Serial.print(F("[SX1278] Data:\t\t"));
Serial.println(str);
// print RSSI (Received Signal Strength Indicator)
Serial.print(F("[SX1278] RSSI:\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));
// print SNR (Signal-to-Noise Ratio)
Serial.print(F("[SX1278] SNR:\t\t"));
Serial.print(radio.getSNR());
Serial.println(F(" dB"));
// print frequency error
Serial.print(F("[SX1278] Frequency error:\t"));
Serial.print(radio.getFrequencyError());
Serial.println(F(" Hz"));
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
// packet was received, but is malformed
Serial.println(F("[SX1278] CRC error!"));
} else {
// some other error occurred
Serial.print(F("[SX1278] Failed, code "));
Serial.println(state);
}
// reception is done now
receiving = false;
}
// check if we got a preamble
if(detectedFlag) {
// LoRa preamble was detected, start reception with timeout of 100 LoRa symbols
Serial.print(F("[SX1278] Preamble detected, starting reception ... "));
state = radio.startReceive(100);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
}
// set the flag for ongoing reception
receiving = true;
} else if(!receiving) {
// nothing was detected
// do not print anything, it just spams the console
}
// if we're not receiving, start scanning again
if(!receiving) {
int state = radio.startChannelScan();
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("[SX1278] Starting new scan failed, code "));
Serial.println(state);
}
}
// reset flags
timeoutFlag = false;
detectedFlag = false;
}
}

View File

@@ -0,0 +1,210 @@
/*
RadioLib SX127x FSK Modem Example
This example shows how to use FSK modem in SX127x chips.
NOTE: The sketch below is just a guide on how to use
FSK modem, so this code should not be run directly!
Instead, modify the other examples to use FSK
modem and use the appropriate configuration
methods.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---fsk-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
void setup() {
Serial.begin(9600);
// initialize SX1278 FSK modem with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.beginFSK();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// if needed, you can switch between LoRa and FSK modes
//
// radio.begin() start LoRa mode (and disable FSK)
// radio.beginFSK() start FSK mode (and disable LoRa)
// the following settings can also
// be modified at run-time
state = radio.setFrequency(433.5);
state = radio.setBitRate(100.0);
state = radio.setFrequencyDeviation(10.0);
state = radio.setRxBandwidth(250.0);
state = radio.setOutputPower(10.0);
state = radio.setCurrentLimit(100);
state = radio.setDataShaping(RADIOLIB_SHAPING_0_5);
uint8_t syncWord[] = {0x01, 0x23, 0x45, 0x67,
0x89, 0xAB, 0xCD, 0xEF};
state = radio.setSyncWord(syncWord, 8);
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("Unable to set configuration, code "));
Serial.println(state);
while (true) { delay(10); }
}
// FSK modulation can be changed to OOK
// NOTE: When using OOK, the maximum bit rate is only 32.768 kbps!
// Also, data shaping changes from Gaussian filter to
// simple filter with cutoff frequency. Make sure to call
// setDataShapingOOK() to set the correct shaping!
state = radio.setOOK(true);
state = radio.setDataShapingOOK(1);
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("Unable to change modulation, code "));
Serial.println(state);
while (true) { delay(10); }
}
#warning "This sketch is just an API guide! Read the note at line 6."
}
void loop() {
// FSK modem can use the same transmit/receive methods
// as the LoRa modem, even their interrupt-driven versions
// NOTE: FSK modem maximum packet length is 63 bytes!
// transmit FSK packet
int state = radio.transmit("Hello World!");
/*
byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
0x89, 0xAB, 0xCD, 0xEF};
int state = radio.transmit(byteArr, 8);
*/
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("[SX1278] Packet transmitted successfully!"));
} else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
Serial.println(F("[SX1278] Packet too long!"));
} else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
Serial.println(F("[SX1278] Timed out while transmitting!"));
} else {
Serial.println(F("[SX1278] Failed to transmit packet, code "));
Serial.println(state);
}
// receive FSK packet
String str;
state = radio.receive(str);
/*
byte byteArr[8];
int state = radio.receive(byteArr, 8);
*/
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("[SX1278] Received packet!"));
Serial.print(F("[SX1278] Data:\t"));
Serial.println(str);
} else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
Serial.println(F("[SX1278] Timed out while waiting for packet!"));
} else {
Serial.println(F("[SX1278] Failed to receive packet, code "));
Serial.println(state);
}
// FSK modem has built-in address filtering system
// it can be enabled by setting node address, broadcast
// address, or both
//
// to transmit packet to a particular address,
// use the following methods:
//
// radio.transmit("Hello World!", address);
// radio.startTransmit("Hello World!", address);
// set node address to 0x02
state = radio.setNodeAddress(0x02);
// set broadcast address to 0xFF
state = radio.setBroadcastAddress(0xFF);
if (state != RADIOLIB_ERR_NONE) {
Serial.println(F("[SX1278] Unable to set address filter, code "));
Serial.println(state);
}
// address filtering can also be disabled
// NOTE: calling this method will also erase previously set
// node and broadcast address
/*
state = radio.disableAddressFiltering();
if (state != RADIOLIB_ERR_NONE) {
Serial.println(F("Unable to remove address filter, code "));
}
*/
// FSK modem supports direct data transmission
// in this mode, SX127x directly transmits any data
// sent to DIO1 (data) and DIO2 (clock)
// activate direct mode transmitter
state = radio.transmitDirect();
if (state != RADIOLIB_ERR_NONE) {
Serial.println(F("[SX1278] Unable to start direct transmission mode, code "));
Serial.println(state);
}
// using the direct mode, it is possible to transmit
// FM notes with Arduino tone() function
// it is recommended to set data shaping to 0
// (no shaping) when transmitting audio
state = radio.setDataShaping(0.0);
if (state != RADIOLIB_ERR_NONE) {
Serial.println(F("[SX1278] Unable to set data shaping, code "));
Serial.println(state);
}
// transmit FM tone at 1000 Hz for 1 second, then 500 Hz for 1 second
// (DIO2 is connected to Arduino pin 4)
// Note: tone() function is not available on Arduino Due and CubeCell
// on these platforms, the following will do nothing
#if !defined(RADIOLIB_TONE_UNSUPPORTED)
tone(4, 1000);
delay(1000);
tone(4, 500);
delay(1000);
noTone(4);
#endif
// NOTE: after calling transmitDirect(), SX127x will start
// transmitting immediately! This signal can jam other
// devices at the same frequency, it is up to the user
// to disable it with standby() method!
// direct mode transmissions can also be received
// as bit stream on DIO1 (data) and DIO2 (clock)
state = radio.receiveDirect();
if (state != RADIOLIB_ERR_NONE) {
Serial.println(F("[SX1278] Unable to start direct reception mode, code "));
Serial.println(state);
}
// NOTE: you will not be able to send or receive packets
// while direct mode is active! to deactivate it, call method
// radio.packetMode()
}

View File

@@ -0,0 +1,152 @@
/*
RadioLib SX127x Ping-Pong Example
This example is intended to run on two SX126x radios,
and send packets between the two.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// uncomment the following only on one
// of the nodes to initiate the pings
//#define INITIATING_NODE
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// NRST pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
// save transmission states between loops
int transmissionState = RADIOLIB_ERR_NONE;
// flag to indicate transmission or reception state
bool transmitFlag = false;
// flag to indicate that a packet was sent or received
volatile bool operationDone = false;
// this function is called when a complete packet
// is transmitted or 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
void setFlag(void) {
// we sent or received packet, set the flag
operationDone = true;
}
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set the function that will be called
// when new packet is received
radio.setDio0Action(setFlag, RISING);
#if defined(INITIATING_NODE)
// send the first packet on this node
Serial.print(F("[SX1278] Sending first packet ... "));
transmissionState = radio.startTransmit("Hello World!");
transmitFlag = true;
#else
// start listening for LoRa packets on this node
Serial.print(F("[SX1278] Starting to listen ... "));
state = radio.startReceive();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
#endif
}
void loop() {
// check if the previous operation finished
if(operationDone) {
// reset flag
operationDone = false;
if(transmitFlag) {
// the previous operation was transmission, listen for response
// print the result
if (transmissionState == RADIOLIB_ERR_NONE) {
// packet was successfully sent
Serial.println(F("transmission finished!"));
} else {
Serial.print(F("failed, code "));
Serial.println(transmissionState);
}
// listen for response
radio.startReceive();
transmitFlag = false;
} else {
// the previous operation was reception
// print data and send another packet
String str;
int state = radio.readData(str);
if (state == RADIOLIB_ERR_NONE) {
// packet was successfully received
Serial.println(F("[SX1278] Received packet!"));
// print data of the packet
Serial.print(F("[SX1278] Data:\t\t"));
Serial.println(str);
// print RSSI (Received Signal Strength Indicator)
Serial.print(F("[SX1278] RSSI:\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));
// print SNR (Signal-to-Noise Ratio)
Serial.print(F("[SX1278] SNR:\t\t"));
Serial.print(radio.getSNR());
Serial.println(F(" dB"));
}
// wait a second before transmitting again
delay(1000);
// send another one
Serial.print(F("[SX1278] Sending another packet ... "));
transmissionState = radio.startTransmit("Hello World!");
transmitFlag = true;
}
}
}

View File

@@ -0,0 +1,114 @@
/*
RadioLib SX127x Blocking Receive Example
This example listens for LoRa transmissions using SX127x Lora modules.
To successfully receive data, the following settings have to be the same
on both transmitter and receiver:
- carrier frequency
- bandwidth
- spreading factor
- coding rate
- sync word
- preamble length
Other modules from SX127x/RFM9x family can also be used.
Using blocking receive is not recommended, as it will lead
to significant amount of timeouts, inefficient use of processor
time and can some miss packets!
Instead, interrupt receive is recommended.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
}
void loop() {
Serial.print(F("[SX1278] Waiting for incoming transmission ... "));
// you can receive data as an Arduino String
String str;
int state = radio.receive(str);
// you can also receive data as byte array
/*
byte byteArr[8];
int state = radio.receive(byteArr, 8);
*/
if (state == RADIOLIB_ERR_NONE) {
// packet was successfully received
Serial.println(F("success!"));
// print the data of the packet
Serial.print(F("[SX1278] Data:\t\t\t"));
Serial.println(str);
// print the RSSI (Received Signal Strength Indicator)
// of the last received packet
Serial.print(F("[SX1278] RSSI:\t\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));
// print the SNR (Signal-to-Noise Ratio)
// of the last received packet
Serial.print(F("[SX1278] SNR:\t\t\t"));
Serial.print(radio.getSNR());
Serial.println(F(" dB"));
// print frequency error
// of the last received packet
Serial.print(F("[SX1278] Frequency error:\t"));
Serial.print(radio.getFrequencyError());
Serial.println(F(" Hz"));
} else if (state == RADIOLIB_ERR_RX_TIMEOUT) {
// timeout occurred while waiting for a packet
Serial.println(F("timeout!"));
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
// packet was received, but is malformed
Serial.println(F("CRC error!"));
} else {
// some other error occurred
Serial.print(F("failed, code "));
Serial.println(state);
}
}

View File

@@ -0,0 +1,84 @@
/*
RadioLib SX127x Direct Receive Example
This example shows how to receive FSK packets without using
SX127x packet engine.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// DIO2 pin: 5
const int pin = 5;
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
// this function is called when a new bit is received
void readBit(void) {
// read the data bit
radio.readBit(pin);
}
void setup() {
Serial.begin(9600);
// initialize SX1278 with FSK modem at 9600 bps
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.beginFSK(434.0, 9.6, 20.0);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set the direct mode sync word
// the default RadioLib FSK sync word is 0x12AD
// we can add in some preamble bits, to lower the chance
// of false detection
radio.setDirectSyncWord(0x555512AD, 32);
// set function that will be called each time a bit is received
radio.setDirectAction(readBit);
// start direct mode reception
radio.receiveDirect();
}
void loop() {
// we expect the packet to contain the string "Hello World!",
// a length byte and 2 CRC bytes, that's 15 bytes in total
if(radio.available() >= 15) {
Serial.println("[SX1278] Received packet in direct mode!");
while(radio.available()) {
// read a byte
byte b = radio.read();
// print it
Serial.print(b, HEX);
Serial.print('\t');
Serial.write(b);
Serial.println();
}
}
}

View File

@@ -0,0 +1,189 @@
/*
RadioLib SX127x Transmit with Frequency Hopping Example
This example transmits packets using SX1278 LoRa radio module.
Each packet contains up to 256 bytes of data, in the form of:
- Arduino String
- null-terminated char array (C-string)
- arbitrary binary data (byte array)
Other modules from SX127x/RFM9x family can also be used.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
SX127x supports FHSS or Frequency Hopping Spread Spectrum.
Once a hopping period is set and a transmission is started, the radio
will begin triggering interrupts every hop period where the radio frequency
is changed to the next channel.
*/
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
// flag to indicate that a packet was received
volatile bool receivedFlag = false;
// flag to indicate frequency must be changed
volatile bool fhssChangeFlag = false;
// the channel frequencies can be generated randomly or hard coded
// NOTE: The frequency list MUST be the same on both sides!
float channels[] = { 433.0, 433.4, 433.2, 433.6, 434.0, 433.8 };
int numberOfChannels = sizeof(channels) / sizeof(float);
// counter to keep track of how many frequency hops were performed
int hopsCompleted = 0;
// 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
void setRxFlag(void) {
receivedFlag = true;
}
// this function is called when FhssChangeChannel interrupt occurs
// (at the beginning of each transmission)
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
void setFHSSFlag(void) {
fhssChangeFlag = true;
}
void setup() {
Serial.begin(9600);
// begin radio on home channel
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin(channels[0]);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set hop period in symbols
// this will also enable FHSS
Serial.print(F("[SX1278] Setting hopping period ... "));
state = radio.setFHSSHoppingPeriod(9);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set the function to call when reception is finished
radio.setDio0Action(setRxFlag, RISING);
// set the function to call when we need to change frequency
radio.setDio1Action(setFHSSFlag, RISING);
// start listening for LoRa packets
Serial.print(F("[SX1278] Starting to listen ... "));
state = radio.startReceive();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
}
void loop() {
// check if the reception flag is set
if (receivedFlag == true) {
// you can read received data as an Arduino String
String str;
int state = radio.readData(str);
// you can also read received data as byte array
/*
byte byteArr[8];
int numBytes = radio.getPacketLength();
int state = radio.readData(byteArr, numBytes);
*/
if (state == RADIOLIB_ERR_NONE) {
// packet was successfully received
Serial.println(F("[SX1278] Received packet!"));
// print data of the packet
Serial.print(F("[SX1278] Data:\t\t"));
Serial.println(str);
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
// packet was received, but is malformed
Serial.println(F("[SX1278] CRC error!"));
} else {
// some other error occurred
Serial.print(F("[SX1278] Failed, code "));
Serial.println(state);
}
// print the number of hops it took
Serial.print(F("[SX1278] Hops completed: "));
Serial.println(hopsCompleted);
// reset the counter
hopsCompleted = 0;
// return to home channel before the next transaction
radio.setFrequency(channels[0]);
// put the module back to listen mode
radio.startReceive();
// we're ready to receive more packets, clear the flag
receivedFlag = false;
}
// check if we need to do another frequency hop
if (fhssChangeFlag == true) {
// we do, change it now
int state = radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("[SX1278] Failed to change frequency, code "));
Serial.println(state);
}
// increment the counter
hopsCompleted++;
// clear the FHSS interrupt
radio.clearFHSSInt();
// we're ready to do another hop, clear the flag
fhssChangeFlag = false;
}
}

View File

@@ -0,0 +1,147 @@
/*
RadioLib SX127x Receive with Interrupts Example
This example listens for LoRa transmissions and tries to
receive them. Once a packet is received, an interrupt is
triggered. To successfully receive data, the following
settings have to be the same on both transmitter
and receiver:
- carrier frequency
- bandwidth
- spreading factor
- coding rate
- sync word
Other modules from SX127x/RFM9x family can also be used.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
// 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
void setFlag(void) {
// we got a packet, set the flag
receivedFlag = true;
}
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set the function that will be called
// when new packet is received
radio.setPacketReceivedAction(setFlag);
// start listening for LoRa packets
Serial.print(F("[SX1278] Starting to listen ... "));
state = radio.startReceive();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// if needed, 'listen' mode can be disabled by calling
// any of the following methods:
//
// radio.standby()
// radio.sleep()
// radio.transmit();
// radio.receive();
// radio.scanChannel();
}
void loop() {
// check if the flag is set
if(receivedFlag) {
// reset flag
receivedFlag = false;
// you can read received data as an Arduino String
String str;
int state = radio.readData(str);
// you can also read received data as byte array
/*
byte byteArr[8];
int numBytes = radio.getPacketLength();
int state = radio.readData(byteArr, numBytes);
*/
if (state == RADIOLIB_ERR_NONE) {
// packet was successfully received
Serial.println(F("[SX1278] Received packet!"));
// print data of the packet
Serial.print(F("[SX1278] Data:\t\t"));
Serial.println(str);
// print RSSI (Received Signal Strength Indicator)
Serial.print(F("[SX1278] RSSI:\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));
// print SNR (Signal-to-Noise Ratio)
Serial.print(F("[SX1278] SNR:\t\t"));
Serial.print(radio.getSNR());
Serial.println(F(" dB"));
// print frequency error
Serial.print(F("[SX1278] Frequency error:\t"));
Serial.print(radio.getFrequencyError());
Serial.println(F(" Hz"));
} else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
// packet was received, but is malformed
Serial.println(F("[SX1278] CRC error!"));
} else {
// some other error occurred
Serial.print(F("[SX1278] Failed, code "));
Serial.println(state);
}
}
}

View File

@@ -0,0 +1,156 @@
/*
RadioLib SX127x Settings Example
This example shows how to change all the properties of LoRa transmission.
RadioLib currently supports the following settings:
- pins (SPI slave select, digital IO 0, digital IO 1)
- carrier frequency
- bandwidth
- spreading factor
- coding rate
- sync word
- output power during transmission
Other modules from SX127x/RFM9x family can also be used.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio1 = new Module(10, 2, 9, 3);
// SX1272 has different connections:
// NSS pin: 9
// DIO0 pin: 4
// RESET pin: 5
// DIO1 pin: 6
SX1272 radio2 = new Module(9, 4, 5, 6);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio3 = new RadioModule();
*/
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio1.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// initialize the second LoRa instance with
// non-default settings
// this LoRa link will have high data rate,
// but lower range
// NOTE: when using spreading factor 6, the total packet
// length has to be known in advance!
// you have to pass the number of expected bytes
// to the receive() method
Serial.print(F("[SX1272] Initializing ... "));
// carrier frequency: 915.0 MHz
// bandwidth: 500.0 kHz
// spreading factor: 6
// coding rate: 5
// sync word: 0x34
// output power: 2 dBm
// preamble length: 20 symbols
// amplifier gain: 1 (maximum gain)
state = radio2.begin(915.0, 500.0, 6, 5, 0x34, 2, 20, 1);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// you can also change the settings at runtime
// and check if the configuration was changed successfully
// set carrier frequency to 433.5 MHz
if (radio1.setFrequency(433.5) == RADIOLIB_ERR_INVALID_FREQUENCY) {
Serial.println(F("Selected frequency is invalid for this module!"));
while (true) { delay(10); }
}
// set bandwidth to 250 kHz
if (radio1.setBandwidth(250.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) {
Serial.println(F("Selected bandwidth is invalid for this module!"));
while (true) { delay(10); }
}
// set spreading factor to 10
if (radio1.setSpreadingFactor(10) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) {
Serial.println(F("Selected spreading factor is invalid for this module!"));
while (true) { delay(10); }
}
// set coding rate to 6
if (radio1.setCodingRate(6) == RADIOLIB_ERR_INVALID_CODING_RATE) {
Serial.println(F("Selected coding rate is invalid for this module!"));
while (true) { delay(10); }
}
// set LoRa sync word to 0x14
// NOTE: value 0x34 is reserved for LoRaWAN networks and should not be used
if (radio1.setSyncWord(0x14) != RADIOLIB_ERR_NONE) {
Serial.println(F("Unable to set sync word!"));
while (true) { delay(10); }
}
// set output power to 10 dBm (accepted range is -3 - 17 dBm)
// NOTE: 20 dBm value allows high power operation, but transmission
// duty cycle MUST NOT exceed 1%
if (radio1.setOutputPower(10) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
Serial.println(F("Selected output power is invalid for this module!"));
while (true) { delay(10); }
}
// set over current protection limit to 80 mA (accepted range is 45 - 240 mA)
// NOTE: set value to 0 to disable overcurrent protection
if (radio1.setCurrentLimit(80) == RADIOLIB_ERR_INVALID_CURRENT_LIMIT) {
Serial.println(F("Selected current limit is invalid for this module!"));
while (true) { delay(10); }
}
// set LoRa preamble length to 15 symbols (accepted range is 6 - 65535)
if (radio1.setPreambleLength(15) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) {
Serial.println(F("Selected preamble length is invalid for this module!"));
while (true) { delay(10); }
}
// set amplifier gain to 1 (accepted range is 1 - 6, where 1 is maximum gain)
// NOTE: set value to 0 to enable automatic gain control
// leave at 0 unless you know what you're doing
if (radio1.setGain(1) == RADIOLIB_ERR_INVALID_GAIN) {
Serial.println(F("Selected gain is invalid for this module!"));
while (true) { delay(10); }
}
Serial.println(F("All settings successfully changed!"));
}
void loop() {
// nothing here
}

View File

@@ -0,0 +1,109 @@
/*
RadioLib SX127x Blocking Transmit Example
This example transmits packets using SX1278 LoRa radio module.
Each packet contains up to 255 bytes of data, in the form of:
- Arduino String
- null-terminated char array (C-string)
- arbitrary binary data (byte array)
Other modules from SX127x/RFM9x family can also be used.
Using blocking transmit is not recommended, as it will lead
to inefficient use of processor time!
Instead, interrupt transmit is recommended.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// some modules have an external RF switch
// controlled via two pins (RX enable, TX enable)
// to enable automatic control of the switch,
// call the following method
// RX enable: 4
// TX enable: 5
/*
radio.setRfSwitchPins(4, 5);
*/
}
// counter to keep track of transmitted packets
int count = 0;
void loop() {
Serial.print(F("[SX1278] Transmitting packet ... "));
// you can transmit C-string or Arduino string up to
// 255 characters long
String str = "Hello World! #" + String(count++);
int state = radio.transmit(str);
// you can also transmit byte array up to 256 bytes long
/*
byte byteArr[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
int state = radio.transmit(byteArr, 8);
*/
if (state == RADIOLIB_ERR_NONE) {
// the packet was successfully transmitted
Serial.println(F(" success!"));
// print measured data rate
Serial.print(F("[SX1278] Datarate:\t"));
Serial.print(radio.getDataRate());
Serial.println(F(" bps"));
} else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
// the supplied packet was longer than 256 bytes
Serial.println(F("too long!"));
} else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
// timeout occurred while transmitting packet
Serial.println(F("timeout!"));
} else {
// some other error occurred
Serial.print(F("failed, code "));
Serial.println(state);
}
// wait for a second before transmitting again
delay(1000);
}

View File

@@ -0,0 +1,187 @@
/*
RadioLib SX127x Transmit with Frequency Hopping Example
This example transmits packets using SX1278 LoRa radio module.
Each packet contains up to 255 bytes of data, in the form of:
- Arduino String
- null-terminated char array (C-string)
- arbitrary binary data (byte array)
Other modules from SX127x/RFM9x family can also be used.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
SX127x supports FHSS or Frequency Hopping Spread Spectrum.
Once a hopping period is set and a transmission is started, the radio
will begin triggering interrupts every hop period where the radio frequency
is changed to the next channel.
*/
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
// flag to indicate that a packet was received
volatile bool transmittedFlag = false;
// flag to indicate frequency must be changed
volatile bool fhssChangeFlag = false;
// the channel frequencies can be generated randomly or hard coded
// NOTE: The frequency list MUST be the same on both sides!
float channels[] = { 433.0, 433.4, 433.2, 433.6, 434.0, 433.8 };
int numberOfChannels = sizeof(channels) / sizeof(float);
// counter to keep track of how many frequency hops were performed
int hopsCompleted = 0;
// counter that increments with each sent packet
int packetCounter = 0;
// save transmission state between loops
int transmissionState = RADIOLIB_ERR_NONE;
// this is the packet that will be sent
String longPacket = "Let's create a really long packet to trigger \
lots of hop interrupts. A packet can be up to 255 bytes long. \
This packet is 222 bytes so using sf = 9, bw = 125, timeOnAir is \
1488ms. 1488ms / (9*4.10ms) = 40 hops. Counter: ";
// this function is called when a complete packet
// is transmitted 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
void setTxFlag(void) {
transmittedFlag = true;
}
// this function is called when FhssChangeChannel interrupt occurs
// (at the beginning of each transmission)
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
void setFHSSFlag(void) {
fhssChangeFlag = true;
}
void setup() {
Serial.begin(9600);
// begin radio on home channel
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin(channels[0]);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set hop period in symbols
// this will also enable FHSS
Serial.print(F("[SX1278] Setting hopping period ... "));
state = radio.setFHSSHoppingPeriod(9);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set the function to call when transmission is finished
radio.setDio0Action(setTxFlag, RISING);
// set the function to call when we need to change frequency
radio.setDio1Action(setFHSSFlag, RISING);
// start transmitting the first packet
Serial.print(F("[SX1278] Sending first packet ... "));
String packet = longPacket + packetCounter;
transmissionState = radio.startTransmit(packet);
}
void loop() {
// check if the transmission flag is set
if (transmittedFlag == true) {
// reset flag
transmittedFlag = false;
if (transmissionState == RADIOLIB_ERR_NONE) {
// packet was successfully sent
Serial.println(F("transmission finished!"));
} else {
Serial.print(F("failed, code "));
Serial.println(transmissionState);
}
// The channel is automatically reset to 0 upon completion
Serial.print(F("[SX1278] Radio is on channel: "));
Serial.println(radio.getFHSSChannel());
// print the number of hops it took
Serial.print(F("[SX1278] Hops completed: "));
Serial.println(hopsCompleted);
// reset the counter
hopsCompleted = 0;
// return to home channel before the next transaction
radio.setFrequency(channels[0]);
// wait a second before transmitting again
delay(1000);
// increment the packet counter
packetCounter++;
// send another packet
Serial.print(F("[SX1278] Sending another packet ... "));
String packet = longPacket + packetCounter;
transmissionState = radio.startTransmit(packet);
}
// check if we need to do another frequency hop
if (fhssChangeFlag == true) {
// we do, change it now
int state = radio.setFrequency(channels[radio.getFHSSChannel() % numberOfChannels]);
if (state != RADIOLIB_ERR_NONE) {
Serial.print(F("[SX1278] Failed to change frequency, code "));
Serial.println(state);
}
// increment the counter
hopsCompleted++;
// clear the FHSS interrupt
radio.clearFHSSInt();
// we're ready to do another hop, clear the flag
fhssChangeFlag = false;
}
}

View File

@@ -0,0 +1,135 @@
/*
RadioLib SX127x Transmit with Interrupts Example
This example transmits LoRa packets with one second delays
between them. Each packet contains up to 255 bytes
of data, in the form of:
- Arduino String
- null-terminated char array (C-string)
- arbitrary binary data (byte array)
Other modules from SX127x/RFM9x family can also be used.
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
#include <RadioLib.h>
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// or detect the pinout automatically using RadioBoards
// https://github.com/radiolib-org/RadioBoards
/*
#define RADIO_BOARD_AUTO
#include <RadioBoards.h>
Radio radio = new RadioModule();
*/
// save transmission state between loops
int transmissionState = RADIOLIB_ERR_NONE;
// flag to indicate that a packet was sent
volatile bool transmittedFlag = false;
// this function is called when a complete packet
// is transmitted 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
void setFlag(void) {
// we sent a packet, set the flag
transmittedFlag = true;
}
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin();
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true) { delay(10); }
}
// set the function that will be called
// when packet transmission is finished
radio.setPacketSentAction(setFlag);
// start transmitting the first packet
Serial.print(F("[SX1278] Sending first packet ... "));
// you can transmit C-string or Arduino string up to
// 255 characters long
transmissionState = radio.startTransmit("Hello World!");
// you can also transmit byte array up to 255 bytes long
/*
byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
0x89, 0xAB, 0xCD, 0xEF};
transmissionState = radio.startTransmit(byteArr, 8);
*/
}
// counter to keep track of transmitted packets
int count = 0;
void loop() {
// check if the previous transmission finished
if(transmittedFlag) {
// reset flag
transmittedFlag = false;
if (transmissionState == RADIOLIB_ERR_NONE) {
// packet was successfully sent
Serial.println(F("transmission finished!"));
// NOTE: when using interrupt-driven transmit method,
// it is not possible to automatically measure
// transmission data rate using getDataRate()
} else {
Serial.print(F("failed, code "));
Serial.println(transmissionState);
}
// clean up after transmission is finished
// this will ensure transmitter is disabled,
// RF switch is powered down etc.
radio.finishTransmit();
// wait a second before transmitting again
delay(1000);
// send another one
Serial.print(F("[SX1278] Sending another packet ... "));
// you can transmit C-string or Arduino string up to
// 255 characters long
String str = "Hello World! #" + String(count++);
transmissionState = radio.startTransmit(str);
// you can also transmit byte array up to 255 bytes long
/*
byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
0x89, 0xAB, 0xCD, 0xEF};
transmissionState = radio.startTransmit(byteArr, 8);
*/
}
}