LoRaData.startsWith() giving LoRaData is not declared in this scope

Hello..

I'm using a Lora node to node communication and everything works. Now i have added a code in receiver to send acknowledgement to sender.
In return the transmitter sends it a transmission delay value. And at that point i want the communication to stop.

For this i want to check the LoRa packet if it starts with "Time:....."

But it gives error: LoRaData was not declared in this scope..

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

#define ss 10 
#define rst 9 
#define dio0 2 

void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("LoRa Receiver");
  LoRa.setPins(ss, rst, dio0);
  while (!LoRa.begin(866E6)){
    Serial.println(".");
    delay(500);  
  }
  Serial.println("tocheck");
//
//  if (!LoRa.begin(866E6)) {
//    Serial.println("Starting LoRa failed!");
//    while (1);
//  }
}

void loop() {

  int packetSize = LoRa.parsePacket();
  if (packetSize){
    Serial.println("Received packet ");

    while (LoRa.available()){
      String LoRaData = LoRa.readString();
      Serial.println(LoRaData);
    }
    if (LoRaData.startsWith("Time")){
        Serial.println("Finished");
      }
     else {
         Serial.println("' with RSSI ");
         Serial.println(LoRa.packetRssi());
         LoRa.beginPacket();
         LoRa.print("received");
         Serial.println("sent acknowledgement");
         LoRa.endPacket(); 
  }

}
}

Any ideas how to fix it?

LoRaData is local to the while loop; I suggest that you read up on scope.

Welcome to the forum

    while (LoRa.available())
    {
      String LoRaData = LoRa.readString();
      Serial.println(LoRaData);
    }
    if (LoRaData.startsWith("Time"))

The LoRaDara String is declared in the while loop so is out of scope when the while loop ends

This was my first guess.. and i tried declaring it in setup. It still doesn't work for me...

As said, read up on scope. Declared in setup() makes that it's not known in loop().

You can make it global or use something like

String LoRaData;
while (LoRa.available())
{
  LoRaData = LoRa.readString();
  Serial.println(LoRaData);

And now head over to google or whatever and start learning about scope.

I declared LoRaData outside of setup and it worked.. silly mistake though!

There is a LoRa library that includes transmit with acknowledge functions as part of the library.

I guess that you know by now why that didn't work

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