How to execute commands only when new LoRa pakage received?

Hi

I dont get my head around how to make a LoRa receiver code execute commands only when new LoRa paket is red and content of pakage contains certain defined data.

I understand that readpacket() must run continuously to receive all packages, but my functions that is based on 'two demands = true' execute other commands, like write to cayenne, and to oled, and to ftp etc.

I receive a LoRa packet that has this data:

String LoRaMessage = String(packetID) + "/" + String(temperature) + "&" + String(humidity) + "£" + String(weight_median) + "#" +  String(alarmValue) + "%" + String(hiveNr);

I ask for some help to set up a if (or while) statement in void loop() that goes like this:

When new packet red and packet contains hiveNr==1 and packetID is bigger than previous package then run function:
Cayenne.loop();

I have tried a bit but not finished, here is code to check change in paketID:

if (packetID_i > previousPacketID_i) {
    Cayenne.loop();
}

PacketID is from this parsing function:

void readPacket() {
  //try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize)  {
    while (LoRa.available())
    {
      LoRaMessage = LoRa.readString();
      
      previousPacketID_i = packetID.toInt();
      
      int pos1 =    LoRaMessage.indexOf('/'); //PacketID
      int pos2 =    LoRaMessage.indexOf('&'); //temp
      int pos3 =    LoRaMessage.indexOf('£'); //hum
      int pos4 =    LoRaMessage.indexOf('#'); //vekt_median
      int pos5 =    LoRaMessage.indexOf('%'); // alarm
      int pos6 =    LoRaMessage.indexOf('@'); // hiveNr - not send to cayenne

      packetID =    LoRaMessage.substring(0, pos1);
      temperature = LoRaMessage.substring(pos1 + 1, pos2);
      humidity =    LoRaMessage.substring(pos2 + 1, pos3);
      weight =      LoRaMessage.substring(pos3 + 1, pos4);
      alarmValue =  LoRaMessage.substring(pos4 + 1, pos5);
      hiveName =    LoRaMessage.substring(pos5 + 1, LoRaMessage.length());

      // convert to floatInt before sending to Cayenne
      packetID_i        = packetID.toInt();
      temperature_f     = temperature.toFloat();
      humidity_f        = humidity.toFloat();
      weight_f          = weight.toFloat();
      //tareValue_i     = tareValue.toInt();
      alarmValue_i      = alarmValue.toInt();
      hiveName_i        = hiveName.toInt();
    }
  }
}

what Arduino are you using and what Lora modules?
what LoRa library are you using?

Does the transmitter send the same packet more than once ?

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