SX127x - Added examples for CAD and LoRa settings

This commit is contained in:
Jan Gromeš 2018-07-11 18:16:47 +02:00
parent 3b51c3b661
commit 79fc6cd9f5
2 changed files with 209 additions and 0 deletions

View file

@ -0,0 +1,63 @@
/*
* KiteLib SX127x Channel Activity Detection Example
*
* This example scans the current LoRa channel using SX1278 LoRa radio module
* 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 family can also be used.
* SX1272 lora = Kite.ModuleA;
* SX1273 lora = Kite.ModuleA;
* SX1276 lora = Kite.ModuleA;
* SX1277 lora = Kite.ModuleA;
* SX1279 lora = Kite.ModuleA;
*/
// include the library
#include <KiteLib.h>
// SX1278 module is in slot A on the shield
SX1278 lora = Kite.ModuleA;
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
// carrier frequency: 434.0 MHz
// bandwidth: 125.0 kHz
// spreading factor: 9
// coding rate: 7
// sync word: 0x12
// output power: 17 dBm
// node address in EEPROM starts at: 0
byte state = lora.begin();
if(state == ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code 0x"));
Serial.println(state, HEX);
while(true);
}
}
void loop() {
Serial.print(F("Scanning channel for LoRa preamble ... "));
// start scanning current channel
uint8_t state = lora.scanChannel();
if(state == PREAMBLE_DETECTED) {
// LoRa preamble was detected
Serial.println(" detected preamble!");
} else if(state == CHANNEL_FREE) {
// no preamble was detected, channel is free
Serial.println(" channel is free!");
}
// wait 100 ms before new scan
delay(100);
}

View file

@ -0,0 +1,146 @@
/*
* KiteLib SX127x Settings Example
*
* This example shows how to change all the properties of LoRa transmission.
* KiteLib 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
*/
// include the library
#include <KiteLib.h>
// SX1278 module is in slot A on the shield
SX1278 loraSX1278 = Kite.ModuleA;
// create another instance of LoRa class using SX1272 module and user-specified pinout
// NSS pin: 6
// DIO0 pin: 4
// DIO1 pin: 5
SX1272 loraSX1272 = new Module(6, 4, 5);
// create third instance of LoRa class using SX1276 module and user-specified pinout
// we ran out of Uno digital pins, so here we use analog ones
// NSS pin: A0
// DIO0 pin: A1
// DIO1 pin: A2
SX1276 loraSX1276 = new Module(A0, A1, A2);
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
// carrier frequency: 434.0 MHz
// bandwidth: 125.0 kHz
// spreading factor: 9
// coding rate: 7
// sync word: 0x12
// output power: 17 dBm
// node address in EEPROM starts at: 0
byte state = loraSX1278.begin();
if(state == ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code 0x"));
Serial.println(state, HEX);
while(true);
}
// initialize the second LoRa instance with non-default settings
// this LoRa link will have maximum range, but very low data rate
Serial.print(F("[SX1276] Initializing ... "));
// carrier frequency: 434.0 MHz
// bandwidth: 7.8 kHz
// spreading factor: 12
// coding rate: 8
// sync word: 0x13
// output power: 17 dBm
// node address in EEPROM starts at: 0
state = loraSX1276.begin(434.0, 7.8, 12, 8, 0x13);
if(state == ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code 0x"));
Serial.println(state, HEX);
while(true);
}
// initialize the third 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!
// it can be set using the length variable of your Packet instance
// Packet::length = x;
// where x is the total packet length including both addresses
Serial.print(F("[SX1272] Initializing ... "));
// carrier frequency: 915.0 MHz
// bandwidth: 500.0 kHz
// spreading factor: 6
// coding rate: 5
// sync word: 0x14
// output power: 2 dBm
// node address in EEPROM starts at: 0
state = loraSX1272.begin(915.0, 500.0, 6, 5, 0x14, 2);
if(state == ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code 0x"));
Serial.println(state, HEX);
while(true);
}
// you can also change the settings at runtime
// different modules accept different parameters
// see https://github.com/jgromes/LoRaLib/wiki/Supported-LoRa-modules
// you can check if the setting was changed successfully
// set bandwidth to 250 kHz
if(loraSX1278.setBandwidth(250.0) == ERR_INVALID_BANDWIDTH) {
Serial.println("Selected bandwidth is invalid for this module!");
while(true);
}
// set spreading factor to 10
if(loraSX1278.setSpreadingFactor(10) == ERR_INVALID_SPREADING_FACTOR) {
Serial.println("Selected spreading factor is invalid for this module!");
while(true);
}
// set coding rate to 6
if(loraSX1278.setCodingRate(6) == ERR_INVALID_CODING_RATE) {
Serial.println("Selected coding rate is invalid for this module!");
while(true);
}
// set carrier frequency to 433.5 MHz
if(loraSX1278.setFrequency(433.5) == ERR_INVALID_FREQUENCY) {
Serial.println("Selected frequency is invalid for this module!");
while(true);
}
// set LoRa sync word to 0x14
// NOTE: value 0x34 is reserved for LoRaWAN networks and should not be used
if(loraSX1278.setSyncWord(0x14) != ERR_NONE) {
Serial.println("Unable to set sync word!");
while(true);
}
// set output power to 10 dBm (accepted range is 2 - 17 dBm)
if(loraSX1278.setOutputPower(10) == ERR_INVALID_OUTPUT_POWER) {
Serial.println("Selected output power is invalid for this module!");
while(true);
}
Serial.println("All settings succesfully changed!");
}
void loop() {
// nothing here
}