Skip to content

Commit 0c72fec

Browse files
tigoesandeepmistry
authored andcommitted
Added new examples and tests
1 parent 2f20d99 commit 0c72fec

File tree

7 files changed

+540
-0
lines changed

7 files changed

+540
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
LoRa register dump
3+
context: Arduino
4+
5+
*/
6+
#include <SPI.h> // include libraries
7+
#include <LoRa.h>
8+
9+
void setup() {
10+
while (!Serial);
11+
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
15+
Serial.println("LoRa init failed. Check your connections.");
16+
while (true); // if failed, do nothing
17+
}
18+
LoRa.sleep();
19+
LoRa.dumpRegisters(Serial);
20+
}
21+
22+
23+
void loop() {
24+
25+
}
26+

examples/LoRaDuplex/LoRaDuplex.ino

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
LoRa Duplex communication
3+
4+
Sends a message every half second, and polls continually
5+
for new incoming messages. Implements a one-byte addressing scheme,
6+
with 0xFF as the broadcast address.
7+
8+
Uses readString() from Stream class to read payload. The Stream class'
9+
timeout may affect other functuons, like the radio's callback. For an
10+
11+
created 28 April 2017
12+
by Tom Igoe
13+
*/
14+
#include <SPI.h> // include libraries
15+
#include <LoRa.h>
16+
const int csPin = 7; // LoRa radio chip select
17+
const int resetPin = 6; // LoRa radio reset
18+
const int irqPin = 1; // change for your board; must be a hardware interrupt pin
19+
20+
String outgoing; // outgoing message
21+
byte msgCount = 0; // count of outgoing messages
22+
byte localAddress = 0xBB; // address of this device
23+
byte destination = 0xFF; // destination to send to
24+
long lastSendTime = 0; // last send time
25+
int interval = 2000; // interval between sends
26+
27+
void setup() {
28+
while (!Serial);
29+
Serial.begin(9600); // initialize serial
30+
Serial.println("LoRa Duplex");
31+
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
32+
33+
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz
34+
Serial.println("LoRa init failed. Check your connections.");
35+
while (true); // if failed, do nothing
36+
}
37+
38+
LoRa.setTimeout(10); // set Stream timeout of 10ms
39+
Serial.println("LoRa init succeeded.");
40+
}
41+
42+
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+
}
50+
51+
// parse for a packet, and call onReceive with the result:
52+
onReceive(LoRa.parsePacket());
53+
54+
}
55+
56+
void sendMessage(String outgoing) {
57+
LoRa.beginPacket(); // start packet
58+
LoRa.write(destination); // add destination address
59+
LoRa.write(localAddress); // add sender address
60+
LoRa.write(msgCount); // add message ID
61+
LoRa.write(outgoing.length()); // add payload length
62+
LoRa.print(outgoing); // add payload
63+
LoRa.endPacket(); // finish packet and send it
64+
msgCount++; // increment message ID
65+
}
66+
67+
68+
void onReceive(int packetSize) {
69+
if (packetSize == 0) return; // if there's no packet, return
70+
71+
// read packet header bytes:
72+
int recipient = LoRa.read(); // recipient address
73+
byte sender = LoRa.read(); // sender address
74+
byte incomingMsgId = LoRa.read(); // incoming msg ID
75+
byte incomingLength = LoRa.read(); // incoming msg length
76+
String incoming = "";
77+
while (LoRa.available()) {
78+
incoming += (char)LoRa.read();
79+
}
80+
if (incomingLength != incoming.length()) { // check length for error
81+
Serial.println("error: message length does not match length");
82+
return; // skip rest of function
83+
}
84+
85+
// if the recipient isn't this device or broadcast,
86+
if (recipient != localAddress && recipient != 0xFF) {
87+
Serial.println("This message is not for me.");
88+
return; // skip rest of function
89+
}
90+
91+
// 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()));
99+
Serial.println();
100+
}
101+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
LoRa Duplex communication wth callback
3+
4+
Sends a message every half second, and uses callback
5+
for new incoming messages. Implements a one-byte addressing scheme,
6+
with 0xFF as the broadcast address.
7+
8+
Note: while sending, LoRa radio is not listening for incoming messages.
9+
Note2: when using the callback method, you can't use any of the Stream
10+
functions that rely on the timeout, such as readString, parseInt(), etc.
11+
12+
created 28 April 2017
13+
by Tom Igoe
14+
*/
15+
#include <SPI.h> // include libraries
16+
#include <LoRa.h>
17+
const int csPin = 7; // LoRa radio chip select
18+
const int resetPin = 6; // LoRa radio reset
19+
const int irqPin = 1; // change for your board; must be a hardware interrupt pin
20+
21+
String outgoing; // outgoing message
22+
byte msgCount = 0; // count of outgoing messages
23+
byte localAddress = 0xBB; // address of this device
24+
byte destination = 0xFF; // destination to send to
25+
long lastSendTime = 0; // last send time
26+
int interval = 2000; // interval between sends
27+
28+
void setup() {
29+
while (!Serial);
30+
Serial.begin(9600); // initialize serial
31+
Serial.println("LoRa Duplex with callback");
32+
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
33+
34+
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz
35+
Serial.println("LoRa init failed. Check your connections.");
36+
while (true); // if failed, do nothing
37+
}
38+
39+
LoRa.onReceive(onReceive);
40+
LoRa.receive();
41+
Serial.println("LoRa init succeeded.");
42+
}
43+
44+
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+
}
53+
}
54+
55+
void sendMessage(String outgoing) {
56+
LoRa.beginPacket(); // start packet
57+
LoRa.write(destination); // add destination address
58+
LoRa.write(localAddress); // add sender address
59+
LoRa.write(msgCount); // add message ID
60+
LoRa.write(outgoing.length()); // add payload length
61+
LoRa.print(outgoing); // add payload
62+
LoRa.endPacket(); // finish packet and send it
63+
msgCount++; // increment message ID
64+
}
65+
66+
67+
void onReceive(int packetSize) {
68+
if (packetSize == 0) return; // if there's no packet, return
69+
70+
// read packet header bytes:
71+
int recipient = LoRa.read(); // recipient address
72+
byte sender = LoRa.read(); // sender address
73+
byte incomingMsgId = LoRa.read(); // incoming msg ID
74+
byte incomingLength = LoRa.read(); // incoming msg length
75+
String incoming = ""; // payload of packet
76+
while (LoRa.available()) { // can't use readString() in callback, so
77+
incoming += (char)LoRa.read(); // add bytes one by one
78+
}
79+
if (incomingLength != incoming.length()) { // check length for error
80+
Serial.println("error: message length does not match length");
81+
return; // skip rest of function
82+
}
83+
84+
// if the recipient isn't this device or broadcast,
85+
if (recipient != localAddress && recipient != 0xFF) {
86+
Serial.println("This message is not for me.");
87+
return; // skip rest of function
88+
}
89+
90+
// 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()));
98+
Serial.println();
99+
}
100+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
LoRa Duplex communication with Spreading Factor
3+
4+
Sends a message every half second, and polls continually
5+
for new incoming messages. Sets the LoRa radio's spreading factor.
6+
7+
Spreading factor affects how far apart the radio's transmissions
8+
are, across the available bandwidth. Radios with different spreading
9+
factors will not receive each other's transmissions. This is one way you
10+
can filter out radios you want to ignore, without making an addressing scheme.
11+
12+
Spreading factor affects reliability of transmission at high rates, however,
13+
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
16+
for more on Spreading Factor.
17+
18+
created 28 April 2017
19+
by Tom Igoe
20+
*/
21+
#include <SPI.h> // include libraries
22+
#include <LoRa.h>
23+
const int csPin = 7; // LoRa radio chip select
24+
const int resetPin = 6; // LoRa radio reset
25+
const int irqPin = 1; // change for your board; must be a hardware interrupt pin
26+
27+
byte msgCount = 0; // count of outgoing messages
28+
int interval = 2000; // interval between sends
29+
long lastSendTime = 0; // time of last packet send
30+
31+
void setup() {
32+
while (!Serial);
33+
Serial.begin(9600); // initialize serial
34+
Serial.println("LoRa Duplex");
35+
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
36+
37+
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz
38+
Serial.println("LoRa init failed. Check your connections.");
39+
while (true); // if failed, do nothing
40+
}
41+
LoRa.setSpreadingFactor(8); // ranges from 6-12,default 7 see API docs
42+
Serial.println("LoRa init succeeded.");
43+
}
44+
45+
void loop() {
46+
if (millis() - lastSendTime > interval) {
47+
String message = "HeLoRa World! "; // send a message
48+
message += msgCount;
49+
sendMessage(message);
50+
Serial.println("Sending " + message);
51+
lastSendTime = millis(); // timestamp the message
52+
interval = random(2000) + 1000; // 2-3 seconds
53+
msgCount++;
54+
}
55+
56+
// parse for a packet, and call onReceive with the result:
57+
onReceive(LoRa.parsePacket());
58+
}
59+
60+
void sendMessage(String outgoing) {
61+
LoRa.beginPacket(); // start packet
62+
LoRa.print(outgoing); // add payload
63+
LoRa.endPacket(); // finish packet and send it
64+
msgCount++; // increment message ID
65+
}
66+
67+
68+
void onReceive(int packetSize) {
69+
if (packetSize == 0) return; // if there's no packet, return
70+
71+
// read packet header bytes:
72+
String incoming = "";
73+
while (LoRa.available()) {
74+
incoming += (char)LoRa.read();
75+
}
76+
Serial.println("Message:" + incoming);
77+
Serial.println("RSSI:" + String(LoRa.packetRssi()));
78+
Serial.println("Snr:" + String(LoRa.packetSnr()));
79+
Serial.println();
80+
}
81+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
LoRa Duplex communication with Sync Word
3+
4+
Sends a message every half second, and polls continually
5+
for new incoming messages. Sets the LoRa radio's Sync Word.
6+
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
9+
can filter out radios you want to ignore, without making an addressing scheme.
10+
11+
See the Semtech datasheet, http://www.semtech.com/images/datasheet/sx1276.pdf
12+
for more on Sync Word.
13+
14+
created 28 April 2017
15+
by Tom Igoe
16+
*/
17+
#include <SPI.h> // include libraries
18+
#include <LoRa.h>
19+
const int csPin = 7; // LoRa radio chip select
20+
const int resetPin = 6; // LoRa radio reset
21+
const int irqPin = 1; // change for your board; must be a hardware interrupt pin
22+
23+
byte msgCount = 0; // count of outgoing messages
24+
int interval = 2000; // interval between sends
25+
long lastSendTime = 0; // time of last packet send
26+
27+
void setup() {
28+
while (!Serial);
29+
Serial.begin(9600); // initialize serial
30+
Serial.println("LoRa Duplex");
31+
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
32+
33+
if (!LoRa.begin(915E6)) { // initialize ratio at 915Mhz
34+
Serial.println("LoRa init failed. Check your connections.");
35+
while (true); // if failed, do nothing
36+
}
37+
LoRa.setSyncWord(F3); // ranges from 0-0xFF, default 0x34, see API docs
38+
Serial.println("LoRa init succeeded.");
39+
}
40+
41+
void loop() {
42+
if (millis() - lastSendTime > interval) {
43+
String message = "HeLoRa World! "; // send a message
44+
message += msgCount;
45+
sendMessage(message);
46+
Serial.println("Sending " + message);
47+
lastSendTime = millis(); // timestamp the message
48+
interval = random(2000) + 1000; // 2-3 seconds
49+
msgCount++;
50+
}
51+
52+
// parse for a packet, and call onReceive with the result:
53+
onReceive(LoRa.parsePacket());
54+
}
55+
56+
void sendMessage(String outgoing) {
57+
LoRa.beginPacket(); // start packet
58+
LoRa.print(outgoing); // add payload
59+
LoRa.endPacket(); // finish packet and send it
60+
msgCount++; // increment message ID
61+
}
62+
63+
64+
void onReceive(int packetSize) {
65+
if (packetSize == 0) return; // if there's no packet, return
66+
67+
// read packet header bytes:
68+
String incoming = "";
69+
while (LoRa.available()) {
70+
incoming += (char)LoRa.read();
71+
}
72+
Serial.println("Message:" + incoming);
73+
Serial.println("RSSI:" + String(LoRa.packetRssi()));
74+
Serial.println("Snr:" + String(LoRa.packetSnr()));
75+
Serial.println();
76+
}
77+

0 commit comments

Comments
 (0)