Lora

Setup

https://www.arduino.cc/en/reference/SPI

Sending Code

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

int counter = 0;
int LED = 12;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
//   while (!Serial);

  Serial.println("LoRa Sender");

  LoRa.setTxPower(20);
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  
  LoRa.setTxPower(20);
  LoRa.setSpreadingFactor(12);
  LoRa.setSignalBandwidth(62.5E3);
  LoRa.setCodingRate4(8);
  
}

void loop() { 
    // Serial.print("Sending packet: ");
  digitalWrite(LED, HIGH);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  Serial.print("Sending packet: ");
  Serial.println(counter);
  
  // send packet
  LoRa.beginPacket();
  LoRa.print("Hi Jemima! ");
  LoRa.print(counter);
  LoRa.endPacket();

  counter++;
  digitalWrite(LED, LOW);

  delay(2000);
}

Receiving Code

#include <Wire.h>
#include "rgb_lcd.h"

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

int counter = 0;

rgb_lcd lcd;

const int colorR = 255;
const int colorG = 20;
const int colorB = 147;

void setup() {
    
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);

    lcd.setRGB(colorR, colorG, colorB);
    // lcd.display();
    // Print a message to the LCD.

    Serial.begin(9600);
    while (!Serial);

    Serial.println("LoRa Sender");

    if (!LoRa.begin(433E6)) {
        Serial.println("Starting LoRa failed!");
        while (1);
    }

    lcd.print("LoRa Initialised");
    Serial.println("LoRa Initialised");
    LoRa.setTxPower(20);
    LoRa.setSpreadingFactor(12);
    LoRa.setSignalBandwidth(62.5E3);
    LoRa.setCodingRate4(8);

    delay(1000);
}

void loop() {
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    
    // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    lcd.setCursor(0, 0);
    lcd.print("Received packet ");

    // read packet
    
    int count = 0;
    while (LoRa.available()) {
      lcd.setCursor(count, 0);
      count++;
      // do not print to serial and lcd at same time as once read a character is removed from the stream
      // Serial.print((char)LoRa.read());  
      lcd.print((char)LoRa.read());
    }
    lcd.setCursor(0, 1);
    lcd.print(LoRa.packetRssi());
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
    delay(2000);

  } else {
    lcd.setCursor(0, 1);
    lcd.print("           "); 
    lcd.setCursor(0, 0);
    lcd.print("Listening...      ");
  }
}

Library

RSSI

Typical LoRa values:

  • RSSI Min = -120 bBm

  • -30dBm = strong signal

  • -120dBm = weak signal

Further Reading

Last updated

Was this helpful?