Usama
Usama

Reputation: 11

LoRa Sx1278 device not receiving nor sending data

I have two devices.

  1. LoRa sx1278 with Arduino mega 2560 (Device 1, sender).
  2. LoRa sx1278 with Arduino UNO (Device 2, receiver).

When I upload the code below to both of them, sometimes they work properly, and sometimes the circuit freezes, the LoRa is not sending/receiving, and it suddenly stops, while no changes were made to the hardware.

This is the workflow:
Device 1: sends 5 messages and then switch to receiving mode to receive data.
Device 2: receives the 5 messages and then switches to sending data.

Device 1 code:

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


const long frequency = 433E6; // LoRa frequency (adjust according to your region)

int sendCount = 0;          // To count sent messages
bool isSending = true;      // Flag for sending/receiving state

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

  if (!LoRa.begin(frequency)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Sender/Receiver");

  // Initially, set the node to send messages
  switchToSending();
}

void loop() {
  if (isSending) {
    sendMessages(5);  // Send 5 messages
    switchToReceiving();  // Switch to receiving after sending
  } else {
    receiveMessages();  // Receive counter messages
  }
}

void sendMessages(int count) {
  for (int i = 0; i < count; i++) {
    String message = "Message " + String(i + 1);
    LoRa.beginPacket();
    LoRa.print(message);
    LoRa.endPacket();
    Serial.println("Sent: " + message);
    delay(1000);  // Delay between messages
  }
  sendCount += count;
}

void receiveMessages() {
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // Read received packet
    String receivedMessage = "";
    while (LoRa.available()) {
      receivedMessage += (char)LoRa.read();
    }
    Serial.println("Received: " + receivedMessage);

    // Process the received message
    if (receivedMessage.startsWith("Counter")) {
      Serial.println("Received counter message from Receiver.");
    }
  }
}

void switchToSending() {
  Serial.println("Switching to sending mode...");
  isSending = true;
}

void switchToReceiving() {
  Serial.println("Switching to receiving mode...");
  isSending = false;
}

Device 2 code:

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

const long frequency = 433E6; // LoRa frequency (adjust according to your region)

int receivedMessageCount = 0;  // Counter for received messages
int counter = 0;               // Counter to track messages sent back to the transmitter
bool isSendingCounter = false; // Flag to determine if the receiver should start sending counter messages

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

  if (!LoRa.begin(frequency)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Receiver/Sender");
}

void loop() {
  if (!isSendingCounter) {
    receiveMessages();  // In receiving mode, waiting for 5 messages
  } else {
    sendCounterMessage();  // Start sending counter messages after receiving all 5
  }
}

void receiveMessages() {
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // Read received packet
    String receivedMessage = "";
    while (LoRa.available()) {
      receivedMessage += (char)LoRa.read();
    }

    // Print received message
    Serial.println("Received: " + receivedMessage);
    receivedMessageCount++;  // Increment received message count

    // Check if all 5 messages are received
    if (receivedMessageCount >= 5) {
      Serial.println("All 5 messages received. Switching to counter sending mode...");
      isSendingCounter = true;  // Switch to sending counter messages
      counter = 0;  // Reset counter
    }
  }
}

void sendCounterMessage() {
  String message = "Counter " + String(counter);
  LoRa.beginPacket();
  LoRa.print(message);
  LoRa.endPacket();
  Serial.println("Sent: " + message);
  counter++;
  delay(1000);  // Delay between counter messages

  // After sending 5 counter messages, switch back to receiving mode
  if (counter >= 5) {
    isSendingCounter = false;
    receivedMessageCount = 0;  // Reset received message count
    Serial.println("Switching back to receiving mode...");
  }
}

Upvotes: 1

Views: 144

Answers (1)

dda
dda

Reputation: 6203

You should not send so many messages at once. For 2 reasons. One, duty cycle: you should not occupy the airwaves, but limit yourself to 1%. That is, 36 seconds per hour. Other devices around you, especially at that very common frequency, could be transmitting too.

Two, you're probably overloading the LoRa chip with successive messages so quickly one after the other.

LoRa was not designed to send continuously and repeatedly[^1]. Send one message, and wait for the next cycle, somewherein the vicinityof 2~3 minutes. Meanwhile you listen for incoming packets. The other device receives, and answers, or not.

Just don't flood your LoRa chip, or the airwaves...

[^1]: well there's a continuous transmit mode but it's for testing, not regular operation...

Upvotes: 0

Related Questions