ESP32 LoRa two way radio communication

Anelito
Posts: 2
Joined: Mon Feb 01, 2021 1:00 am

ESP32 LoRa two way radio communication

Postby Anelito » Mon Feb 01, 2021 1:07 am

Hello everyone,
I have recently been exploring LoRa communication system and, inspired by the Meshtastic project to which I contributed in the early days, I decided to create a very basic two-way radio using a commercial LoRa module based on the ESP32.
Leveraging the two cores of the ESP32, I separated the logic into two threads, one responsible for sending messages and another for receiving. The TX thread also takes care of displaying the user interface via WiFi.

Now, going to the debug part, messages are correctly sent, I can see their spectral trace using a SDR dongle at 433MHz. However, somehow the receiving logic doesn't work. The RX thread is correctly working, there are no errors. I tried to replace with callbacks without success.

What can I do to find out the cause?

Here's the project files: https://github.com/grcasanova/Talker

Thank you

Here is the RX task code

Code: Select all

[Codebox=c file=Untitled.c]  
  while(true) {
    // try to parse packet
    int packetSize = LoRa.parsePacket();
    if (packetSize) {
      // received a packet
      Serial.print("Received packet");

      // read packet header bytes:
      int recipient = LoRa.read();          // recipient address
      byte sender = LoRa.read();            // sender address
      byte incomingMsgId = LoRa.read();     // incoming msg ID
      byte incomingLength = LoRa.read();    // incoming msg length

      String incoming = "";

      while (LoRa.available()) {
        incoming += (char)LoRa.read();
      }

      if (incomingLength != incoming.length()) { // check length for error
        Serial.println("error: message length does not match length");
        continue;  // skip rest of the cycle
      }

      // if the recipient isn't this device or broadcast,
      if (recipient != FROM_ADDR && recipient != ALL) {
        Serial.println("This message is not for me.");
        continue;  // skip rest of the cycle
      }

      // Save msg
      String temp = "<li class='you'>"+incoming+"</li>";
      appendFile(SPIFFS, FILENAME, temp.c_str());

      // if message is for this device, or broadcast, print details:
      Serial.println("Message: " + incoming);
      Serial.println("RSSI: " + String(LoRa.packetRssi()));
      Serial.println("SNR: " + String(LoRa.packetSnr()));
      Serial.println();

      screen.clear();
      screen.setTextAlignment(TEXT_ALIGN_LEFT);
      screen.setFont(ArialMT_Plain_10);
      screen.drawString(0, 0, "Received Message:");
      screen.drawString(0, 15, incoming);
      screen.display();
    }
    
    vTaskDelay(10);
  }
}[/Codebox]
Last edited by Anelito on Mon Feb 01, 2021 10:34 am, edited 1 time in total.

chegewara
Posts: 2376
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 LoRa two way radio communication

Postby chegewara » Mon Feb 01, 2021 7:21 am

After quick pick on your code i have question, because i might miss it, where is LoRa switching to RX with lora.setRX(n) or lora.receive() which internally calls setRX()?
https://github.com/StuartsProjects/SX12 ... 7XLT.h#L57

Also you have to remember that you cant RX and TX at the same time, so you may miss some packets sometimes. In addition you have to call stRX() always after transmitting.

Anelito
Posts: 2
Joined: Mon Feb 01, 2021 1:00 am

Re: ESP32 LoRa two way radio communication

Postby Anelito » Mon Feb 01, 2021 10:39 am

I just followed Arduino LoRa library duplex example, where they don't use any switching between transmission and reception of data frames. Unless the "onReceive" callback already sets the radio in the receiving mode.

Regarding loss of packets, that's inherently connected to LoRa being half duplex, I haven't seen any special solution rather than basic buffering implemented in other projects like Meshtastic.
  1. #include <SPI.h>              // include libraries
  2. #include <LoRa.h>
  3.  
  4. const int csPin = 7;          // LoRa radio chip select
  5. const int resetPin = 6;       // LoRa radio reset
  6. const int irqPin = 1;         // change for your board; must be a hardware interrupt pin
  7.  
  8. String outgoing;              // outgoing message
  9.  
  10. byte msgCount = 0;            // count of outgoing messages
  11. byte localAddress = 0xBB;     // address of this device
  12. byte destination = 0xFF;      // destination to send to
  13. long lastSendTime = 0;        // last send time
  14. int interval = 2000;          // interval between sends
  15.  
  16. void setup() {
  17.   Serial.begin(9600);                   // initialize serial
  18.   while (!Serial);
  19.  
  20.   Serial.println("LoRa Duplex");
  21.  
  22.   // override the default CS, reset, and IRQ pins (optional)
  23.   LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
  24.  
  25.   if (!LoRa.begin(915E6)) {             // initialize ratio at 915 MHz
  26.     Serial.println("LoRa init failed. Check your connections.");
  27.     while (true);                       // if failed, do nothing
  28.   }
  29.  
  30.   Serial.println("LoRa init succeeded.");
  31. }
  32.  
  33. void loop() {
  34.   if (millis() - lastSendTime > interval) {
  35.     String message = "HeLoRa World!";   // send a message
  36.     sendMessage(message);
  37.     Serial.println("Sending " + message);
  38.     lastSendTime = millis();            // timestamp the message
  39.     interval = random(2000) + 1000;    // 2-3 seconds
  40.   }
  41.  
  42.   // parse for a packet, and call onReceive with the result:
  43.   onReceive(LoRa.parsePacket());
  44. }
  45.  
  46. void sendMessage(String outgoing) {
  47.   LoRa.beginPacket();                   // start packet
  48.   LoRa.write(destination);              // add destination address
  49.   LoRa.write(localAddress);             // add sender address
  50.   LoRa.write(msgCount);                 // add message ID
  51.   LoRa.write(outgoing.length());        // add payload length
  52.   LoRa.print(outgoing);                 // add payload
  53.   LoRa.endPacket();                     // finish packet and send it
  54.   msgCount++;                           // increment message ID
  55. }
  56.  
  57. void onReceive(int packetSize) {
  58.   if (packetSize == 0) return;          // if there's no packet, return
  59.  
  60.   // read packet header bytes:
  61.   int recipient = LoRa.read();          // recipient address
  62.   byte sender = LoRa.read();            // sender address
  63.   byte incomingMsgId = LoRa.read();     // incoming msg ID
  64.   byte incomingLength = LoRa.read();    // incoming msg length
  65.  
  66.   String incoming = "";
  67.  
  68.   while (LoRa.available()) {
  69.     incoming += (char)LoRa.read();
  70.   }
  71.  
  72.   if (incomingLength != incoming.length()) {   // check length for error
  73.     Serial.println("error: message length does not match length");
  74.     return;                             // skip rest of function
  75.   }
  76.  
  77.   // if the recipient isn't this device or broadcast,
  78.   if (recipient != localAddress && recipient != 0xFF) {
  79.     Serial.println("This message is not for me.");
  80.     return;                             // skip rest of function
  81.   }
  82.  
  83.   // if message is for this device, or broadcast, print details:
  84.   Serial.println("Received from: 0x" + String(sender, HEX));
  85.   Serial.println("Sent to: 0x" + String(recipient, HEX));
  86.   Serial.println("Message ID: " + String(incomingMsgId));
  87.   Serial.println("Message length: " + String(incomingLength));
  88.   Serial.println("Message: " + incoming);
  89.   Serial.println("RSSI: " + String(LoRa.packetRssi()));
  90.   Serial.println("Snr: " + String(LoRa.packetSnr()));
  91.   Serial.println();
  92. }

chegewara
Posts: 2376
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 LoRa two way radio communication

Postby chegewara » Thu Feb 04, 2021 1:21 pm

Sorry, my bad. I assumed you are using different library, and i dont know that one, so i cant help.

Who is online

Users browsing this forum: No registered users and 39 guests