Communication Using LoRa
LoRaWAN is an exciting IoT technology that is expanding our communication capabilities. LoRaWAN allows devices to communicate over tens or even hundreds of kilometers (in extreme cases), all without a cell network or a high-powered antenna array. The big tradeoff with this technology is that data rates become severely restricted—in the low kb/s range depending on conditions. While you might not be able to stream a 4k movie or even a low-fi audio clip, LoRaWAN is still an excellent option for intermittent sensor data spread over a wide geographic area.
One way to get started with LoRaWAN is with an internet gateway that already exists, like The Things Network (TTN), or by installing one yourself. An internet gateway lets you get your data to the cloud for usage anywhere, but we can also set up point-to-point communication between two MKR WAN boards as the first step towards LoRaWAN exploration and point-to-point communication is pretty simple, though figuring out the steps can be tricky. Let’s walk through how to get one-way communication going between two of these boards.
Arduino IDE Setup
As for any modern Arduino Board you need to download the right Board and Library. So, once you have started your Arduino application you need to add these elements:
- Go to Tools >> Boards >> Board Manager menu
- Search for MKR
- Select the last revision and click on the Install button.
- Install the MKRWAN library.
Hardware Needed
- 2x Arduino MKR WAN 1300.
- 2x Antenna.
- 2x Micro USB cable.
Once you have your hardware ready to go, follow these steps to install and run your Arduino LoRaWAN:
1. Physically attach an antenna to both MKR WAN boards.
2. Open two instances of the Arduino IDE via a desktop or Start menu shortcut. Opening both separately (not through the file-new dialog) allows you to manipulate two serial ports at the same time. Doing so will make the process much easier.
3. Navigate back to File-Examples, then LoRa. Under this section, load “LoRaSender” onto one board, then “LoRaReceiver” onto the other.
4. Start a serial monitor on both ports, and you’ll see the transmitter board sending a packet every five seconds. The receiver serial port—if everything works properly—will show it receiving these packets with a Received Signal Strength Indication (RSSI) number to show how well the pair is communicating.
Sender Code
#include <SPI.h>
#include <LoRa.h>
int counter = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Sender");
if (!LoRa.begin(868E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
Serial.print("Sending packet: ");
Serial.println(counter);
// send packet
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter);
LoRa.endPacket();
counter++;
delay(5000);
}
Receiver Code
#include <SPI.h>
#include <LoRa.h>
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Receiver");
if (!LoRa.begin(868E6)) {
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());
}
}
Troubleshoot
If the code is not working, there are some common issues we might need to troubleshoot:
- Antenna is not connected properly.
- The radio frequency is wrong. Remember, 868E6 for Europe and 915E6 for Australia & North America.
- We have not opened the Serial Monitor.
- We are using the same computer for both boards without a serial interfacing program.
More Detail is here.