Try Embedded

Long-range sensor link (two SX1278 LoRa radios)

One Arduino sends a sensor reading over LoRa every two seconds; the other prints it with the signal strength and SNR it arrived with.

Communication · Intermediate

In a real product: Farm sensor network (Agriculture): Readings travel kilometres across fields on LoRa.

LoRa trades speed for range: a few hundred bytes a second, but kilometres between radios outdoors. A typical use is a sensor far from anything, reporting now and then. The first Uno reads an LM35 and sends it; the second prints each packet with its RSSI and SNR, the numbers that tell you how much margin the link has.

What you'll learn

  • Sending a packet with beginPacket(), print() and endPacket(), and receiving with parsePacket()
  • Time on air: a packet at SF7 takes about 40 ms; at SF12 over a second
  • Reading the link: RSSI (dBm) and SNR (dB), and what makes them worse

How it works

Both radios start on 433 MHz with the library's defaults: spreading factor 7, 125 kHz bandwidth, coding rate 4/5 and sync word 0x12. The sender's endPacket() waits for the radio's TxDone flag, so it returns once the packet has gone. The receiver calls parsePacket() in its loop: it keeps the radio in receive mode and returns the packet's length once one has arrived. packetRssi() and packetSnr() report how strong and how clean it was.

Try this

  • Drag the receiver's RSSI down to −120 dBm and its SNR below zero: LoRa still decodes below the noise floor.
  • Call LoRa.setSpreadingFactor(12) on the sender only: nothing arrives. Then on both: it arrives, much later.
  • Set the receiver's range to out and add a timeout on the receiver that reports a lost link.

sketch.ino

// Sender: an LM35 on A0 read every two seconds and sent over an SX1278 LoRa radio (NSS 10, RST 9, DIO0 2) at
// 433 MHz, with a packet number.
#include <SPI.h>
#include <LoRa.h>

int packet = 0;

void setup() {
  Serial.begin(9600);
  LoRa.setPins(10, 9, 2);
  if (!LoRa.begin(433E6)) {
    Serial.println("No LoRa radio found");
    while (true) {}
  }
}

void loop() {
  float celsius = analogRead(A0) * 500.0 / 1023;
  unsigned long t0 = millis();
  LoRa.beginPacket();
  LoRa.print("#");
  LoRa.print(packet++);
  LoRa.print(" ");
  LoRa.print(celsius, 1);
  LoRa.print(" C");
  LoRa.endPacket();
  Serial.print("Sent in ");
  Serial.print(millis() - t0);
  Serial.println(" ms");
  delay(2000);
}

uno2.ino

// Receiver: an SX1278 LoRa radio (NSS 10, RST 9, DIO0 2) at 433 MHz prints each packet with its RSSI and SNR.
#include <SPI.h>
#include <LoRa.h>

void setup() {
  Serial.begin(9600);
  LoRa.setPins(10, 9, 2);
  if (!LoRa.begin(433E6)) {
    Serial.println("No LoRa radio found");
    while (true) {}
  }
  Serial.println("Listening on 433 MHz");
}

void loop() {
  if (!LoRa.parsePacket()) return;
  Serial.print("Got \"");
  while (LoRa.available()) Serial.print((char)LoRa.read());
  Serial.print("\" RSSI ");
  Serial.print(LoRa.packetRssi());
  Serial.print(" dBm, SNR ");
  Serial.print(LoRa.packetSnr());
  Serial.println(" dB");
}

Lessons behind it