LoRa unable to read reply from Rx unit

Hi Everyone.
Using Nanos and LoRa RA-01.

I am sending 3 packets of data to a LoRa receiver.

The one packet is the unit Id which is 102 , I can receive and print the 102 and test if 102 was received.

Then when the unit id is 102 I switch on a LED to indicate a valid id Received.

//------- Up to here all works fine ........

The problem stars here.

When the Rx id is confirmed I send 102 back to the Transmitter but the Transmitter never seem to goto to "while (LoRa.available())".

This is how I test what lines is executed.

Serial.println("1");

int packetSize = LoRa.parsePacket(); // try to parse packet

  Serial.println("2");
  if (packetSize)
  {
    Serial.println("3");
    while (LoRa.available())
    {
      Serial.println("4");
      incoming = ((int)LoRa.read());//char
    }
  }

Once in a BLUE MOON running for a long time the Led on the Tx unit will light up indicating that the id was received.

Both my pcb is exactly the same so I also swap the 2 Nanos (swap Rx and Tx sketch) and then only the Rx unit will switch on the Led.

Thanks for any help in advance.

//7 12 2021
//After TX data is send unit will wait for Rx data and then -
//If RX data (ack)match unit ID LED will switch on.
//No ack received LED will switch off.


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



int incoming = 0;
int counterA = 0;
int counterB = 0;
long int unitId = 102;
int analog = 128;
int switch1 = 0;
int switch2 = 0;
long int data = 0;
int digital = 10;

void setup()
{
  pinMode(3, OUTPUT); //ack LED

  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRaTx Telem01");

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

void loop()
{
  counterA = counterA + 1;//TX Interval Timer
  counterB = counterB + 1;//no ack Timer

  if (counterB == 20)
  {
    digitalWrite(3, LOW); //LED OFF
    LoRa.begin(433E6);//Restart Lora
    delay(1000);
  }
  if (counterA == 5)//Send packet every 5 Seconds
  {
    // send packet
    LoRa.beginPacket();
    LoRa.write(unitId);//
    LoRa.write(analog);//
    LoRa.write(digital);//
    // LoRa.print(analog);//
    LoRa.endPacket();
    counterA = 0;//clear counterA to start 5Sec timer
  }
  //---- Wait for acknow ---------------

  int packetSize = LoRa.parsePacket(); // try to parse packet

  if (packetSize)
  {
    while (LoRa.available())
    {
      incoming = ((int)LoRa.read());//char
    }
  }

  Serial.print(incoming);
  if (incoming == (unitId))
  {
    digitalWrite(3, HIGH);
    incoming = 0;//clear value
    counterB = 0;//Reset counterB 20 SEC Counter
  }

  delay(1000);

}

//7 12 2021
//When received unit ID match , unit will respond and TX unit ID back ..Led on
//LED will switch off if no data is received in ~~20 Seconds

#include <SPI.h>
#include <LoRa.h>
#include <LiquidCrystal.h> //Arduino standard LCD Library

//
//                RS   E  D4  D5  D6  D7
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

//const unsigned int numReadings = 10;
unsigned int myArray[3];

const unsigned long eventInterval = 1000;//used in millis
unsigned long previousTime = 0;//used in millis

int incoming ;
int rxData;
int i = 0;
int value = 0;
int analog = 0;
int unitId = 102;
int digital = 0;
int readFlag = 1;
int idFlag = 0;//0 = not valid
int counter = 0;
int rxId = 0;

void setup()
{
  //------ Configure I/O Pins -------
  pinMode(3, OUTPUT); //Test LED

  //------ Test LED ---------------
  digitalWrite(3, HIGH);
  delay(500);
  digitalWrite(3, LOW);

  //------ Setup Serial & LCD -----
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("LoraRx Telem01");//This works
  delay(500);
  lcd.clear();
  lcd.setCursor(0, 0);

  //-----Configure LoRa -----------
  while (!Serial);

  Serial.println("LoRaRxTelem01");

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

  //------ END of Setup -----------
}

void loop()
{
  unsigned long currentTime = millis();
  if (currentTime - previousTime >= eventInterval)//eventInterval = 1000
  {
    counter = counter + 1;//increment counter every ~~ 1sec
    previousTime = currentTime;
  }


  if (counter > 10)//Clear counter in LoRa.Read
  {
    digitalWrite(3, LOW);
    LoRa.begin(433E6);//Restart Lora
    delay(1000);
  }

  i = 0;//start of Array

  int packetSize = LoRa.parsePacket(); // try to parse packet

  if (packetSize)
  {
    while (LoRa.available())
    {
      counter = 0;//Reset Counter valid read was done
      readFlag = 0;
      incoming = ((int)LoRa.read());//char


      //----- Save Read packets in Array --------------
      myArray[i] = incoming ;//save and loop until packet == size
      i = i + 1;             //Increment Array pointer until pacckets is Read
    }
  }
  //------- Done reading packets ----------------------
  //-------
  if (readFlag == 0)//ReadFlag is "0" when new packet is Read.Prevent looping in Array Read
  {
    i = 0;
    rxId = ( myArray[i]);//1
    Serial.println();
    Serial.print ("rxid  ");
    Serial.print (rxId);
    if (rxId == unitId)
    {
      digitalWrite(3, HIGH);//LED on when id is valid
      idFlag = 1;           //Set Flag id is valid, will reply id/2
    }

    i = i + 1;
    analog = ( myArray[i]);//2
    Serial.println();
    Serial.print ("analog  ");
    Serial.print (analog);


    i = i + 1;
    digital = ( myArray[i]);//3
    Serial.println();
    Serial.print ("digital  ");
    Serial.print (digital);
    Serial.println();

    readFlag = 1;//Array read done prevent looping until new packet is received
  }
  //---- id is valid answer back with id -------
  if (idFlag == 1)
  {
    LoRa.beginPacket();
    LoRa.write(unitId ); //
    LoRa.endPacket();
    idFlag = 0;//Reset idFlag for next read
    //Serial.print(unitId);
  }

}

So why not check if it does ?

Add a debug message such as;

Serial.println("Listening");

Just before the;

while (LoRa.available());

I did test it with a lot of Serial.println("1"); and 2 and 3 and 4 .

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