Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

LoRa long-range communication system, similar to WIFI but working over very long distances (up to 10km). Unlike regular wifi, LoRa is designed to send only very small amounts of data, and pretty slowly.

Hardware

The Arduino MKR WAN 1310 is a board that combines the functionality of the MKR Zero and LoRa / LoRaWANTM connectivity. There are also modules that allow LoRa communication for almost any device, and several other Arduino based devices with inbuilt LoRa functionality.

For the experiments below we will also need:

  • Compatible Antena

  • Jumper wires

  • Breadboard

  • A sensor

Getting started with the MKR WAN 1310

For this experiment, we need need to LoRa devices. We need to have a compatible antenna connected to both our devices.

We will also need to install the Arduino Lora Library.

We also have to be aware of the radio frequency plan allowed in our region. It’s 868E6 for Europe, and you can find the full list of frequency bands are here.

Sender Code

The sender code sends a simple message, with a delay of 5 seconds. Make sure you change the contents of myData.

#include <SPI.h>
#include <LoRa.h>

int counter = 0;
String myData = "my name";

void setup() {
  Serial.begin(9600);// wait until serial has started 
  Serial.println("LoRa Sender");
  if (!LoRa.begin(868E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  delay(1000);
 pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);
  // send packet
  LoRa.beginPacket();
  LoRa.print(myData);
  LoRa.endPacket();
  counter++;
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(1000);                  
  digitalWrite(LED_BUILTIN, LOW);   
  delay(4000);   
}

Receiver Code

#include <SPI.h>
#include <LoRa.h>

String contents = "";

void setup() {
  Serial.begin(9600);
  while (!Serial); // wait until serial has started 
  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()) {  
      contents += (char)LoRa.read();
    }
    Serial.println(contents);
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
    contents = "";
  }
}

Advanced example MKR WAN 1310

In the previous example, you might have noticed a potential problem. The receiving device is getting LorRa messages from anyone sending data on that frequency. Furthermore, anyone can listen in to the data being sent. This could be resolved with some encryption, but that goes beyond the scope of this tutorial.

We are going to solve the problem by simply sending an ID with our packet, then checking it at the receiving end and discarding any other messages.

We are using the Serial port extensively here to see the workings of our code, which is very slow for the arduino. It would be wise to remove most of this communication before using the example in the real world.

Sender Code with an Analog Sensor.

#include <SPI.h>
#include <LoRa.h>

int counter = 0;
String myUniqueId = "asdfw1";
void setup() {
  Serial.begin(9600);
  Serial.println("LoRa Sender");
  if (!LoRa.begin(868E6)) {
    Serial.println("Starting LoRa failed!");
    while(1);
  }
 delay(1000);
 pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int sensorValue = analogRead(A0);

  Serial.print("Sending packet: ");
  Serial.println(counter);
  // send packet
  LoRa.beginPacket();
  LoRa.print(sensorValue);
  LoRa.print(",");
  LoRa.print(myUniqueId);
  LoRa.endPacket();
  counter++;
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);  
  delay(4000);    
}

Receiver Code

#include <SPI.h>
#include <LoRa.h>

String contents = "";
String myUniqueId = "asdfw1";

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(868E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");

    // read packet
    while (LoRa.available()) {
      contents += (char)LoRa.read();
    }
    Serial.println(contents);
    // print RSSI of packet (Received Signal Strength Indicator)
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
    int commaIndex = contents.indexOf(',');
    String data = contents.substring(0, commaIndex);
    String ID = contents.substring(commaIndex + 1);

    if (data && ID) {
      Serial.println("Recieved Matching format");
      if (ID.equals(myUniqueId)) {
          Serial.println("ID matches!");
          digitalWrite(LED_BUILTIN, HIGH);
          Serial.print("data: ");
          Serial.println(data);
        } else {
          Serial.print(myUniqueId);
          Serial.print(" does not match ");
          Serial.println(ID);
          digitalWrite(LED_BUILTIN, LOW);
        }
    }
    contents = "";
  }
}