sandeepmistry / arduino-LoRa

An Arduino library for sending and receiving data using LoRa radios.
MIT License
1.6k stars 621 forks source link

Lora and Serial Port #659

Open arifainchtein opened 10 months ago

arifainchtein commented 10 months ago

Hello, I have a custom made board with Ra-02 and Esp32. I connect this board to a raspberry pi via usb. This board acts as a receiver of data from two other boards also with esp32 and Ra-02. These senders each send a struct to the receiver in either 10 sec interval for Sender 1 and 30 seconds interval for sender 2.

In the pi i run a program that sends Serial port messages asking for data. These request happen every 10 seconds or so. When a request for data arrives in the esp32 it serializes the s1 and s2 structs and sends them via the serial port.

The problem i am having is that if Lora receives a message while the serial port is active the esp 32 crashes. Otherwise it works perfectly. I have tried setting lora as transmission when receiving Serial commands (so as to make sure it does not receive) and then setting it to receive when the serial port transaction is happening but that did not seem to fix the issue.

I also tried setting NoInterrupts() and Interrupts() at the beginning and end of the serial transaction and that literally crash the esp32 and it rebooted itself. Is there a way to turn on and off the Lora radio so that i can insure that the radio is off when i am doing serial communications?

here is the code that receives the lora message:

void processLora(int packetSize){
  receivingLora=true;
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("rec lora:");
  lcd.print( packetSize);
  if (packetSize == 0) return; 
  if(inSerial){
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("In Serial");
    return;
  }
   if(!timeSet){
     lcd.clear();
     lcd.setCursor(0,2);
      lcd.print("currentTimerRecord is null");
      return;
    }
    int hour = currentTimerRecord.hour;
    int minute = currentTimerRecord.minute;
    int displaytime = (hour * 100) + minute;
    if(packetSize==sizeof(GloriaTankFlowPumpData)){
      memset(&gloriaTankFlowPumpData, 0, sizeof(GloriaTankFlowPumpData));
      LoRa.readBytes((uint8_t*)&gloriaTankFlowPumpData,sizeof(GloriaTankFlowPumpData));
      lcd.setCursor(0,2);
      lcd.print("GTF:");
      lcd.print(gloriaTankFlowPumpData.devicename);
       gloriaTankFlowPumpData.rssi = LoRa.packetRssi();
      gloriaTankFlowPumpData.snr = LoRa.packetSnr();
      lcd.setCursor(0,3);
       lcd.print("sn:");
      lcd.print(gloriaTankFlowPumpData.snr);
      lcd.print("  rs:");
      lcd.print(gloriaTankFlowPumpData.rssi);
      long messageReceivedTime = timeManager.getCurrentTimeInSeconds(currentTimerRecord);
      lastReceptionRTCInfoRecord.year = currentTimerRecord.year;
      lastReceptionRTCInfoRecord.month = currentTimerRecord.month;
      lastReceptionRTCInfoRecord.date = currentTimerRecord.date;
      lastReceptionRTCInfoRecord.hour = currentTimerRecord.hour;
      lastReceptionRTCInfoRecord.minute = currentTimerRecord.minute;
      lastReceptionRTCInfoRecord.second = currentTimerRecord.second;
      gloriaTankFlowPumpData.secondsTime=messageReceivedTime;
      const uint8_t glorianame[] = {
          SEG_A | SEG_C | SEG_D| SEG_E | SEG_F | SEG_G ,  // G
          SEG_D | SEG_E | SEG_F,  // L
          SEG_F | SEG_D | SEG_A | SEG_B | SEG_C| SEG_E,  // O
          SEG_E | SEG_G  // r
      };
      display1.setSegments(glorianame, 4, 0);
      display2.clear();
      display3.showNumberDec(gloriaTankFlowPumpData.rssi, false);
      //display4.showNumberDec(gloriaTankFlowPumpData.snr, false);  
      //  display5.showNumberDec(displaytime  , false);
      display4.showNumberDec(4444, false);
      display5.showNumberDec(5555  , false);
      display6.showNumberDec(6666, false);
      gloriaTankFlowPumpNewData=true;
     }else  if(packetSize==sizeof(PanchoTankFlowData)){
        memset(&panchoTankFlowData, 0, sizeof(PanchoTankFlowData));
       LoRa.readBytes((uint8_t*)&panchoTankFlowData,sizeof(PanchoTankFlowData));
       lcd.setCursor(0,2);
      lcd.print("PTF:");
      lcd.print(panchoTankFlowData.devicename);
      panchoTankFlowData.rssi = LoRa.packetRssi();
      panchoTankFlowData.snr = LoRa.packetSnr();
    // Serial.println(panchoTankFlowData.secondsTime);
    // Serial.println(panchoTankFlowData.rssi);
    // Serial.println(panchoTankFlowData.snr);
        const uint8_t panchoname[] = {
              SEG_A | SEG_B | SEG_E| SEG_F | SEG_G,  // P
              SEG_A | SEG_B | SEG_C | SEG_E | SEG_F| SEG_G,  // A
              SEG_C | SEG_E | SEG_G,  // n
              SEG_D | SEG_E | SEG_G  // c
      };
    display1.setSegments(panchoname, 4, 0);
        display2.clear();
        display3.showNumberDec(panchoTankFlowData.rssi, false);
        display5.showNumberDec(displaytime, false);
        int startValue=0;
        int endValue=4;
        for(int i=startValue;i<endValue;i++){
            if(panchoTankFlowData.flowRate>0){
                leds[i] = CRGB(0, 0, 255);
            }else {
                leds[i] = CRGB(0, 0, 0);
            }
        }
    FastLED.show();
    delay(1000);
    for(int i=0;i<8;i++){
        leds[i] = CRGB(0, 0, 0);
    }
    FastLED.show();
      }else{
        badPacketCount++;
        lcd.print("  receive bad data size= ");
        lcd.print(packetSize);
      }
      loraPacketSize=0;
      receivingLora=false;
}

Do you have any suggestions as to why or how to fix this issue?

thanks

keithr0 commented 10 months ago

On 12/08/2023 6:33 pm, arifainchtein wrote:

Hello, I have a custom made board with Ra-02 and Esp32. I connect this board to a raspberry pi via usb. This board acts as a receiver of data from two other boards also with esp32 and Ra-02. These senders each send a struct to the receiver in either 10 sec interval for Sender 1 and 30 seconds interval for sender 2.

In the pi i run a program that sends Serial port messages asking for data. These request happen every 10 seconds or so. When a request for data arrives in the esp32 it serializes the s1 and s2 structs and sends them via the serial port.

The problem i am having is that if Lora receives a message while the serial port is active the esp 32 crashes. Otherwise it works perfectly. I have tried setting lora as transmission when receiving Serial commands (so as to make sure it does not receive) and then setting it to receive when the serial port transaction is happening but that did not seem to fix the issue.

I also tried setting NoInterrupts() and Interrupts() at the beginning and end of the serial transaction and that literally crash the esp32 and it rebooted itself. Is there a way to turn on and off the Lora radio so that i can insure that the radio is off when i am doing serial communications?

Do you have any suggestions as to why or how to fix this issue?

thanks

I have a couple of suggestions, both involve a re-design.

1) The ESP-32 has 2 cores, separate the LoRa, and RasPi tasks one to each core. The tasks can communicate in several ways, by global variables is the simplest.

2) Change to a polling system. At the moment you have 3 asynchronous events, and there is a high  probability of a clash, instead use a system where the RasPi requests information, your board passes the request to the appropriate sensor, and passes the result back to the RasPi. This simplifies the code in your board as it become a simple serial -  LoRa - serial converter.  The RasPi would just need to specify the sensor that it wished to interrogate, and the number of bytes expected. It would also mean that, if the structure of the data was to change for some reason, there would be no changes needed to this board.