APIs and example programs related to Unit C6L LoRa communication.
#include <RadioLib.h>
// SX1262: NSS, DIO1, NRST, BUSY
SX1262 radio = new Module(23, 7, RADIOLIB_NC, 19);
// 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() {
delay(1000);
Serial.begin(115200);
// initialize SX1262
Serial.print(F("[SX1262] Initializing ... "));
// frequency, bandwidth, spreading factor, coding rate, sync word, power, preamble length, TCXO reference voltage, useRegulatorLDO
int state = radio.begin(868.0, 125.0f, 12, 5, 0x34, 22, 20, 3.0, true);
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("[SX1262] Sending first packet ... "));
// you can transmit C-string or Arduino string up to 256 characters long
transmissionState = radio.startTransmit("Hello world from M5Stack!");
}
// 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("[SX1262] Sending another packet ... "));
// you can transmit C-string or Arduino string up to 256 characters long
String str = "Hello world from M5Stack! #" + String(count++);
transmissionState = radio.startTransmit(str);
}
}
#include <RadioLib.h>
// SX1262: NSS, DIO1, NRST, BUSY
SX1262 radio = new Module(23, 7, RADIOLIB_NC, 19);
// 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() {
delay(1000);
Serial.begin(115200);
// initialize SX1262
Serial.print(F("[SX1262] Initializing ... "));
// frequency, bandwidth, spreading factor, coding rate, sync word, power, preamble length, TCXO reference voltage, useRegulatorLDO
int state = radio.begin(868.0, 125.0f, 12, 5, 0x34, 22, 20, 3.0, true);
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("[SX1262] 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 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);
if (state == RADIOLIB_ERR_NONE) {
// packet was successfully received
Serial.println(F("[SX1262] Received packet!"));
// print data of the packet
Serial.print(F("[SX1262] Data:\t\t"));
Serial.println(str);
// print RSSI (Received Signal Strength Indicator)
Serial.print(F("[SX1262] RSSI:\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));
// print SNR (Signal-to-Noise Ratio)
Serial.print(F("[SX1262] SNR:\t\t"));
Serial.print(radio.getSNR());
Serial.println(F(" dB"));
// print frequency error
Serial.print(F("[SX1262] 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("CRC error!"));
} else {
// some other error occurred
Serial.print(F("failed, code "));
Serial.println(state);
}
}
}
Compile and upload the two sketches to two Unit C6L respectively. The transmitter will send LoRa signal, and the receiver will capture this signal. Their serial output is shown below:
The Unit C6L LoRa communication uses the RadioLib
library as the driver. For more related APIs, please refer to the following documentation: