From 98f60dd092e9b09afc5d3a719511e703bf3ec0f5 Mon Sep 17 00:00:00 2001 From: jgromes <jgromes@users.noreply.github.com> Date: Sun, 2 Jun 2019 15:03:21 +0200 Subject: [PATCH] [SX126x] Updated examples --- .../SX126x_Channel_Activity_Detection.ino | 23 +++++--- .../SX126x_FSK_Modem/SX126x_FSK_Modem.ino | 25 ++++++--- .../SX126x/SX126x_Receive/SX126x_Receive.ino | 20 ++++++- .../SX126x_Receive_Interrupt.ino | 20 ++++++- .../SX126x_Settings/SX126x_Settings.ino | 53 ++++++++++--------- .../SX126x_Transmit/SX126x_Transmit.ino | 20 ++++++- .../SX126x_Transmit_Interrupt.ino | 11 +++- 7 files changed, 126 insertions(+), 46 deletions(-) diff --git a/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino b/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino index b1f2611b..0750f181 100644 --- a/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino +++ b/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino @@ -1,20 +1,31 @@ /* RadioLib SX126x 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 + 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 SX126x family can also be used. + + For full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ */ // include the library #include <RadioLib.h> -// SX1262 module is in slot A on the shield -SX1262 lora = RadioShield.ModuleA; +// SX1262 has the following connections: +// NSS pin: 10 +// DIO1 pin: 2 +// DIO2 pin: 3 +// BUSY pin: 9 +SX1262 lora = new Module(10, 2, 3, 9); + +// or using RadioShield +// https://github.com/jgromes/RadioShield +//SX1262 lora = RadioShield.ModuleA; void setup() { Serial.begin(9600); diff --git a/examples/SX126x/SX126x_FSK_Modem/SX126x_FSK_Modem.ino b/examples/SX126x/SX126x_FSK_Modem/SX126x_FSK_Modem.ino index f961f5a6..61b6a1e8 100644 --- a/examples/SX126x/SX126x_FSK_Modem/SX126x_FSK_Modem.ino +++ b/examples/SX126x/SX126x_FSK_Modem/SX126x_FSK_Modem.ino @@ -2,19 +2,30 @@ RadioLib SX126x FSK Modem Example This example shows how to use FSK modem in SX126x 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 full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ */ // include the library #include <RadioLib.h> -// SX1262 module is in slot A on the shield -SX1262 fsk = RadioShield.ModuleA; +// SX1262 has the following connections: +// NSS pin: 10 +// DIO1 pin: 2 +// DIO2 pin: 3 +// BUSY pin: 9 +SX1262 fsk = new Module(10, 2, 3, 9); + +// or using RadioShield +// https://github.com/jgromes/RadioShield +//SX1262 fsk = RadioShield.ModuleA; void setup() { Serial.begin(9600); @@ -45,7 +56,7 @@ void setup() { // lora.begin() start LoRa mode (and disable FSK) // lora.beginFSK() start FSK mode (and disable LoRa) - // the following settings can also + // the following settings can also // be modified at run-time state = fsk.setFrequency(433.5); state = fsk.setBitRate(100.0); @@ -54,7 +65,7 @@ void setup() { state = fsk.setOutputPower(10.0); state = fsk.setCurrentLimit(100.0); state = fsk.setDataShaping(1.0); - uint8_t syncWord[] = {0x01, 0x23, 0x45, 0x67, + uint8_t syncWord[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; state = fsk.setSyncWord(syncWord, 8); if (state != ERR_NONE) { @@ -75,7 +86,7 @@ void setup() { void loop() { // FSK modem can use the same transmit/receive methods // as the LoRa modem, even their interrupt-driven versions - + // transmit FSK packet int state = fsk.transmit("Hello World!"); /* @@ -116,7 +127,7 @@ void loop() { // it can be enabled by setting node address, broadcast // address, or both // - // to transmit packet to a particular address, + // to transmit packet to a particular address, // use the following methods: // // fsk.transmit("Hello World!", address); diff --git a/examples/SX126x/SX126x_Receive/SX126x_Receive.ino b/examples/SX126x/SX126x_Receive/SX126x_Receive.ino index 8cbec6cc..1e582158 100644 --- a/examples/SX126x/SX126x_Receive/SX126x_Receive.ino +++ b/examples/SX126x/SX126x_Receive/SX126x_Receive.ino @@ -12,13 +12,24 @@ - preamble length Other modules from SX126x family can also be used. + + For full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ */ // include the library #include <RadioLib.h> -// SX1262 module is in slot A on the shield -SX1262 lora = RadioShield.ModuleA; +// SX1262 has the following connections: +// NSS pin: 10 +// DIO1 pin: 2 +// DIO2 pin: 3 +// BUSY pin: 9 +SX1262 lora = new Module(10, 2, 3, 9); + +// or using RadioShield +// https://github.com/jgromes/RadioShield +//SX1262 lora = RadioShield.ModuleA; void setup() { Serial.begin(9600); @@ -88,5 +99,10 @@ void loop() { // packet was received, but is malformed Serial.println(F("CRC error!")); + } else { + // some other error occurred + Serial.print(F("failed, code ")); + Serial.println(state); + } } diff --git a/examples/SX126x/SX126x_Receive_Interrupt/SX126x_Receive_Interrupt.ino b/examples/SX126x/SX126x_Receive_Interrupt/SX126x_Receive_Interrupt.ino index 107b2d38..3de2873e 100644 --- a/examples/SX126x/SX126x_Receive_Interrupt/SX126x_Receive_Interrupt.ino +++ b/examples/SX126x/SX126x_Receive_Interrupt/SX126x_Receive_Interrupt.ino @@ -13,13 +13,24 @@ - sync word Other modules from SX126x/RFM9x family can also be used. + + For full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ */ // include the library #include <RadioLib.h> -// SX1262 module is in slot A on the shield -SX1262 lora = RadioShield.ModuleA; +// SX1262 has the following connections: +// NSS pin: 10 +// DIO1 pin: 2 +// DIO2 pin: 3 +// BUSY pin: 9 +SX1262 lora = new Module(10, 2, 3, 9); + +// or using RadioShield +// https://github.com/jgromes/RadioShield +//SX1262 lora = RadioShield.ModuleA; void setup() { Serial.begin(9600); @@ -131,6 +142,11 @@ void loop() { // packet was received, but is malformed Serial.println(F("CRC error!")); + } else { + // some other error occurred + Serial.print(F("failed, code ")); + Serial.println(state); + } // we're ready to receive more packets, diff --git a/examples/SX126x/SX126x_Settings/SX126x_Settings.ino b/examples/SX126x/SX126x_Settings/SX126x_Settings.ino index d6daf6d7..52553045 100644 --- a/examples/SX126x/SX126x_Settings/SX126x_Settings.ino +++ b/examples/SX126x/SX126x_Settings/SX126x_Settings.ino @@ -14,28 +14,31 @@ - preamble length - TCXO voltage - Other modules from SX126x family can also be used. + Other modules from SX126x family can also be used. + + For full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ */ // include the library #include <RadioLib.h> -// SX1268 module is in slot A on the shield -SX1268 loraSX1268 = RadioShield.ModuleA; +// SX1262 has the following connections: +// NSS pin: 10 +// DIO1 pin: 2 +// DIO2 pin: 3 +// BUSY pin: 9 +SX1262 loraSX1262 = new Module(10, 2, 3, 9); -// if you're not using RadioShield, you can specify -// the connection yourself -// NSS pin: 6 -// DIO1 pin: 4 -// DIO2 pin: 5 -// BUSY pin: 7 -SX1262 loraSX1262 = new Module(6, 4, 5, 7); +// or using RadioShield +// https://github.com/jgromes/RadioShield +SX1268 loraSX1268 = RadioShield.ModuleB; void setup() { Serial.begin(9600); // initialize SX1268 with default settings - Serial.print(F("[SX1268] Initializing ... ")); + Serial.print(F("[SX1262] Initializing ... ")); // carrier frequency: 434.0 MHz // bandwidth: 125.0 kHz // spreading factor: 9 @@ -45,7 +48,7 @@ void setup() { // current limit: 60 mA // preamble length: 8 symbols // CRC: enabled - int state = loraSX1268.begin(); + int state = loraSX1262.begin(); if (state == ERR_NONE) { Serial.println(F("success!")); } else { @@ -58,7 +61,7 @@ void setup() { // non-default settings // this LoRa link will have high data rate, // but lower range - Serial.print(F("[SX1262] Initializing ... ")); + Serial.print(F("[SX1268] Initializing ... ")); // carrier frequency: 915.0 MHz // bandwidth: 500.0 kHz // spreading factor: 6 @@ -68,7 +71,7 @@ void setup() { // current limit: 50 mA // preamble length: 20 symbols // CRC: enabled - state = loraSX1262.begin(915.0, 500.0, 6, 5, 0x3444, 50, 20); + state = loraSX1268.begin(915.0, 500.0, 6, 5, 0x3444, 50, 20); if (state == ERR_NONE) { Serial.println(F("success!")); } else { @@ -81,64 +84,64 @@ void setup() { // and check if the configuration was changed successfully // set carrier frequency to 433.5 MHz - if (loraSX1268.setFrequency(433.5) == ERR_INVALID_FREQUENCY) { + if (loraSX1262.setFrequency(433.5) == ERR_INVALID_FREQUENCY) { Serial.println(F("Selected frequency is invalid for this module!")); while (true); } // set bandwidth to 250 kHz - if (loraSX1268.setBandwidth(250.0) == ERR_INVALID_BANDWIDTH) { + if (loraSX1262.setBandwidth(250.0) == ERR_INVALID_BANDWIDTH) { Serial.println(F("Selected bandwidth is invalid for this module!")); while (true); } // set spreading factor to 10 - if (loraSX1268.setSpreadingFactor(10) == ERR_INVALID_SPREADING_FACTOR) { + if (loraSX1262.setSpreadingFactor(10) == ERR_INVALID_SPREADING_FACTOR) { Serial.println(F("Selected spreading factor is invalid for this module!")); while (true); } // set coding rate to 6 - if (loraSX1268.setCodingRate(6) == ERR_INVALID_CODING_RATE) { + if (loraSX1262.setCodingRate(6) == ERR_INVALID_CODING_RATE) { Serial.println(F("Selected coding rate is invalid for this module!")); while (true); } // set LoRa sync word to 0x1234 - if (loraSX1268.setSyncWord(0x1234) != ERR_NONE) { + if (loraSX1262.setSyncWord(0x1234) != ERR_NONE) { Serial.println(F("Unable to set sync word!")); while (true); } // set output power to 10 dBm (accepted range is -17 - 22 dBm) - if (loraSX1268.setOutputPower(10) == ERR_INVALID_OUTPUT_POWER) { + if (loraSX1262.setOutputPower(10) == ERR_INVALID_OUTPUT_POWER) { Serial.println(F("Selected output power is invalid for this module!")); while (true); } // set over current protection limit to 80 mA (accepted range is 45 - 240 mA) // NOTE: set value to 0 to disable overcurrent protection - if (loraSX1268.setCurrentLimit(80) == ERR_INVALID_CURRENT_LIMIT) { + if (loraSX1262.setCurrentLimit(80) == ERR_INVALID_CURRENT_LIMIT) { Serial.println(F("Selected current limit is invalid for this module!")); while (true); } // set LoRa preamble length to 15 symbols (accepted range is 0 - 65535) - if (loraSX1268.setPreambleLength(15) == ERR_INVALID_PREAMBLE_LENGTH) { + if (loraSX1262.setPreambleLength(15) == ERR_INVALID_PREAMBLE_LENGTH) { Serial.println(F("Selected preamble length is invalid for this module!")); while (true); } // disable CRC - if (loraSX1268.setCRC(false) == ERR_INVALID_CRC_CONFIGURATION) { + if (loraSX1262.setCRC(false) == ERR_INVALID_CRC_CONFIGURATION) { Serial.println(F("Selected CRC is invalid for this module!")); while (true); } - // Some SX126x have TCXO (temperature compensated crystal + // Some SX126x modules have TCXO (temperature compensated crystal // oscillator). To configure TCXO reference voltage, // the following method can be used. - if (loraSX1268.setTCXO(2.4) == ERR_INVALID_TCXO_VOLTAGE) { + if (loraSX1262.setTCXO(2.4) == ERR_INVALID_TCXO_VOLTAGE) { Serial.println(F("Selected TCXO voltage is invalid for this module!")); while (true); } diff --git a/examples/SX126x/SX126x_Transmit/SX126x_Transmit.ino b/examples/SX126x/SX126x_Transmit/SX126x_Transmit.ino index 2b49efff..94333c17 100644 --- a/examples/SX126x/SX126x_Transmit/SX126x_Transmit.ino +++ b/examples/SX126x/SX126x_Transmit/SX126x_Transmit.ino @@ -8,13 +8,24 @@ - arbitrary binary data (byte array) Other modules from SX126x family can also be used. + + For full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ */ // include the library #include <RadioLib.h> -// SX1262 module is in slot A on the shield -SX1262 lora = RadioShield.ModuleA; +// SX1262 has the following connections: +// NSS pin: 10 +// DIO1 pin: 2 +// DIO2 pin: 3 +// BUSY pin: 9 +SX1262 lora = new Module(10, 2, 3, 9); + + // or using RadioShield + // https://github.com/jgromes/RadioShield + //SX1262 lora = RadioShield.ModuleA; void setup() { Serial.begin(9600); @@ -94,6 +105,11 @@ void loop() { // timeout occured 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 diff --git a/examples/SX126x/SX126x_Transmit_Interrupt/SX126x_Transmit_Interrupt.ino b/examples/SX126x/SX126x_Transmit_Interrupt/SX126x_Transmit_Interrupt.ino index 1c4e0508..04151894 100644 --- a/examples/SX126x/SX126x_Transmit_Interrupt/SX126x_Transmit_Interrupt.ino +++ b/examples/SX126x/SX126x_Transmit_Interrupt/SX126x_Transmit_Interrupt.ino @@ -9,13 +9,20 @@ - arbitrary binary data (byte array) Other modules from SX126x family can also be used. + + For full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ */ // include the library #include <RadioLib.h> -// SX1262 module is in slot A on the shield -SX1262 lora = RadioShield.ModuleA; +// SX1262 has the following connections: +// NSS pin: 10 +// DIO1 pin: 2 +// DIO2 pin: 3 +// BUSY pin: 9 +SX1262 lora = new Module(10, 2, 3, 9); void setup() { Serial.begin(9600);