Skip to content

Commit fb0d544

Browse files
committed
Restructure examples folder
1 parent 8a3ae6a commit fb0d544

File tree

15 files changed

+315
-213
lines changed

15 files changed

+315
-213
lines changed

examples/SX126x_LoRa_simple_gateway/SX126x_LoRa_simple_gateway.ino renamed to examples/Network/LoRa_simple_gateway/LoRa_simple_gateway.ino

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#include <SX126x.h>
2+
#include <SX127x.h>
23

4+
// #define SX126X
5+
#define SX127X
6+
7+
#if defined(SX126X)
38
SX126x LoRa;
9+
#elif defined(SX127X)
10+
SX127x LoRa;
11+
#endif
412

513
// gateway ID
614
uint8_t gatewayId = 0xCC;
@@ -21,27 +29,34 @@ void setup() {
2129
// Begin serial communication
2230
Serial.begin(38400);
2331

32+
#if defined(SX126X)
2433
// Begin LoRa radio and set NSS, reset, busy, IRQ, txen, and rxen pin with connected arduino pins
2534
Serial.println("Begin LoRa radio");
2635
int8_t nssPin = 10, resetPin = 9, busyPin = 4, irqPin = 2, txenPin = 8, rxenPin = 7;
2736
if (!LoRa.begin(nssPin, resetPin, busyPin, irqPin, txenPin, rxenPin)){
2837
Serial.println("Something wrong, can't begin LoRa radio");
2938
while(1);
3039
}
31-
3240
// Configure TCXO used in RF module
3341
Serial.println("Set RF module to use TCXO as clock reference");
34-
uint8_t dio3Voltage = SX126X_DIO3_OUTPUT_1_8;
35-
uint32_t tcxoDelay = SX126X_TCXO_DELAY_10;
36-
LoRa.setDio3TcxoCtrl(dio3Voltage, tcxoDelay);
37-
42+
LoRa.setDio3TcxoCtrl(SX126X_DIO3_OUTPUT_1_8, SX126X_TCXO_DELAY_10);
43+
#elif defined(SX127X)
44+
// Begin LoRa radio and set NSS, reset, IRQ, txen, and rxen pin with connected arduino pins
45+
Serial.println("Begin LoRa radio");
46+
int8_t nssPin = 10, resetPin = 9, irqPin = 2, txenPin = 8, rxenPin = 7;
47+
if (!LoRa.begin(nssPin, resetPin, irqPin, txenPin, rxenPin)){
48+
Serial.println("Something wrong, can't begin LoRa radio");
49+
while(1);
50+
}
51+
#endif
52+
3853
// Set frequency to 915 Mhz
3954
Serial.println("Set frequency to 915 Mhz");
4055
LoRa.setFrequency(915000000);
4156

42-
// Set RX gain
57+
// Set RX gain to boosted gain
4358
Serial.println("Set RX gain to boosted gain");
44-
LoRa.setRxGain(SX126X_RX_GAIN_BOOSTED);
59+
LoRa.setRxGain(LORA_RX_GAIN_BOOSTED);
4560

4661
// Configure modulation parameter including spreading factor (SF), bandwidth (BW), and coding rate (CR)
4762
Serial.println("Set modulation parameters:\n\tSpreading factor = 7\n\tBandwidth = 125 kHz\n\tCoding rate = 4/5");
@@ -52,30 +67,27 @@ void setup() {
5267

5368
// Configure packet parameter including header type, preamble length, payload length, and CRC type
5469
Serial.println("Set packet parameters:\n\tImplicit header type\n\tPreamble length = 12\n\tPayload Length = message length\n\tCRC on");
55-
uint8_t headerType = SX126X_HEADER_IMPLICIT;
70+
uint8_t headerType = LORA_HEADER_IMPLICIT;
5671
uint16_t preambleLength = 12;
5772
uint8_t payloadLength = messageLen;
5873
bool crcType = true;
5974
LoRa.setLoRaPacket(headerType, preambleLength, payloadLength, crcType);
6075

61-
// Set syncronize word for private network (0x1424)
62-
Serial.println("Set syncronize word to 0x1424");
63-
LoRa.setSyncWord(0x1424);
76+
// Set syncronize word for public network (0x3444)
77+
Serial.println("Set syncronize word to 0x3444");
78+
LoRa.setSyncWord(0x3444);
6479

6580
Serial.print("\nGateway ID : 0x");
6681
if (gatewayId < 0x10) Serial.print("0");
6782
Serial.println(gatewayId, HEX);
68-
Serial.println("-- LoRa Gateway --\n");
83+
Serial.println("-- LORA GATEWAY --\n");
6984

7085
}
7186

7287
void loop() {
7388

74-
// Set RF module to listen mode with 30 ms RX mode and 10 ms sleep
75-
// Some LoRa packet will not be received if sleep period too long or preamble length too short
76-
uint32_t rxPeriod = 30;
77-
uint32_t sleepPeriod = 10;
78-
LoRa.listen(rxPeriod, sleepPeriod);
89+
// Set RF module to receive mode
90+
LoRa.request();
7991

8092
// Wait incoming signal and demodulation process for receiving packet finish
8193
LoRa.wait();
@@ -113,9 +125,9 @@ void loop() {
113125
Serial.println(" dB");
114126

115127
// Show received status in case CRC or header error occur
116-
uint8_t Status = LoRa.status();
117-
if (Status == SX126X_STATUS_CRC_ERR) Serial.println("CRC error");
118-
if (Status == SX126X_STATUS_HEADER_ERR) Serial.println("Packet header error");
128+
uint8_t status = LoRa.status();
129+
if (status == LORA_STATUS_CRC_ERR) Serial.println("CRC error");
130+
if (status == LORA_STATUS_HEADER_ERR) Serial.println("Packet header error");
119131
Serial.println();
120132

121133
}

examples/SX126x_LoRa_simple_node/SX126x_LoRa_simple_node.ino renamed to examples/Network/LoRa_simple_node/LoRa_simple_node.ino

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#include <SX126x.h>
2+
#include <SX127x.h>
23

4+
// #define SX126X
5+
#define SX127X
6+
7+
#if defined(SX126X)
38
SX126x LoRa;
9+
#elif defined(SX127X)
10+
SX127x LoRa;
11+
#endif
412

513
// gateway ID and node ID
614
uint8_t gatewayId = 0xCC;
@@ -22,27 +30,34 @@ void setup() {
2230
// Begin serial communication
2331
Serial.begin(38400);
2432

33+
#if defined(SX126X)
2534
// Begin LoRa radio and set NSS, reset, busy, IRQ, txen, and rxen pin with connected arduino pins
2635
Serial.println("Begin LoRa radio");
2736
int8_t nssPin = 10, resetPin = 9, busyPin = 4, irqPin = 2, txenPin = 8, rxenPin = 7;
2837
if (!LoRa.begin(nssPin, resetPin, busyPin, irqPin, txenPin, rxenPin)){
2938
Serial.println("Something wrong, can't begin LoRa radio");
3039
while(1);
3140
}
32-
3341
// Configure TCXO used in RF module
3442
Serial.println("Set RF module to use TCXO as clock reference");
35-
uint8_t dio3Voltage = SX126X_DIO3_OUTPUT_1_8;
36-
uint32_t tcxoDelay = SX126X_TCXO_DELAY_10;
37-
LoRa.setDio3TcxoCtrl(dio3Voltage, tcxoDelay);
38-
43+
LoRa.setDio3TcxoCtrl(SX126X_DIO3_OUTPUT_1_8, SX126X_TCXO_DELAY_10);
44+
#elif defined(SX127X)
45+
// Begin LoRa radio and set NSS, reset, IRQ, txen, and rxen pin with connected arduino pins
46+
Serial.println("Begin LoRa radio");
47+
int8_t nssPin = 10, resetPin = 9, irqPin = 2, txenPin = 8, rxenPin = 7;
48+
if (!LoRa.begin(nssPin, resetPin, irqPin, txenPin, rxenPin)){
49+
Serial.println("Something wrong, can't begin LoRa radio");
50+
while(1);
51+
}
52+
#endif
53+
3954
// Set frequency to 915 Mhz
4055
Serial.println("Set frequency to 915 Mhz");
4156
LoRa.setFrequency(915000000);
4257

43-
// Set TX power
44-
Serial.println("Set TX power to +22 dBm");
45-
LoRa.setTxPower(SX126X_TX_POWER_SX1262_22);
58+
// Set TX power to 17 dBm
59+
Serial.println("Set TX power to +17 dBm");
60+
LoRa.setTxPower(17);
4661

4762
// Configure modulation parameter including spreading factor (SF), bandwidth (BW), and coding rate (CR)
4863
Serial.println("Set modulation parameters:\n\tSpreading factor = 7\n\tBandwidth = 125 kHz\n\tCoding rate = 4/5");
@@ -53,17 +68,17 @@ void setup() {
5368

5469
// Configure packet parameter including header type, preamble length, payload length, and CRC type
5570
Serial.println("Set packet parameters:\n\tImplicit header type\n\tPreamble length = 12\n\tPayload Length = message length\n\tCRC on");
56-
uint8_t headerType = SX126X_HEADER_IMPLICIT;
71+
uint8_t headerType = LORA_HEADER_IMPLICIT;
5772
uint16_t preambleLength = 12;
5873
uint8_t payloadLength = messageLen;
5974
bool crcType = true;
6075
LoRa.setLoRaPacket(headerType, preambleLength, payloadLength, crcType);
6176

62-
// Set syncronize word for private network (0x1424)
63-
Serial.println("Set syncronize word to 0x1424");
64-
LoRa.setSyncWord(0x1424);
77+
// Set syncronize word for private network (0x3444)
78+
Serial.println("Set syncronize word to 0x3444");
79+
LoRa.setSyncWord(0x3444);
6580

66-
Serial.println("\n-- LoRa Node --\n");
81+
Serial.println("\n-- LORA NODE --\n");
6782

6883
// Assign gateway Id and node Id to message object
6984
message.gatewayId = gatewayId;
@@ -80,11 +95,9 @@ void loop() {
8095
message.messageId++;
8196

8297
// Transmit message object
83-
// Set transmit timeout to 250 ms
84-
uint32_t timeout = 250;
8598
LoRa.beginPacket();
8699
LoRa.put(message);
87-
LoRa.endPacket(timeout);
100+
LoRa.endPacket();
88101

89102
// Print message in serial
90103
Serial.print("Gateway ID : 0x");
@@ -109,9 +122,6 @@ void loop() {
109122
Serial.println(" ms");
110123
Serial.println();
111124

112-
// Print status timeout when transmit process terminated due to timeout
113-
if (LoRa.status() == SX126X_STATUS_TX_TIMEOUT) Serial.println("Transmit timeout");
114-
115125
// Put RF module to sleep in a few seconds
116126
LoRa.sleep();
117127
delay(5000);

examples/SX126x_LoRa_receiver/SX126x_LoRa_receiver.ino renamed to examples/SX126x/SX126x_LoRa_receiver/SX126x_LoRa_receiver.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void setup() {
3333
Serial.println("Set frequency to 915 Mhz");
3434
LoRa.setFrequency(915000000);
3535

36-
// Set RX gain. RX gain option are power saving gain or boosted gain
36+
// Set RX gain. RX gain option are power saving gain or boosted gain
3737
Serial.println("Set RX gain to power saving gain");
3838
LoRa.setRxGain(SX126X_RX_GAIN_POWER_SAVING); // Power saving gain
3939

@@ -59,7 +59,7 @@ void setup() {
5959
Serial.println("Set syncronize word to 0x3444");
6060
LoRa.setSyncWord(0x3444);
6161

62-
Serial.println("\n-- LoRa RECEIVER --\n");
62+
Serial.println("\n-- LORA RECEIVER --\n");
6363

6464
}
6565

@@ -95,8 +95,9 @@ void loop() {
9595
Serial.println(" dB");
9696

9797
// Show received status in case CRC or header error occur
98-
if (LoRa.status() == SX126X_STATUS_CRC_ERR) Serial.println("CRC error");
99-
if (LoRa.status() == SX126X_STATUS_HEADER_ERR) Serial.println("Packet header error");
98+
uint8_t status = LoRa.status();
99+
if (status == SX126X_STATUS_CRC_ERR) Serial.println("CRC error");
100+
else if (status == SX126X_STATUS_HEADER_ERR) Serial.println("Packet header error");
100101
Serial.println();
101102

102103
}

examples/SX126x_LoRa_receiver_callback/SX126x_LoRa_receiver_callback.ino renamed to examples/SX126x/SX126x_LoRa_receiver_continuous/SX126x_LoRa_receiver_continuous.ino

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
SX126x LoRa;
44

5-
// receive data length and pointer
6-
const uint8_t packetLength = 15;
7-
uint8_t packetData[packetLength];
8-
volatile bool flag = false;
9-
105
void setup() {
116

127
// Begin serial communication
@@ -20,7 +15,7 @@ void setup() {
2015
while(1);
2116
}
2217

23-
// Configure TCXO used in RF module
18+
// Configure TCXO or XTAL used in RF module
2419
Serial.println("Set RF module to use TCXO as clock reference");
2520
uint8_t dio3Voltage = SX126X_DIO3_OUTPUT_1_8;
2621
uint32_t tcxoDelay = SX126X_TCXO_DELAY_10;
@@ -30,9 +25,9 @@ void setup() {
3025
Serial.println("Set frequency to 915 Mhz");
3126
LoRa.setFrequency(915000000);
3227

33-
// Set RX gain
28+
// Set RX gain to boosted gain
3429
Serial.println("Set RX gain to power saving gain");
35-
LoRa.setRxGain(SX126X_RX_GAIN_POWER_SAVING);
30+
LoRa.setRxGain(SX126X_RX_GAIN_BOOSTED);
3631

3732
// Configure modulation parameter including spreading factor (SF), bandwidth (BW), and coding rate (CR)
3833
Serial.println("Set modulation parameters:\n\tSpreading factor = 7\n\tBandwidth = 125 kHz\n\tCoding rate = 4/5");
@@ -53,42 +48,46 @@ void setup() {
5348
Serial.println("Set syncronize word to 0x3444");
5449
LoRa.setSyncWord(0x3444);
5550

56-
Serial.println("\n-- LoRa RECEIVER CALLBACK --\n");
57-
58-
// Register callback function to be called every RX done
59-
LoRa.onReceive(getReceiveData);
60-
61-
// Begin request LoRa packet in continuous mode
51+
Serial.println("\n-- LORA RECEIVER CONTINUOUS --\n");
52+
53+
// Request for receiving new LoRa packet in RX continuous mode
6254
LoRa.request(SX126X_RX_CONTINUOUS);
55+
6356
}
6457

6558
void loop() {
6659

67-
if (flag) {
68-
// Print received package
69-
Serial.write(packetData, packetLength - 1);
60+
// Check for incoming LoRa packet
61+
const uint8_t msgLen = LoRa.available();
62+
if (msgLen) {
63+
64+
// Put received packet to message and counter variable
65+
char message[msgLen-1];
66+
uint8_t counter;
67+
uint8_t i=0;
68+
while (LoRa.available() > 1){
69+
message[i++] = LoRa.read();
70+
}
71+
counter = LoRa.read();
72+
73+
// Print received message and counter in serial
74+
Serial.print(message);
7075
Serial.print(" ");
71-
Serial.println(packetData[packetLength - 1]);
76+
Serial.println(counter);
7277

7378
// Print packet/signal status including package RSSI and SNR
7479
Serial.print("Packet status: RSSI = ");
7580
Serial.print(LoRa.packetRssi());
7681
Serial.print(" dBm | SNR = ");
7782
Serial.print(LoRa.snr());
7883
Serial.println(" dB");
84+
85+
// Show received status in case CRC or header error occur
86+
uint8_t status = LoRa.status();
87+
if (status == SX126X_STATUS_CRC_ERR) Serial.println("CRC error");
88+
else if (status == SX126X_STATUS_HEADER_ERR) Serial.println("Packet header error");
7989
Serial.println();
8090

81-
// Reset flag
82-
flag = false;
8391
}
84-
}
8592

86-
void getReceiveData() {
87-
88-
// set flag
89-
flag = true;
90-
// Store received data
91-
for (uint8_t i=0; i<packetLength; i++) {
92-
packetData[i] = LoRa.read();
93-
}
9493
}

0 commit comments

Comments
 (0)