nkolban / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
2.35k stars 710 forks source link

Semaphore waiting stuck in getservices() #708

Open lvronghai opened 5 years ago

lvronghai commented 5 years ago

Hello,

I was trying ESP32 acts as BLE clients to three BLE sensors. It worked nicely until I started the long period testing. Sometimes, it seems it will get stuck with semaphore waiting. Here is debug log. I am new to esp32 and arduino and studying the code as well.

= ESP32> Retrieve data from BLE Sensors, D (334836) FreeRTOS: Semaphore taking: name: RegEvt (0x3ffe84cc), owner: <N/A> for connect D (334836) FreeRTOS: Semaphore taken: name: RegEvt (0x3ffe84cc), owner: connect V (334843) FreeRTOS: >> wait: Semaphore waiting: name: RegEvt (0x3ffe84cc), owner: connect for connect V (334844) FreeRTOS: Semaphore giving: name: RegEvt (0x3ffe84cc), owner: connect V (334859) FreeRTOS: << wait: Semaphore released: name: RegEvt (0x3ffe84cc), owner: connect D (334867) FreeRTOS: Semaphore taking: name: OpenEvt (0x3ffe8980), owner: <N/A> for connect D (334875) FreeRTOS: Semaphore taken: name: OpenEvt (0x3ffe8980), owner: connect V (334883) FreeRTOS: >> wait: Semaphore waiting: name: OpenEvt (0x3ffe8980), owner: connect for connect V (335659) FreeRTOS: Semaphore giving: name: RssiCmplEvt (0x3ffe8734), owner: <N/A> V (335660) FreeRTOS: Semaphore giving: name: SearchCmplEvt (0x3ffe86d4), owner: <N/A> V (335664) FreeRTOS: Semaphore giving: name: OpenEvt (0x3ffe8980), owner: connect V (335671) FreeRTOS: << wait: Semaphore released: name: OpenEvt (0x3ffe8980), owner: connect

  • Connected to server c4:7c:8d:64:72:df D (340684) FreeRTOS: Semaphore taking: name: SearchCmplEvt (0x3ffe86d4), owner: <N/A> for getServices D (340684) FreeRTOS: Semaphore taken: name: SearchCmplEvt (0x3ffe86d4), owner: getServices V (340690) FreeRTOS: >> wait: Semaphore waiting: name: SearchCmplEvt (0x3ffe86d4), owner: getServices for getServices

There are three sensors, it stucked in the first sensor getServices. The code: // Connect to the remove BLE Server. pClient->connect(pAddress); Serial.print(" - Connected to server "); Serial.println(sensor_mac); delay(5000); //Obtain a reference to the service we are after in the remote BLE server. BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService == nullptr) { Serial.print(" Failed to find our service UUID: "); Serial.println(serviceUUID.toString().c_str()); return false; } Serial.println(" - Found our service"); Serial.print(" - disConnected to server "); pClient->disconnect(); return true; Yonghai

lvronghai commented 5 years ago

Add the log which in successful search for getServices();

ESP32> Retrieve data from BLE Sensors, D (415404) FreeRTOS: Semaphore taking: name: RegEvt (0x3ffe8704), owner: <N/A> for connect D (415404) FreeRTOS: Semaphore taken: name: RegEvt (0x3ffe8704), owner: connect

V (415411) FreeRTOS: >> wait: Semaphore waiting: name: RegEvt (0x3ffe8704), owner: connect for connect V (415412) FreeRTOS: Semaphore giving: name: RegEvt (0x3ffe8704), owner: connect

V (415427) FreeRTOS: << wait: Semaphore released: name: RegEvt (0x3ffe8704), owner: connect D (415435) FreeRTOS: Semaphore taking: name: OpenEvt (0x3ffe8440), owner: <N/A> for connect D (415443) FreeRTOS: Semaphore taken: name: OpenEvt (0x3ffe8440), owner: connect

V (415451) FreeRTOS: >> wait: Semaphore waiting: name: OpenEvt (0x3ffe8440), owner: connect for connect

V (418065) FreeRTOS: Semaphore giving: name: OpenEvt (0x3ffe8440), owner: connect V (418067) FreeRTOS: << wait: Semaphore released: name: OpenEvt (0x3ffe8440), owner: <N/A>

chegewara commented 5 years ago

I think the problem is not that semaphore is blocking (i may be wrong), the problem is where in your code is getServices() function called.

lvronghai commented 5 years ago

Hello,

Thank you very much for fast response. I provide more detail. I have esp32 + A6 GSM + three BLE sensors. Since esp32 will go deep sleep and power off A6.

  1. So I run BLE scan only in loop first time, to get all the sensor address.

void loop() { loop_count++; if (!BLUETOOTH_OK) { Serial.println(" -- search BLE sensors"); BLEDevice::init(""); BLEScan* pBLEScan = BLEDevice::getScan(); pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setActiveScan(true); pBLEScan->start(BLESCANTIMEOUT); BLUETOOTH_OK=1; }

  1. in loop, it continues with all mobile network registration and mqtt server connection with AT command calls. if it is failed, new loop run will retry.

  2. if everything is ok. the connectToServer is called. a6_serial_response(); CIPSEND_OK=1; Serial.println(" -- ready for sending data"); delay(5000); pClient = BLEDevice::createClient(); for (int i=0; i<deviceAddress.size();i++){ Serial.println(" "); if (!connectToServer(deviceAddress[i])) { Serial.println("failed to connect to the server; there is nothin more we will do."); } else { MQTTpublish(); delay(2000); } } delay(5000); delete pClient; delay(5000);

  3. getServices is called in connectToServer(). bool connectToServer(BLEAddress pAddress) { // Connect to the remove BLE Server. pClient->connect(pAddress); Serial.print(" - Connected to server "); Serial.println(sensor_mac); delay(5000);

    //Obtain a reference to the service we are after in the remote BLE server. BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService == nullptr) { Serial.print(" Failed to find our service UUID: "); Serial.println(serviceUUID.toString().c_str()); return false; } Serial.println(" - Found our service");

    Serial.print(" - disConnected to server "); pClient->disconnect(); return true;

chegewara commented 5 years ago
  1. looks good, its like in examples so all should work;

Sorry, but its hard to say what can cause this issue. Ive been working on library improvements, also semaphores, but at the moment i have no time. Maybe someone else could help with this.

lvronghai commented 5 years ago

Hi,

OK, thanks. At least I can reproduce the problem. I try to get familiar with code and see if I can find root cause.

Best regards! Yonghai.

On Fri, 26 Oct 2018, 17:45 chegewara, notifications@github.com wrote:

  1. looks good, its like in examples so all should work;

Sorry, but its hard to say what can cause this issue. Ive been working on library improvements, also semaphores, but at the moment i have no time. Maybe someone else could help with this.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nkolban/esp32-snippets/issues/708#issuecomment-433432529, or mute the thread https://github.com/notifications/unsubscribe-auth/ANPqUyS7jAOTy1p7GxFkUyLnhKCfUlThks5uox_xgaJpZM4X742F .

zaindroid commented 4 years ago

Hi, I am not able to get services from client at all. I have tried all of the tree methods 1-getServices()
2-getService(const char* uuid) 3- getService(BLEUUID uuid)

In all of the cases esp32 gets stuck and then after a some time onDisconnect() callback is called from clientCallback class.

Can you please guide me here, i am using the latest build on arduino 1.8.12 as per your instruction in https://github.com/nkolban/esp32-snippets.git

lightingghost commented 4 years ago

Hi @lvronghai, I am having the same issue, did you manage to solve the problem?

edge7 commented 3 years ago

same problem here, but it happens randomly

JoFRT commented 2 years ago

Hello, I face to the same problem, but unfortunately for me the BLE_Client sketch stucks always in getService. Has anyone solved this issue since? I hope that you can help me. Thanks for reading

ilektron commented 2 years ago

I'm experiencing this, too. Unfortunately, it only happens when connecting to the BLE Uart example for esp32. When I connect to an NRF chip using their UART code it doen't hang. It appears to me like this might have something to do with the fact that the client isn't advertising the 128 bit primary service UUID where the NRF chip is.

ilektron commented 2 years ago

Whoops, it looks like because I was using a random address to connect instead of public, connect was failing and a disconnect was happening before the getServices function was called.