无法接收通过 LoRa 发送的第二条或第三条消息

时间:2021-05-24 16:18:31

标签: esp32 lora

嗨,我正在两个 Lora esp32 设备 (Heltec V2) 之间发送一些传感器读数

第一个数据包被正确接收,但我发送的接下来的两个数据包没有被正确读取。 onReceive 中断被触发,但是当我读取数据包时,我只获得了初始消息的一部分。我确信这是用户错误,但我无法解决。

来自发件人:

  • 消息 1:33.33:739.36:41.64:9
  • 消息 2:1:2:3:4
  • 消息 3:111:222:333:444

收到:

  • 消息 1:33.33:739.36:41.64:9(正确)
  • 消息 2:33.33:7(已损坏)
  • 消息 3:33.33:739.36:41(已损坏)

发射器节点:

void sendMessage(byte localAddress_t, byte destination, String outgoing) {
  while (!LoRa.beginPacket())
  {
    Serial.println("sendMessage: Lora Busy!");
    delay(20);
  }
  Serial.println("sendMessage: Lora free");
  LoRa.write(destination);              // add destination address
  LoRa.write(localAddress_t);             // add sender address
  LoRa.write(msgCount);                 // add message ID
  LoRa.write(outgoing.length());        // add payload length
  LoRa.print(outgoing);                 // add payload
  if (LoRa.endPacket())
  {
    Serial.println("sendMessage: send success");
  } else {
    Serial.println("sendMessage: send fail");
  }
  // finish packet and send it
  LoRa.flush();     
  msgCount++;                           // increment message ID 
}
...
...
From loop section:
      String sensorData = String(temperature) + ":" + String(pressure) + ":" + String(humidity) + ":" + String(soil);
      Serial.println("\n\n------\n\n");
      sendMessage(localAddress, destination,sensorData);
      Serial.println("\n\n------\n\n");
      delay(1500);         
      int T1 = 1;
      int P1 = 2;
      int H1 = 3;
      int S1 = 4;
      sensorData = String(T1) + ":" + String(P1) + ":" + String(H1) + ":" + String(S1);
      sendMessage(tmpAddress, destination,sensorData);
      Serial.println("\n\n------\n\n");
      delay(1000);
      T1 = 111;
      P1 = 222;
      H1 = 333;
      S1 = 444;
      sensorData = String(T1) + ":" + String(P1) + ":" + String(H1) + ":" + String(S1);
      sendMessage(tmpAddress1, destination,sensorData);
      Serial.println("\n\n------\n\n");

接收节点:

void onReceive(int packetSize) {
  digitalWrite(25, HIGH); 
  Serial.println("\n-------------\nBasestation: onReceive Triggered: " + String(packetSize) +"\n");
  #endif
  if (packetSize == 0) return;          // if there's no packet, return
  packetReceived = packetSize;
}
 
void readLora() {
  Serial.println("> readLora - Start");
  // 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
  Serial.print("Msg Length: ");
  Serial.println(incomingLength);
  
  String incoming = LoRa.readString();
  // I have tried 2 methods to read the data but the result is the same
  /*while (LoRa.available()) {            // can't use readString() in callback, so
    String tmp = "";
    tmp += (char)LoRa.read(); 
    //incoming += (char)LoRa.read();      // add bytes one by one
    incoming += tmp;
    Serial.println("LoRa.read = "+tmp);
  }
  LoRa.flush();
  */

  // Get readingID, temperature and soil moisture
  int pos0    = incoming.indexOf(':');
  int pos1    = incoming.indexOf(':',pos0+1);
  int pos2    = incoming.indexOf(':',pos1+1);
  int pos3    = incoming.indexOf(':',pos2+1);
  int pos4    = incoming.indexOf(':',pos3+1);
  int pos5    = incoming.indexOf(':',pos4+1);

  temperature = incoming.substring(0, pos0).toDouble();
  pressure    = incoming.substring(pos0+1, pos1).toDouble();    
  humidity    = incoming.substring(pos1+1, pos2).toDouble();
  soil        = incoming.substring(pos2+1, incoming.length()).toDouble();
}

void loop() {
  if (packetReceived  > 0) {
    readLora();
    packetReceived = 0;
       
    digitalWrite(25, LOW);
    LoRa.receive();
}

0 个答案:

没有答案