The arduino-LoRa library comes with a number of samples showing how to use its functionality. The LoRaSender and LoRaReceiver samples show the bare minimum of code required to send and receive messages.
LoRaSender
This sample uses all default settings except for frequency
#include <SPI.h>
#include <LoRa.h>
int counter = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Sender");
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
delay(5000);
}
void loop() {
Serial.print("Sending packet: ");
Serial.println(counter);
// send packet
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter);
LoRa.endPacket();
counter++;
delay(5000);
}
In the Visual Studio output window I could see the received messages including a “corrupted” one which was displayed because the SX127XLoRaDeviceClient couldn’t force Cyclic Redundancy Check(CRC)s.
Loaded '/usr/lib/dotnet/shared/Microsoft.NETCore.App/5.0.4/Microsoft.Win32.Primitives.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
17:08:14-RX length 108 "hello 7" snr 9.3 packet rssi -63dBm rssi -102dBm
17:08:24-RX length 108 "hello 0" snr 9.5 packet rssi -64dBm rssi -104dBm
17:08:29-RX length 108 "hello 1" snr 9.3 packet rssi -64dBm rssi -102dBm
17:08:34-RX length 108 "hello 2" snr 9.5 packet rssi -64dBm rssi -102dBm
17:08:39-RX length 108 "hello 3" snr 8.5 packet rssi -61dBm rssi -104dBm
17:08:44-RX length 108 "hello 4" snr 8.5 packet rssi -62dBm rssi -104dBm
17:08:49-RX length 108 "hello 5" snr 9.3 packet rssi -64dBm rssi -104dBm
17:08:54-RX length 108 "hello 6" snr 9.3 packet rssi -64dBm rssi -102dBm
17:08:59-RX length 108 "hello 7" snr 9.3 packet rssi -64dBm rssi -102dBm
17:09:04-RX length 108 "hello 8" snr 9.3 packet rssi -64dBm rssi -100dBm
17:09:09-RX length 108 "hello 9" snr 9.3 packet rssi -64dBm rssi -102dBm
17:09:14-RX length 108 "hello 10" snr 8.8 packet rssi -58dBm rssi -102dBm
17:09:19-RX length 108 "hello 11" snr 9.3 packet rssi -60dBm rssi -104dBm
17:09:24-RX length 108 "hello 12" snr 9.5 packet rssi -59dBm rssi -104dBm
17:09:29-RX length 108 "hello 13" snr 9.0 packet rssi -60dBm rssi -102dBm
17:09:34-RX length 108 "hello 14" snr 9.5 packet rssi -59dBm rssi -105dBm
17:09:39-RX length 108 "hello 15" snr 9.0 packet rssi -57dBm rssi -102dBm
17:09:44-RX length 108 "hello 16" snr 9.3 packet rssi -61dBm rssi -104dBm
17:09:49-RX length 108 "hello 17" snr 9.5 packet rssi -61dBm rssi -104dBm
17:09:54-RX length 108 "hello 18" snr 9.0 packet rssi -59dBm rssi -104dBm
17:09:59-RX length 108 "hello 19" snr 9.3 packet rssi -61dBm rssi -102dBm
17:10:04-RX length 108 "hello 20" snr 9.0 packet rssi -59dBm rssi -104dBm
17:10:09-RX length 108 "hello 21" snr 9.3 packet rssi -61dBm rssi -102dBm
17:10:14-RX length 108 "hello 22" snr 9.5 packet rssi -60dBm rssi -102dBm
17:10:19-RX length 108 "hello 23" snr 9.3 packet rssi -60dBm rssi -104dBm
17:10:24-RX length 108 "hello 24" snr 9.3 packet rssi -60dBm rssi -103dBm
17:10:26-RX length 212 "�Q�Ԕv&G=����[9Y���
2S��ᒵ��O*�Ϥ��X��p쏊��" snr 50.8 packet rssi -104dBm rssi -102dBm
17:10:30-RX length 108 "hello 25" snr 9.5 packet rssi -60dBm rssi -102dBm
17:10:35-RX length 108 "hello 26" snr 9.3 packet rssi -60dBm rssi -104dBm
LoRaReceiver
This sample also uses all default settings except of the frequency
#include <SPI.h>
#include <LoRa.h>
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Receiver");
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
In the Visual Studio output window I could see messages getting transmitted with sent confirmations.
Loaded '/usr/lib/dotnet/shared/Microsoft.NETCore.App/5.0.4/Microsoft.Win32.Primitives.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
17:21:19- Length 28 "Hello LoRa from .NET Core! 1"
17:21:19-TX Done
17:21:29- Length 28 "Hello LoRa from .NET Core! 2"
17:21:29-TX Done
17:21:39- Length 28 "Hello LoRa from .NET Core! 3"
17:21:39-TX Done
17:21:49- Length 28 "Hello LoRa from .NET Core! 4"
17:21:49-TX Done
17:21:59- Length 28 "Hello LoRa from .NET Core! 5"
17:21:59-TX Done
17:22:09- Length 28 "Hello LoRa from .NET Core! 6"
17:22:09-TX Done
17:22:19- Length 28 "Hello LoRa from .NET Core! 7"
17:22:19-TX Done
17:22:29- Length 28 "Hello LoRa from .NET Core! 8"
17:22:29-TX Done
17:22:39- Length 28 "Hello LoRa from .NET Core! 9"
17:22:39-TX Done
17:22:49- Length 29 "Hello LoRa from .NET Core! 10"
17:22:49-TX Done
17:22:59- Length 29 "Hello LoRa from .NET Core! 11"
17:22:59-TX Done
17:23:09- Length 29 "Hello LoRa from .NET Core! 12"
17:23:09-TX Done
17:23:19- Length 29 "Hello LoRa from .NET Core! 13"
17:23:19-TX Done
17:23:29- Length 29 "Hello LoRa from .NET Core! 14"
17:23:29-TX Done
17:23:39- Length 29 "Hello LoRa from .NET Core! 15"
17:23:39-TX Done
17:23:49- Length 29 "Hello LoRa from .NET Core! 16"
17:23:49-TX Done
17:23:59- Length 29 "Hello LoRa from .NET Core! 17"
17:23:59-TX Done
17:24:09- Length 29 "Hello LoRa from .NET Core! 18"
17:24:09-TX Done
I modified the SX127X.NetCore SX127XLoRaDeviceClient adding a conditional compile options for each sample
static void Main(string[] args)
{
int messageCount = 1;
sX127XDevice.Initialise(
SX127XDevice.RegOpModeMode.ReceiveContinuous,
915000000.0,
powerAmplifier: SX127XDevice.PowerAmplifier.PABoost,
#if LORA_SENDER // From the Arduino point of view
rxDoneignoreIfCrcMissing: false
#endif
#if LORA_RECEIVER // From the Arduino point of view, don't actually need this as already inverted
invertIQTX: true
#endif
#if LORA_SET_SYNCWORD
syncWord: 0xF3,
invertIQTX: true,
rxDoneignoreIfCrcMissing: false
#endif
#if LORA_SET_SPREAD
spreadingFactor: SX127XDevice.RegModemConfig2SpreadingFactor._256ChipsPerSymbol,
invertIQTX: true,
rxDoneignoreIfCrcMissing: false
#endif
);
#if DEBUG
sX127XDevice.RegisterDump();
#endif
#if LORA_SENDER
sX127XDevice.OnReceive += SX127XDevice_OnReceive;
sX127XDevice.Receive();
#endif
#if LORA_RECEIVER
sX127XDevice.OnTransmit += SX127XDevice_OnTransmit;
#endif
#if LORA_SENDER
Thread.Sleep(-1);
#else
Thread.Sleep(5000);
#endif
while (true)
{
string messageText = "Hello LoRa from .NET Core! " + messageCount.ToString();
byte[] messageBytes = UTF8Encoding.UTF8.GetBytes(messageText);
Console.WriteLine($"{DateTime.Now:HH:mm:ss}- Length {messageBytes.Length} \"{messageText}\"");
sX127XDevice.Send(messageBytes);
messageCount += 1;
Thread.Sleep(10000);
}
}
Summary
While testing the LoRaReceiver sample I found a problem with how my code managed the RegOpMode register LoRa status value. In previous versions of the code I used RegOpModeModeDefault to manage status when the ProcessTxDone(byte IrqFlags) method completed and Receive() was called.
I had assumed that that the device would always be set with SetMode(RegOpModeModeDefault) but RegOpModeModeDefault was always RegOpModeMode.Sleep.
I guess this is a good place to ask some questions….
I have a Dragino Moisture sensor that we would like to have read by the Pi. I have two of them on loan, however the Pi never sees them.
What am I missing. The devices are designed to be added to a gateway but is this required? Can’t the pi just gather up the data and process it itself. I have no requirement to send the data anywhere else other than the Pi.
Regards.
Hi
One of the main problems I encounter is that LoRa (https://www.semtech.com/lora) & LoRaWAN(https://lora-alliance.org/) are used interchangeably but technically they aren’t.
I assume you have the LSE01 this a LoRaWAN devices, my library is for accessing the LoRa capabilities of Semtech SX127X devices.
So sorry my library is not a lot of use to you.
@KiwiBryn