-
Notifications
You must be signed in to change notification settings - Fork 17
Examples
Chandra Wijaya Sentosa edited this page Jan 12, 2022
·
1 revision
See more examples for SX126x, SX127x, and simple network implementation.
#include <SX126x.h>
#include <SX127x.h>
// #define SX126X
#define SX127X
#if defined(SX126X)
SX126x LoRa;
#elif defined(SX127X)
SX127x LoRa;
#endif
void setup() {
// Begin serial communication
Serial.begin(38400);
// Begin LoRa radio and set NSS, reset, IRQ, txen, and rxen pin with connected arduino pins
Serial.println("Begin LoRa radio");
#if defined(SX126X)
// Begin LoRa radio for SX126x and LLCC68
int8_t nssPin = 10, resetPin = 9, busyPin = 4, irqPin = 2, txenPin = 8, rxenPin = 7;
if (!LoRa.begin(nssPin, resetPin, busyPin, irqPin, txenPin, rxenPin)){
Serial.println("Something wrong, can't begin LoRa radio");
while(1);
}
// Configure TCXO used in RF module
Serial.println("Set RF module to use TCXO as clock reference");
LoRa.setDio3TcxoCtrl(SX126X_DIO3_OUTPUT_1_8, SX126X_TCXO_DELAY_10);
#elif defined(SX127X)
// Begin LoRa radio for SX126x and LLCC68
int8_t nssPin = 10, resetPin = 9, irqPin = 2, txenPin = 8, rxenPin = 7;
if (!LoRa.begin(nssPin, resetPin, irqPin, txenPin, rxenPin)){
Serial.println("Something wrong, can't begin LoRa radio");
while(1);
}
#endif
// Set frequency to 915 Mhz
Serial.println("Set frequency to 915 Mhz");
LoRa.setFrequency(915000000);
// Set RX gain. RX gain option are power saving gain or boosted gain
Serial.println("Set RX gain to power saving gain");
LoRa.setRxGain(LORA_RX_GAIN_POWER_SAVING); // Power saving gain
// Configure modulation parameter including spreading factor (SF), bandwidth (BW), and coding rate (CR)
Serial.println("Set modulation parameters:\n\tSpreading factor = 7\n\tBandwidth = 125 kHz\n\tCoding rate = 4/5");
uint8_t sf = 7; // Spreading factor: 7
uint32_t bw = 125000; // Bandwidth: 125 kHz
uint8_t cr = 5; // Coding rate: 4/5
LoRa.setLoRaModulation(sf, bw, cr);
// Configure packet parameter including header type, preamble length, payload length, and CRC type
Serial.println("Set packet parameters:\n\tExplicit header type\n\tPreamble length = 12\n\tPayload Length = 15\n\tCRC on");
uint8_t headerType = LORA_HEADER_EXPLICIT; // Explicit header mode
uint16_t preambleLength = 12; // Set preamble length to 12
uint8_t payloadLength = 15; // Initialize payloadLength to 15
bool crcType = true; // Set CRC enable
LoRa.setLoRaPacket(headerType, preambleLength, payloadLength, crcType);
// Set syncronize word for public network (0x3444)
Serial.println("Set syncronize word to 0x3444");
LoRa.setSyncWord(0x3444);
Serial.println("\n-- LORA RECEIVER --\n");
}
void loop() {
// Transmit message and counter
// write() method must be placed between beginPacket() and endPacket()
LoRa.beginPacket();
LoRa.write(message, nBytes);
LoRa.write(counter);
LoRa.endPacket();
// Print message and counter in serial
Serial.print(message);
Serial.print(" ");
Serial.println(counter++);
// Wait until modulation process for transmitting packet finish
LoRa.wait();
// Print transmit time
Serial.print("Transmit time: ");
Serial.print(LoRa.transmitTime());
Serial.println(" ms");
Serial.println();
// Don't load RF module with continous transmit
delay(5000);
}
#include <SX126x.h>
#include <SX127x.h>
// #define SX126X
#define SX127X
#if defined(SX126X)
SX126x LoRa;
#elif defined(SX127X)
SX127x LoRa;
#endif
void setup() {
// Begin serial communication
Serial.begin(38400);
// Begin LoRa radio and set NSS, reset, IRQ, txen, and rxen pin with connected arduino pins
Serial.println("Begin LoRa radio");
#if defined(SX126X)
// Begin LoRa radio for SX126x and LLCC68
int8_t nssPin = 10, resetPin = 9, busyPin = 4, irqPin = 2, txenPin = 8, rxenPin = 7;
if (!LoRa.begin(nssPin, resetPin, busyPin, irqPin, txenPin, rxenPin)){
Serial.println("Something wrong, can't begin LoRa radio");
while(1);
}
// Configure TCXO used in RF module
Serial.println("Set RF module to use TCXO as clock reference");
LoRa.setDio3TcxoCtrl(SX126X_DIO3_OUTPUT_1_8, SX126X_TCXO_DELAY_10);
#elif defined(SX127X)
// Begin LoRa radio for SX126x and LLCC68
int8_t nssPin = 10, resetPin = 9, irqPin = 2, txenPin = 8, rxenPin = 7;
if (!LoRa.begin(nssPin, resetPin, irqPin, txenPin, rxenPin)){
Serial.println("Something wrong, can't begin LoRa radio");
while(1);
}
#endif
// Set frequency to 915 Mhz
Serial.println("Set frequency to 915 Mhz");
LoRa.setFrequency(915000000);
// Set RX gain. RX gain option are power saving gain or boosted gain
Serial.println("Set TX power to +17 dBm");
LoRa.setTxPower(17); // TX power +17 dBm
// Configure modulation parameter including spreading factor (SF), bandwidth (BW), and coding rate (CR)
Serial.println("Set modulation parameters:\n\tSpreading factor = 7\n\tBandwidth = 125 kHz\n\tCoding rate = 4/5");
uint8_t sf = 7; // Spreading factor: 7
uint32_t bw = 125000; // Bandwidth: 125 kHz
uint8_t cr = 5; // Coding rate: 4/5
LoRa.setLoRaModulation(sf, bw, cr);
// Configure packet parameter including header type, preamble length, payload length, and CRC type
Serial.println("Set packet parameters:\n\tExplicit header type\n\tPreamble length = 12\n\tPayload Length = 15\n\tCRC on");
uint8_t headerType = LORA_HEADER_EXPLICIT; // Explicit header mode
uint16_t preambleLength = 12; // Set preamble length to 12
uint8_t payloadLength = 15; // Initialize payloadLength to 15
bool crcType = true; // Set CRC enable
LoRa.setLoRaPacket(headerType, preambleLength, payloadLength, crcType);
// Set syncronize word for public network (0x3444)
Serial.println("Set syncronize word to 0x3444");
LoRa.setSyncWord(0x3444);
Serial.println("\n-- LORA RECEIVER --\n");
}
void loop() {
// Request for receiving new LoRa packet
LoRa.request();
// Wait for incoming LoRa packet
LoRa.wait();
// Put received packet to message and counter variable
const uint8_t msgLen = LoRa.available() - 1;
char message[msgLen];
uint8_t counter;
uint8_t i=0;
while (LoRa.available() > 1){
message[i++] = LoRa.read();
}
counter = LoRa.read();
// Print received message and counter in serial
Serial.print(message);
Serial.print(" ");
Serial.println(counter);
// Print packet/signal status including package RSSI and SNR
Serial.print("Packet status: RSSI = ");
Serial.print(LoRa.packetRssi());
Serial.print(" dBm | SNR = ");
Serial.print(LoRa.snr());
Serial.println(" dB");
// Show received status in case CRC or header error occur
uint8_t status = LoRa.status();
if (status == SX126X_STATUS_CRC_ERR) Serial.println("CRC error");
else if (status == SX126X_STATUS_HEADER_ERR) Serial.println("Packet header error");
Serial.println();
}