user73081: I'm working on LoRa project using [Ai-Thinker RA-01 SX1278 433MHz LoRa][1] with [sandeepmistry/arduino-LoRa][2] library. I modified LoRaReciever example so it looks like this
#include <SPI.h>
#include <LoRa.h>

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

  Serial.println("LoRa Receiver");

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

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  LoRa.onReceive(onReceive);
}
void onReceive(int packetSize) {
  
  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());
  }
}

I receive 2-4 packets successfully, but then I don't receive any, even though the transmitter is still transmitting. (If I restart my Arduino it receives 2-4 packets again).
I have both LoRa modules really close to each other (about 10 cm) could it cause the problem? - Even when I tried like 15m with walls, problem stayed.
Sometimes, the receiver gets bugged and starts receiving packets all the time (one packet like 50 times), when it's bugged like this, it doesn't stop receiving (unique) packets after any number.
Do you know where the problem could be? Thanks.
I use Arduino Mega
#include <SPI.h>
#include <LoRa.h>

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

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(915E6)) {
    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());
  }
}


This is original LoRaReciever example, but I don't receive anything using it.
[1]: https://www.laskarduino.cz/ai-thinker-ra-01-sx1278-433mhz-lora-modul/
[2]: https://github.com/sandeepmistry/arduino-LoRa

user73081: I found the solution.
The problem was with calling onRecieve in loop not in setup
It should have looked like this...
#include <SPI.h>
#include <LoRa.h>

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

  Serial.println("LoRa Receiver");

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

void loop() {
  //do nothing
}
void onReceive(int packetSize) {
  
  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());
  }
}


73081: <p>I'm working on LoRa project using [Ai-Thinker RA-01 SX1278 433MHz LoRa][1] with [sandeepmistry/arduino-LoRa][2] library. I modified LoRaReciever example so it looks like this</p>
<pre><code>#include &lt;SPI.h&gt;
#include &lt;LoRa.h&gt;

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

  Serial.println(&quot;LoRa Receiver&quot;);

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

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

    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());
    }

    // print RSSI of packet
    Serial.print(&quot;' with RSSI &quot;);
    Serial.println(LoRa.packetRssi());
  }
}
</code></pre>
<p>I receive 2-4 packets successfully, but then I don't receive any, even though the transmitter is still transmitting. (If I restart my Arduino it receives 2-4 packets again).</p>
<p>I have both LoRa modules really close to each other (about 10 cm) could it cause the problem? - Even when I tried like 15m with walls, problem stayed.</p>
<p>Sometimes, the receiver gets bugged and starts receiving packets all the time (one packet like 50 times), when it's bugged like this, it doesn't stop receiving (unique) packets after any number.</p>
<p>Do you know where the problem could be? Thanks.</p>
<p>I use Arduino Mega</p>
<pre><code>#include &lt;SPI.h&gt;
#include &lt;LoRa.h&gt;

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

  Serial.println(&quot;LoRa Receiver&quot;);

  if (!LoRa.begin(915E6)) {
    Serial.println(&quot;Starting LoRa failed!&quot;);
    while (1);
  }
}

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

    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());
    }

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

</code></pre>
<p>This is original LoRaReciever example, but I don't receive anything using it.
[1]: <a href="https://www.laskarduino.cz/ai-thinker-ra-01-sx1278-433mhz-lora-modul/" rel="nofollow noreferrer">https://www.laskarduino.cz/ai-thinker-ra-01-sx1278-433mhz-lora-modul/</a>
[2]: <a href="https://github.com/sandeepmistry/arduino-LoRa" rel="nofollow noreferrer">https://github.com/sandeepmistry/arduino-LoRa</a></p>
 73081: <p>I found the solution.
The problem was with calling <code>onRecieve</code> in <code>loop</code> not in <code>setup</code>
It should have looked like this...</p>
<pre><code>#include &lt;SPI.h&gt;
#include &lt;LoRa.h&gt;

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

  Serial.println(&quot;LoRa Receiver&quot;);

  if (!LoRa.begin(433E6)) {
    Serial.println(&quot;Starting LoRa failed!&quot;);
    while (1);
  }
  LoRa.onReceive(onReceive);
  LoRa.receive();
}

void loop() {
  //do nothing
}
void onReceive(int packetSize) {
  
  if (packetSize) {
    // received a packet
    Serial.print(&quot;Received packet '&quot;);

    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());
    }

    // print RSSI of packet
    Serial.print(&quot;' with RSSI &quot;);
    Serial.println(LoRa.packetRssi());
  }
}
</code></pre>
