LoRa: Initializarion problem that follows the code and not the hardware

Hi Folks:

This stumped ChatGPT (paid version). You will see this one is weird! Any help i appreciated.

// Sender.ino

/*********
Modified from the examples of the Arduino LoRa library
More resources: https://randomnerdtutorials.com
*********/

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

//define the pins used by the transceiver module
#define ss 5
#define rst 14
#define dio0 2

int counter = 0;

void setup() {
//initialize Serial Monitor
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Sender");

//setup LoRa transceiver module
LoRa.setPins(ss, rst, dio0);

//replace the LoRa.begin(---E-) argument with your location's frequency
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
while (!LoRa.begin(915E6)) {
Serial.println(".");
delay(500);
}
// Change sync word (0xF3) to match the receiver
// The sync word assures you don't get LoRa messages from other LoRa transceivers
// ranges from 0-0xFF
LoRa.setSyncWord(0xF3);
Serial.println("LoRa Initializing OK!");
}

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

//Send LoRa packet to receiver
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter);
LoRa.endPacket();

counter++;

delay(10000);
}

// Receiver.ino /********* Modified from the examples of the Arduino LoRa library More resources: https://randomnerdtutorials.com *********/

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

//define the pins used by the transceiver module
#define ss 5
#define rst 14
#define dio0 2

void setup() {
//initialize Serial Monitor
Serial.begin(115200);
while (!Serial);
Serial.println("LoRa Receiver");

//setup LoRa transceiver module
LoRa.setPins(ss, rst, dio0);

//replace the LoRa.begin(---E-) argument with your location's frequency
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
while (!LoRa.begin(915E6)) {
Serial.println(".");
delay(500);
}
// Change sync word (0xF3) to match the receiver
// The sync word assures you don't get LoRa messages from other LoRa transceivers
// ranges from 0-0xFF
LoRa.setSyncWord(0xF3);
Serial.println("LoRa Initializing OK!");
}

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

// read packet
while (LoRa.available()) {
  String LoRaData = LoRa.readString();
  Serial.print(LoRaData); 
}

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

}
}

Sender.ino prints Sending packet: and a monotonically increasing number every 10 seconds
Receiver.ino prints a . every 500ms. It never gets out of the inicialization loop:
while (!LoRa.begin(866E6)) {
Serial.println(".");
delay(500);
}
The two devices are inches apart on my desk
I am in a place that little 915MHz communications occurs.

To see if it might be hardware related I switched the roles, flashing the Receiver code on what was the Sender and the Sender code onto what was the Receiver.
The output remained the same, the problem follows the program, not the hardware.
I even did the switching among 3 boards changing the roles and the boards. The problem followed the program every single time.

You can see that both parts of the setup differ in only the Serial.print statement. It would be expected that the programs, in that section, would behave identically.
You can see the sync word is not being changed anywhere in the code
You ca see that SPI is use only for LoRa, nothing else oth programs are flashed from the exact same Arduino IDE bersion 2.3.1 typically seconds apart so board definitions, software versions and libraries are exactly the same.
Both are using "ESP32 Dev Module"

What is wrong

Do the boards work when you use the Sender and Receiver examples from the LoRa.h library ?

Best to start from known working examples, whoever wrote the example code above does not fully undestand LoRa .......................

The exaples come from: Files / Examples / Examples From Custom Libraries / Lora / Lora Sender y el LoRa REceiver.
I added some print statements:

#include #include

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

Serial.println("LoRa Receiver");

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

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());

}
}

#include #include

int counter = 0;

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

Serial.println("LoRa Sender");

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

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

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

counter++;

delay(5000);
}

The receiver prints this:
16:18:01.930 -> LoRa Receiver
16:18:02.668 -> LoRa Receiver
16:18:03.413 -> LoRa Receiver
16:18:04.155 -> LoRa Receiver

The ender prints this:

16:01:17.690 -> ets Jun 8 2016 00:22:57
16:01:17.690 ->
16:01:17.690 -> rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
16:01:17.690 -> configsip: 0, SPIWP:0xee
16:01:17.690 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
16:01:17.690 -> mode:DIO, clock div:1
16:01:17.690 -> load:0x3fff0030,len:1344
16:01:17.690 -> load:0x40078000,len:13964
16:01:17.690 -> load:0x40080400,len:3600
16:01:17.723 -> entry 0x400805f0
16:01:17.819 -> LoRa Sender
16:01:18.720 -> ets Jun 8 2016 00:22:57

Again, if I switch the programs on the boards, the problem moves with the code.

What blows my mind is that the setup() is funtionally identical.

In both cases, as the problem moves with the code I have to assume it is something in the code.

Any help would be appreciated.

So what happens when you use the original examples, with nothing changed or added, apart from the required pin definitions ?

The same things except the added statements that were missing did not print anything :slight_smile: .
The same thing, I added the print statements in an effort to see where the problems were.

I actually pounded rather hard on ChatGPT in the debugging. It stumped ChatGPT.

The 'LoRaSender' example form the library you are using does work on an ESP32.

When posting code please use the quotes, it makes code a lot easier to read, details found here;

I dont see a LoRa.setPins() command, thats normally needed to tell the library which pins on the ESP32 you have the LoRa module connected too.

HI and thanks for answering, There are actually 2 sets of examples. The one I started with(first post) that has set pins. I was then asked a question about if I had used the basic examples, which I had, and I paced those in the second post.

If you would be so kind as to review the code in the first post I would appreciate it.

Could you first edit the post so the code is in a readable format, you need to use the code quotes when posting code, described in the link I posted in #7.

I hope this is better:

This stumped ChatGPT (paid version). You will see this one is weird! Any help i appreciated.

// Sender.ino
/*********
Modified from the examples of the Arduino LoRa library
More resources: https://randomnerdtutorials.com 1
*********/

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

//define the pins used by the transceiver module
#define ss 5
#define rst 14
#define dio0 2

int counter = 0;

void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  while (!Serial);
  Serial.println("LoRa Sender");

  //setup LoRa transceiver module
  LoRa.setPins(ss, rst, dio0);

  //replace the LoRa.begin(---E-) argument with your location's frequency
  //433E6 for Asia
  //866E6 for Europe
  //915E6 for North America
  while (!LoRa.begin(915E6)) {
    Serial.println(".");
    delay(500);
  }
  // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");
}

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

  //Send LoRa packet to receiver
  LoRa.beginPacket();
  LoRa.print("hello ");
  LoRa.print(counter);
  LoRa.endPacket();

  counter++;

  delay(10000);
}
// Receiver.ino /********* Modified from the examples of the Arduino LoRa library More resources: https://randomnerdtutorials.com *********/
#include <SPI.h>
#include <LoRa.h>

//define the pins used by the transceiver module
#define ss 5
#define rst 14
#define dio0 2

void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  while (!Serial);
  Serial.println("LoRa Receiver");

  //setup LoRa transceiver module
  LoRa.setPins(ss, rst, dio0);

  //replace the LoRa.begin(---E-) argument with your location's frequency
  //433E6 for Asia
  //866E6 for Europe
  //915E6 for North America
  while (!LoRa.begin(915E6)) {
    Serial.println(".");
    delay(500);
  }
  // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");
}

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

    // read packet
    while (LoRa.available()) {
      String LoRaData = LoRa.readString();
      Serial.print(LoRaData); 
    }

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

Sender.ino prints Sending packet: and a monotonically increasing number every 10 seconds

Receiver.ino prints a . every 500ms. It never gets out of the inicialization loop:

  while (!LoRa.begin(866E6)) {
    Serial.println(".");
    delay(500);
  }

The two devices are inches apart on my desk

I am in a place that little 915MHz communications occurs.

To see if it might be hardware related I switched the roles, flashing the Receiver code on what was the Sender and the Sender code onto what was the Receiver.

The output remained the same, the problem follows the program, not the hardware.

I even did the switching among 3 boards changing the roles and the boards. The problem followed the program every single time.

You can see that both parts of the setup differ in only the Serial.print statement. It would be expected that the programs, in that section, would behave identically.

You can see the sync word is not being changed anywhere in the code

You ca see that SPI is use only for LoRa, nothing else oth programs are flashed from the exact same Arduino IDE bersion 2.3.1 typically seconds apart so board definitions, software versions and libraries are exactly the same.

Both are using "ESP32 Dev Module"

What is wrong

No idea, the code should work.

The Sender and Receiver code looks the same up the the LoRa.begin(); part, but you say one fails and one does not.

I would not myself use pin GPIO2 for a LoRa module, its a pin used by the PSRAM if fitted.

I dont agree with this bit;

  // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);

Leave the syncword alone, just use the two standard options that are known to work reliably , 0x12 and 0x34. Its not true that the syncword can be any value from 0 to 0xFF. Some syncwords values can;

Not work at all.
Be leaky in that packets with different syncwords than the receiver is using are still received.
Some syncwords result in reduced LoRa sensitivity.

But that is not the source of the problem.

Which LoRa devices are you using ?

SX1276 chips. I have one gateway working with 16 chips in the field. What I am trying to do is to replicate the gateway I have that works.I tried the working code and it does not work I then went for basic and other simple code. Whatever I do, the device assigned the receiver code fails.

It's like Einstein's "spooky action at a distance". The code beyond the setup has not executed but it seems to affect the setup code.

PLease consider this closed

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.