Skip to content

Commit 21ace81

Browse files
committed
Some tweaks to new examples
1 parent b894e71 commit 21ace81

File tree

5 files changed

+97
-71
lines changed

5 files changed

+97
-71
lines changed
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
/*
22
LoRa register dump
3-
context: Arduino
43
4+
This examples shows how to inspect and output the LoRa radio's
5+
registers on the Serial interface
56
*/
67
#include <SPI.h> // include libraries
78
#include <LoRa.h>
89

910
void setup() {
10-
while (!Serial);
1111
Serial.begin(9600); // initialize serial
12-
Serial.println("LoRa Receiver");
13-
LoRa.setPins(7, 6, 1); // set CS, reset, IRQ pin
14-
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz
12+
while (!Serial);
13+
14+
Serial.println("LoRa Dump Registers");
15+
16+
// override the default CS, reset, and IRQ pins (optional)
17+
// LoRa.setPins(7, 6, 1); // set CS, reset, IRQ pin
18+
19+
if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
1520
Serial.println("LoRa init failed. Check your connections.");
1621
while (true); // if failed, do nothing
1722
}
18-
LoRa.sleep();
19-
LoRa.dumpRegisters(Serial);
23+
24+
LoRa.dumpRegisters(Serial);
2025
}
2126

2227

2328
void loop() {
24-
2529
}
2630

examples/LoRaDuplex/LoRaDuplex.ino

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,54 @@
66
with 0xFF as the broadcast address.
77
88
Uses readString() from Stream class to read payload. The Stream class'
9-
timeout may affect other functuons, like the radio's callback. For an
9+
timeout may affect other functuons, like the radio's callback. For an
1010
1111
created 28 April 2017
1212
by Tom Igoe
1313
*/
1414
#include <SPI.h> // include libraries
1515
#include <LoRa.h>
16+
1617
const int csPin = 7; // LoRa radio chip select
1718
const int resetPin = 6; // LoRa radio reset
1819
const int irqPin = 1; // change for your board; must be a hardware interrupt pin
1920

2021
String outgoing; // outgoing message
22+
2123
byte msgCount = 0; // count of outgoing messages
2224
byte localAddress = 0xBB; // address of this device
2325
byte destination = 0xFF; // destination to send to
2426
long lastSendTime = 0; // last send time
2527
int interval = 2000; // interval between sends
2628

2729
void setup() {
28-
while (!Serial);
2930
Serial.begin(9600); // initialize serial
31+
while (!Serial);
32+
3033
Serial.println("LoRa Duplex");
34+
35+
// override the default CS, reset, and IRQ pins (optional)
3136
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
3237

33-
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz
38+
if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
3439
Serial.println("LoRa init failed. Check your connections.");
3540
while (true); // if failed, do nothing
3641
}
3742

38-
LoRa.setTimeout(10); // set Stream timeout of 10ms
39-
Serial.println("LoRa init succeeded.");
43+
Serial.println("LoRa init succeeded.");
4044
}
4145

4246
void loop() {
43-
if (millis() - lastSendTime > interval) {
44-
String message = "HeLoRa World!"; // send a message
45-
sendMessage(message);
46-
Serial.println("Sending " + message);
47-
lastSendTime = millis(); // timestamp the message
48-
interval = random(2000) + 1000; // 2-3 seconds
49-
}
47+
if (millis() - lastSendTime > interval) {
48+
String message = "HeLoRa World!"; // send a message
49+
sendMessage(message);
50+
Serial.println("Sending " + message);
51+
lastSendTime = millis(); // timestamp the message
52+
interval = random(2000) + 1000; // 2-3 seconds
53+
}
5054

5155
// parse for a packet, and call onReceive with the result:
52-
onReceive(LoRa.parsePacket());
53-
56+
onReceive(LoRa.parsePacket());
5457
}
5558

5659
void sendMessage(String outgoing) {
@@ -64,7 +67,6 @@ void sendMessage(String outgoing) {
6467
msgCount++; // increment message ID
6568
}
6669

67-
6870
void onReceive(int packetSize) {
6971
if (packetSize == 0) return; // if there's no packet, return
7072

@@ -73,10 +75,13 @@ void onReceive(int packetSize) {
7375
byte sender = LoRa.read(); // sender address
7476
byte incomingMsgId = LoRa.read(); // incoming msg ID
7577
byte incomingLength = LoRa.read(); // incoming msg length
78+
7679
String incoming = "";
80+
7781
while (LoRa.available()) {
78-
incoming += (char)LoRa.read();
82+
incoming += (char)LoRa.read();
7983
}
84+
8085
if (incomingLength != incoming.length()) { // check length for error
8186
Serial.println("error: message length does not match length");
8287
return; // skip rest of function
@@ -89,13 +94,13 @@ void onReceive(int packetSize) {
8994
}
9095

9196
// if message is for this device, or broadcast, print details:
92-
Serial.println("Received from:" + String(sender, HEX));
93-
Serial.println("Sent to:" + String(recipient, HEX));
94-
Serial.println("Message ID:" + String(incomingMsgId));
95-
Serial.println("Message length:" + String(incomingLength));
96-
Serial.println("Message:" + incoming);
97-
Serial.println("RSSI:" + String(LoRa.packetRssi()));
98-
Serial.println("Snr:" + String(LoRa.packetSnr()));
97+
Serial.println("Received from: 0x" + String(sender, HEX));
98+
Serial.println("Sent to: 0x" + String(recipient, HEX));
99+
Serial.println("Message ID: " + String(incomingMsgId));
100+
Serial.println("Message length: " + String(incomingLength));
101+
Serial.println("Message: " + incoming);
102+
Serial.println("RSSI: " + String(LoRa.packetRssi()));
103+
Serial.println("Snr: " + String(LoRa.packetSnr()));
99104
Serial.println();
100105
}
101106

examples/LoRaDuplexCallback/LoRaDuplexCallback.ino

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
#include <SPI.h> // include libraries
1616
#include <LoRa.h>
17+
1718
const int csPin = 7; // LoRa radio chip select
1819
const int resetPin = 6; // LoRa radio reset
1920
const int irqPin = 1; // change for your board; must be a hardware interrupt pin
@@ -26,12 +27,15 @@ long lastSendTime = 0; // last send time
2627
int interval = 2000; // interval between sends
2728

2829
void setup() {
29-
while (!Serial);
3030
Serial.begin(9600); // initialize serial
31+
while (!Serial);
32+
3133
Serial.println("LoRa Duplex with callback");
34+
35+
// override the default CS, reset, and IRQ pins (optional)
3236
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
3337

34-
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz
38+
if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
3539
Serial.println("LoRa init failed. Check your connections.");
3640
while (true); // if failed, do nothing
3741
}
@@ -42,14 +46,14 @@ void setup() {
4246
}
4347

4448
void loop() {
45-
if (millis() - lastSendTime > interval) {
46-
String message = "HeLoRa World!"; // send a message
47-
sendMessage(message);
48-
Serial.println("Sending " + message);
49-
lastSendTime = millis(); // timestamp the message
50-
interval = random(2000) + 1000; // 2-3 seconds
51-
LoRa.receive(); // go back into receive mode
52-
}
49+
if (millis() - lastSendTime > interval) {
50+
String message = "HeLoRa World!"; // send a message
51+
sendMessage(message);
52+
Serial.println("Sending " + message);
53+
lastSendTime = millis(); // timestamp the message
54+
interval = random(2000) + 1000; // 2-3 seconds
55+
LoRa.receive(); // go back into receive mode
56+
}
5357
}
5458

5559
void sendMessage(String outgoing) {
@@ -63,7 +67,6 @@ void sendMessage(String outgoing) {
6367
msgCount++; // increment message ID
6468
}
6569

66-
6770
void onReceive(int packetSize) {
6871
if (packetSize == 0) return; // if there's no packet, return
6972

@@ -72,10 +75,13 @@ void onReceive(int packetSize) {
7275
byte sender = LoRa.read(); // sender address
7376
byte incomingMsgId = LoRa.read(); // incoming msg ID
7477
byte incomingLength = LoRa.read(); // incoming msg length
78+
7579
String incoming = ""; // payload of packet
80+
7681
while (LoRa.available()) { // can't use readString() in callback, so
7782
incoming += (char)LoRa.read(); // add bytes one by one
7883
}
84+
7985
if (incomingLength != incoming.length()) { // check length for error
8086
Serial.println("error: message length does not match length");
8187
return; // skip rest of function
@@ -88,13 +94,13 @@ void onReceive(int packetSize) {
8894
}
8995

9096
// if message is for this device, or broadcast, print details:
91-
Serial.println("Received from:" + String(sender, HEX));
92-
Serial.println("Sent to:" + String(recipient, HEX));
93-
Serial.println("Message ID:" + String(incomingMsgId));
94-
Serial.println("Message length:" + String(incomingLength));
95-
Serial.println("Message:" + incoming);
96-
Serial.println("RSSI:" + String(LoRa.packetRssi()));
97-
Serial.println("Snr:" + String(LoRa.packetSnr()));
97+
Serial.println("Received from: 0x" + String(sender, HEX));
98+
Serial.println("Sent to: 0x" + String(recipient, HEX));
99+
Serial.println("Message ID: " + String(incomingMsgId));
100+
Serial.println("Message length: " + String(incomingLength));
101+
Serial.println("Message: " + incoming);
102+
Serial.println("RSSI: " + String(LoRa.packetRssi()));
103+
Serial.println("Snr: " + String(LoRa.packetSnr()));
98104
Serial.println();
99105
}
100106

examples/LoRaSetSpread/LoRaSetSpread.ino

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
77
Spreading factor affects how far apart the radio's transmissions
88
are, across the available bandwidth. Radios with different spreading
9-
factors will not receive each other's transmissions. This is one way you
9+
factors will not receive each other's transmissions. This is one way you
1010
can filter out radios you want to ignore, without making an addressing scheme.
1111
1212
Spreading factor affects reliability of transmission at high rates, however,
1313
so avoid a hugh spreading factor when you're sending continually.
14-
15-
See the Semtech datasheete, http://www.semtech.com/images/datasheet/sx1276.pdf
14+
15+
See the Semtech datasheet, http://www.semtech.com/images/datasheet/sx1276.pdf
1616
for more on Spreading Factor.
1717
1818
created 28 April 2017
1919
by Tom Igoe
2020
*/
2121
#include <SPI.h> // include libraries
2222
#include <LoRa.h>
23+
2324
const int csPin = 7; // LoRa radio chip select
2425
const int resetPin = 6; // LoRa radio reset
2526
const int irqPin = 1; // change for your board; must be a hardware interrupt pin
@@ -29,16 +30,20 @@ int interval = 2000; // interval between sends
2930
long lastSendTime = 0; // time of last packet send
3031

3132
void setup() {
32-
while (!Serial);
3333
Serial.begin(9600); // initialize serial
34-
Serial.println("LoRa Duplex");
35-
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
34+
while (!Serial);
35+
36+
Serial.println("LoRa Duplex - Set spreading factor");
3637

37-
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz
38+
// override the default CS, reset, and IRQ pins (optional)
39+
LoRa.setPins(csPin, resetPin, irqPin); // set CS, reset, IRQ pin
40+
41+
if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
3842
Serial.println("LoRa init failed. Check your connections.");
3943
while (true); // if failed, do nothing
4044
}
41-
LoRa.setSpreadingFactor(8); // ranges from 6-12,default 7 see API docs
45+
46+
LoRa.setSpreadingFactor(8); // ranges from 6-12,default 7 see API docs
4247
Serial.println("LoRa init succeeded.");
4348
}
4449

@@ -64,18 +69,19 @@ void sendMessage(String outgoing) {
6469
msgCount++; // increment message ID
6570
}
6671

67-
6872
void onReceive(int packetSize) {
6973
if (packetSize == 0) return; // if there's no packet, return
7074

7175
// read packet header bytes:
7276
String incoming = "";
77+
7378
while (LoRa.available()) {
7479
incoming += (char)LoRa.read();
7580
}
76-
Serial.println("Message:" + incoming);
77-
Serial.println("RSSI:" + String(LoRa.packetRssi()));
78-
Serial.println("Snr:" + String(LoRa.packetSnr()));
81+
82+
Serial.println("Message: " + incoming);
83+
Serial.println("RSSI: " + String(LoRa.packetRssi()));
84+
Serial.println("Snr: " + String(LoRa.packetSnr()));
7985
Serial.println();
8086
}
8187

examples/LoRaSetSyncWord/LoRaSetSyncWord.ino

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
Sends a message every half second, and polls continually
55
for new incoming messages. Sets the LoRa radio's Sync Word.
66
7-
Spreading factor is basically the radio's network ID. Radios with different
8-
Sync Words will not receive each other's transmissions. This is one way you
7+
Spreading factor is basically the radio's network ID. Radios with different
8+
Sync Words will not receive each other's transmissions. This is one way you
99
can filter out radios you want to ignore, without making an addressing scheme.
1010
1111
See the Semtech datasheet, http://www.semtech.com/images/datasheet/sx1276.pdf
@@ -25,16 +25,20 @@ int interval = 2000; // interval between sends
2525
long lastSendTime = 0; // time of last packet send
2626

2727
void setup() {
28-
while (!Serial);
2928
Serial.begin(9600); // initialize serial
30-
Serial.println("LoRa Duplex");
29+
while (!Serial);
30+
31+
Serial.println("LoRa Duplex - Set sync word");
32+
33+
// override the default CS, reset, and IRQ pins (optional)
3134
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
3235

33-
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz
36+
if (!LoRa.begin(915E6)) { // initialize ratio at 915 MHz
3437
Serial.println("LoRa init failed. Check your connections.");
3538
while (true); // if failed, do nothing
3639
}
37-
LoRa.setSyncWord(F3); // ranges from 0-0xFF, default 0x34, see API docs
40+
41+
LoRa.setSyncWord(0xF3); // ranges from 0-0xFF, default 0x34, see API docs
3842
Serial.println("LoRa init succeeded.");
3943
}
4044

@@ -60,18 +64,19 @@ void sendMessage(String outgoing) {
6064
msgCount++; // increment message ID
6165
}
6266

63-
6467
void onReceive(int packetSize) {
6568
if (packetSize == 0) return; // if there's no packet, return
6669

6770
// read packet header bytes:
6871
String incoming = "";
72+
6973
while (LoRa.available()) {
7074
incoming += (char)LoRa.read();
7175
}
72-
Serial.println("Message:" + incoming);
73-
Serial.println("RSSI:" + String(LoRa.packetRssi()));
74-
Serial.println("Snr:" + String(LoRa.packetSnr()));
76+
77+
Serial.println("Message: " + incoming);
78+
Serial.println("RSSI: " + String(LoRa.packetRssi()));
79+
Serial.println("Snr: " + String(LoRa.packetSnr()));
7580
Serial.println();
7681
}
7782

0 commit comments

Comments
 (0)