Arduino LORA Send and Receive Data

mavericks1100 picture mavericks1100 · Jun 21, 2018 · Viewed 10.1k times · Source

I tried to allow sending and receiving data using 2 Arduino Unos, 2 LORA chips (SX1278 433MHz), 2 antennas and 2 Arduino IDE.

Problem is with the receiving data command.

This is the code for SENDING command:

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

int counter = 0;

const int ss = 10;          
const int reset = 9;     
const int dio0 = 2;

void setup() {
  Serial.begin(9600);
  LoRa.begin(433E6);
  LoRa.setPins(ss, reset, dio0);
  while (!Serial);

  Serial.println("LoRa Sender");

}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);

  // send packet
  LoRa.beginPacket();
  LoRa.print("hello ");
  LoRa.print(counter);
  LoRa.endPacket();

  counter++;

  delay(5000);
}

On serial monitor, I succeed in sending packages, but not receiving. This is the RECEIVING code:

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

const int ss = 10;        
const int reset = 9;       
const int dio0 = 2;

void setup() {
  Serial.begin(9600);
  LoRa.begin(433E6);
  LoRa.setPins(ss, reset, dio0);
  while (!Serial);

  Serial.println("LoRa Receiver");

}

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());
      Serial.print("hello ");
    }

    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

I used instructions about connections from this git page: https://github.com/sandeepmistry/arduino-LoRa

Answer

Simone Salerno picture Simone Salerno · Dec 20, 2018

To make this board work, I had to explicitly initialize SPI

SPI.begin(/*sck*/ 5, /*miso*/ 19, /*mosi*/ 27, /*ss*/ cs);

It may be the same for yours. Also, you should properly initialize Lora:

while (!LoRa.begin(433E6)) {
    Serial.print(".");
    delay(500);
}
Serial.println("LoRa Initializing OK!");

With the code you posted, you don't really know if it initialized correcly or if it's actually sending any data.