1

I am using an ESP32 (without arduino board and using arduino IDE ) with bluetooth and communicating with other hardware via serial communication.
Serial communication speed is high (1382400) but it works and I can read few bytes normally. I need to read a large number of bytes (541) in loop to be able to flush the memory of the hardware in question.
Using the following code I can read the bytes and can even perform a number of iterations but usually get the following error after a few iterations:
Guru Meditation Error: Core 1 panic'ed (InstrFetchProhibited). Exception was unhandled.

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#define RXD2 16
#define TXD2 17

BluetoothSerial SerialBT;
HardwareSerial SerialPIC(2); // using esp32 port2

byte recebidoPIC[542]; 
byte recebidoBT[50];

bool conectado;
int pagina;
bool coletaftp;
bool enviar;

void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
  if (event == ESP_SPP_SRV_OPEN_EVT) {
    // pin set to high enable communication to other hardware
    digitalWrite(2, HIGH);
    digitalWrite(5, HIGH);
    conectado = true;
  }

  if (event == ESP_SPP_CLOSE_EVT) {
    digitalWrite(2, LOW);
    digitalWrite(5, LOW);
    conectado = false;
  }
}

void setup() {
  pinMode(5,OUTPUT); // Control pin to enable hardware
  Serial.begin(19200);
  SerialPIC.begin(1382400,SERIAL_8N1,RXD2,TXD2);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println(F("The device started, now you can pair it with bluetooth!"));
  SerialBT.register_callback(callback);
}

int leStringSerialBT()
{
     int cont=0;
     while ( SerialBT.available() > 0 ) {
       recebidoBT[cont++] = SerialBT.read();
       yield();
     }
  return cont;   
}

int leStringSerialPIC()
{
  int cont = 0;
   while ( SerialPIC.available() > 0 ) {
     recebidoPIC[cont++] = SerialPIC.read();
    // delay(0);
     yield(); 
    }
    return cont;
}

void loopcoleta()
{
  if (SerialPIC.available()) {
     int cont=0;

    cont = leStringSerialPIC();

      if (cont < 541)
      {
        Serial.print(F("Nao completou - "));
        Serial.println(cont);

      }
      else{
        Serial.print(F("Completou - "));
        Serial.println(cont);
        Serial.print(F("Iteracao :"));
        Serial.print(pagina);
        pagina++;
      }


  }
}

void loop() {
 // digitalWrite(5,HIGH);

  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }

  if (SerialBT.available()) {
    int cont=-1;
    cont = leStringSerialBT();
    if ( conectado )
    {

       if ((recebidoBT[0]=='$') && (recebidoBT[1]=='f')) 
       {
          coletaftp = true;
          enviar = true;
          //recebidoBT = "";
          pagina = 1;   
       }
       else 
       {

           SerialPIC.write(recebidoBT,cont); 

       }

       if ( (coletaftp) && (enviar)  ) 
       {
          char comando[11];
          int looppagina = pagina;
          int contcoleta=0;
          unsigned int timeinit = millis();
          while ( pagina < 8191 ) 
          {
           sprintf(comando, "$rd 00%04x", pagina);
           SerialPIC.println(comando); 

           loopcoleta();
           delay(0);
          }
          Serial.println(millis() - timeinit);

       }

    }

  }

  if (SerialPIC.available()) {
     int cont=0;

    unsigned int timeprevious = millis();
    cont = leStringSerialPIC();


    Serial.println(millis() - timeprevious);
    Serial.println("");
    Serial.write(recebidoPIC,cont);
    Serial.println("");
    Serial.println(cont);
    SerialBT.write(recebidoPIC,cont);

     cont=0;
 }

 // delay(20);
}

The above code is complete and after some iterations I get the following message (copied from serial monitor)

Iteracao :1 Completou - 541
Iteracao :2 Completou - 541
Iteracao :3 Completou - 541
Iteracao :4 Completou - 541
Iteracao :5 Completou - 541
Iteracao :6 Completou - 541
Iteracao :7 Completou - 541
Iteracao :8 Completou - 541
Iteracao :9 Completou - 541
Iteracao :10 Completou - 541
< list goes ok here >
Iteracao :67 Completou - 541
Iteracao :68 Completou - 541
Iteracao :69 Completou - 541
Iteracao :70 Nao completou - 4
Guru Meditation Error: Core  1 panic'ed (InstrFetchProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x17335c40  PS      : 0x00060330  A0      : 0x800d18bf  A1      : 0x3ffc89d0  
A2      : 0x3ffc50f0  A3      : 0x3f40152f  A4      : 0x0000000a  A5      : 0x0000ff00  
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x800d240f  A9      : 0x3ffc89b0  
A10     : 0x3ffc50f0  A11     : 0x00060320  A12     : 0x400908ef  A13     : 0x3ffc8990  
A14     : 0x00050323  A15     : 0x3ffba470  SAR     : 0x0000001b  EXCCAUSE: 0x00000014  
EXCVADDR: 0x17335c40  LBEG    : 0x4000c28c  LEND    : 0x4000c296  LCOUNT  : 0x00000000  

Backtrace: 0x17335c40:0x3ffc89d0 0x400d18bc:0x3ffc89f0 0x400d18d8:0x3ffc8a10 0x400d1a26:0x3ffc8a30 0x400d2c05:0x3ffc8a60 0x4008e0bd:0x3ffc8a80

Rebooting...

Already tried to put a longer delay () between each iteration, reduce the size of the array and did not solve ...
What could cause this problem in the code I'm using? If you need more details just tell me.

7
  • Guessing, your program throws an exception after reading a short response. So consider your count value to be incorrect. For example, you use count then increment it. So likely it is one too large after reading less than 541 bytes. Or, your string (array of bytes) may not be terminated as expected by the library (BluetoothSerial) you are using.
    – st2000
    Oct 20, 2019 at 18:16
  • You make a fundamental error in your serial reading assumptions. You should read this: majenko.co.uk/blog/reading-serial-arduino
    – Majenko
    Oct 21, 2019 at 10:56
  • @st2000 - i will investigate the code , maybe the line : recebidoBT[cont++] = SerialBT.read(); could cause the problem. Oct 21, 2019 at 13:00
  • @Majenko - i will read the article and look for what i made wrong. Oct 21, 2019 at 13:01
  • 1
    @Majenko - i'm outside today but reading the article , the diference is the while loop. Maybe using for ( int i=0; i<Serial.available() ; i++ ) do the job or reading serial. available to int variable and later compare it. Oct 21, 2019 at 14:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.