-
Notifications
You must be signed in to change notification settings - Fork 715
BLE Central Arduino Example #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Howdy John ... noticed and received. Lets see if we can't get specific on what you'd like to see so that we can deliver the example as close as possible to what you want. As we see here, there is ambiguity in phrases like central and peripheral, so I'm going to spell out what I think you'd like to see and then see how close this comes to what you actually want. You want the ESP32 to be a "client" in that it will perform a scan for a "server" that is continually broadcasting. When it finds a suitable server, it will then connect to that server, enable notifications and start receiving notifications from the server. In addition, the ESP32 will want to read the value of the characteristic and write a new value at any given time. Is that correct? When you confirm or tweak, we'll start writing the sample for your review. |
Hi Neil, wow, that's very kind, thank you. I'm sorry I couldn't help more, I wanted to try to contribute to the examples. Your description is exactly right in every way! It will also make a wonderful central example. |
Okly dokly ... on it my friend. |
I'd be writing Hex byte arrays if that helps |
A first pass at a full BLE Client application running in Arduino has been made available here: The paint is still wet on it and there may very well be problems but built and tested on my system and so far so good. What I'd like you to do is run up the application and give it a whirl. You will need an external BLE Server such as the nRF Connect application. The sample expects a BLE Server with an exposed service with UUID of
That's a lot of function. Usually much more than we put in a "sample". Please have a read of the code and then we can start the discussions on how much is understandable and work together to make what isn't clear clearer for the next person. |
It looks great! I'll throw in my uuids and give it a go tomorrow. Thank you very much, this is a great help. |
Alright, I put my uuids in and set it up to connect by mac address rather than service as the ones I want aren't advertised, and I get this out with no further data: Found our device! address: e9:44:8b:41:d7:2d
|
Howdy John, |
Hello Neil. When a server is detected, the following error occurs and a cyclic reboot. ets Jun 8 2016 00:22:57 rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
Backtrace: 0x4000c3fa:0x3ffde0b0 0x400da0f7:0x3ffde0d0 0x400da4b1:0x3ffde0f0 0x400da575:0x3ffde120 0x400da585:0x3ffde140 0x400d6222:0x3ffde160 0x400d3d0a:0x3ffde180 0x400d3fdd:0x3ffde210 0x400f2336:0x3ffde230 0x400ef188:0x3ffde2b0 Rebooting... |
Howdy, |
This is your example BLE_cient.ino. And as a server NRF connect. |
Where do I need to add a delay? After BLEDevice :: init ("") ? |
What I'll need to do is see if I can recreate to pin it down. I have been testing with debugging on which means that my app is slowed down and hence isn't finding the "race condition". |
I sat down to try and recreate the problem. Using nRF Connect and exactly the sample code in the repo, I ran the test. I must have started the device 50 times. Once (and only once) I got the exact same error as you did. It felt like it was somewhere in the range 20-30. The fact that I saw it once does mean that there is something wrong ... however the fact that I can't reproduce it means its going to be a booger to track down. Experience tells me that we have a programming concept called a "race condition" going on. This means that in our multi-tasking environment something gets released/used before it is finished/ready in another task. Since we have multiple tasks at play and when a task runs is mostly non-deterministic, these become royal pains to diagnose. If I could recreate even 1 in 10 times I'd be happy. Switching on trace is likely not going to help as the act of trace is likely to synchronize things and the problem will never show up. What I'm interested in finding out is why your device keeps hitting the problem every startup. What kind of ESP32 module are you using? I'm using an Espressif DevKit-C. Lets see if you can't liberally sprinkle Serial.println() statements in your source and see if we can't find the last statement entered. That might give us some more hints/clues. If we think it might help, we can maybe arrange a live debug session over the weekend. |
The board is called Wemos Lolin 32 OLED. |
If I set the output #define CONFIG_LOG_DEFAULT_LEVEL 5, then everything works, the characteristic value in the NRF Connect changes. Another bug - if you disable the GATT server in the NRF Connect and leave only the advertiser with the desired UUID, then a cyclic restart occurs. |
Howdy @nklys ... re the new bug ... disabling the GATT server in nRF Connect. Awesome catch. The code didn't check that the request to connect to the BLE Server has succeeded and merrily wen't on its way as thought it had ... even though the variables relating to the characteristics were null. Not pretty. Thankfully the fix was easy and has been done and committed to the repository. The race condition issue is going to be tricky to track down. As soon as we switch on debug level diagnostics, the problem fails to manifest which means that it is a race condition issue. I call these kinds of problems "Shrodeinger's bugs". As soon as you try and look at them, they no longer exist to be looked at. What we will need to do is continually splatter manually inserted debug statements into the code while not switching on ALL the debug. At some point we will then see that there is a common "last thing BLE APIs do before crash" and that will tell us where we should put in our manual synchronization to prevent the race condition. Another possible solution is to NOT use loop() but instead put 100% of our code in setup() and control our execution ourselves. My gut says that will also resolve the issue. How does that feel to you? Do you actually need "loop()" in your code? |
With an empty "loop()", nothing has changed. Bug stayed. I do not know where to insert println (), there are closed libraries. An error, perhaps when calling esp_ble_gattc_open. |
Hmm the mystery continues. What kind of ESP32 module are you running? I have many/most types and have been testing with DevKitC. If you tell me which one you are using, I can test against that to eliminate another possibility. |
Perfect ... I have two of those on my desk right now still in their wrappers. I haven't soldered the pins on yet so that will have to be tomorrow now. If this is more urgent than that, you can always ping me live on channel |
Hello Neil. Log: Backtrace: 0x40085847:0x3ffdaeb0 0x400868df:0x3ffdaed0 0x40085fd8:0x3ffdaef0 0x400d5787:0x3ffdaf30 0x400d34d4:0x3ffdaf50 0x400d29dd:0x3ffdafc0 0x4018386a:0x3ffdb000 Rebooting... If enable debug information output, everything starts working. Did you succeed in repeating this bug? |
I tried your sketch but it all seemed to work on my board. Here is the sketch I used:
I'm hearing you loud and clear that switching on debug "masks" the problem. So here is another idea ... I haven't had the opportunity to test it myself ... but Ive heard good things. See the following: https://github.com/me-no-dev/EspExceptionDecoder Follow those instructions and get it working. Now when the exception occurs again, follow that recipe and we will decode the backtrace addresses to be able to see where within the code the exception is occurring. Run it a few times, I want to see if it is always the same place that the exception happens. |
Thank you for helping. Here is the output of the exception decoder for code from my past message -https://pastebin.com/uJHaCL9i |
After updating arduino-esp32 in the log another line was added. With multiple starts, the backtrace sometimes changes, there are three variants: |
Argh ... that's the problem with asynchronous callbacks and race conditions. Each run can produce a distinct result and when we switch on tracing, things are "slowed down" enough that we have trouble recreating. Before we go too much further, I'd like to see the content of the "sdkconfig.h" in play in your environment. Find your Arduino IDE install directory and south of there, we will find a file called "sdkconfig.h". Likely in hardware/espressif/tools/sdk/include/config (or something similar to that). I'm explicitly looking for the BT task stack size but if we pastebin the whole file, that would be useful. What has me puzzles is that you are experiencing the problem but yet we don't have the same reports from others. This makes me want to look at what is "special" in your environment. I'm going to re-read this whole thread from the start and see if I can't find some other clues. |
sdkconfig.h - https://pastebin.com/t51hSjf7 |
Hello every one! I am in the same situation that @nklys. @nklys did you make the UART.ino example work? I'm trying with a Wemos Lolin 32 (without OLED) and always get the same result (the rebooting loop). I set the debug configuration at 5 but nothing different happens and I changed the target device to "ESP32 Dev Module". Thanks a lot for your help and congratulation for your great job 👍 👍 |
Howdy .... in this document: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/ArduinoBLE.md At the bottom in a section titled "Decoding an exception stack trace" there is a recipe for getting information from a stack trace. Can I ask you to recreate the problem, copy the stack trace and paste it into the exception decoder. From there, can you post back what you find? |
Thank you for your help @nkolban and sorry for the delay in the response. This is the result:
|
@elloza Many thanks for the traces ... that helps. The good news is that we have a match for your problem ... see issues #110 and #81. In that story, the problem can be traced back to an attempt to run WiFi and BLE at the same time and running out of RAM. In your application, can we examine the nature of it. Does it attempt to use WiFi and BLE together? Would you be able to pastebin your script so that we can take a look at it? |
Hello again @nkolban! I have been seeing the issues #110 and #81. My code is exactly this: I don't know if someone have a simple example using only BLE comunication (in fact, I only need BLE for my current project). If I can help in any way please let me know it. 👍 👍 |
Hi @nkolban! Thank you so much for your help! I was reading deeply the issues #110 and #81 and I have updated my ESP32 core libraries to the last commit available. Now the UART example is working great! :) I will continue developing with the LOLIN32 board so I will give you more feedback :) Thanks so so much!!!! 👍 👍 |
Excellent... that is great news. My tinkering hours on the libraries are evenings and weekends but I'll try and assist as quickly as I can if new issues appear. |
Closing for now. Re-open as needed. |
Hai neil, how to enter into AT mode for ESP32 ?? PLEASE HELP |
Hi all, I am using ESP32 "BLE Client" and "BLE uart". I can do Scan well and connect, able to read notify data sent from "Server/BLE uart" in "BLE Client". Reverse way I want to read data in "BLE uart" from "BLE Client" to "BLE Server".
The above data sending on every second, how can I read it from the "Server/BLE uart", I am getting no callbacks to, class MyCallbacks: public BLECharacteristicCallbacks {
} |
How client code looks like to send that data? |
/**
#include "BLEDevice.h" // The remote service we wish to connect to. #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID static BLEAddress pServerAddress; BLEClient* pClient; static void notifyCallback(
// Serial.println(value.c_str());
} bool connectToServer(BLEAddress pAddress) { // BLEClient* pClient = BLEDevice::createClient();
// BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
}
} // onResult void setup() { // Retrieve a Scanner and set the callback we want to use to be informed when we } // End of setup. // This is the Arduino main loop function.
// If the flag "doConnect" is true then we have scanned for and found the desired
} // If we are connected to a peer BLE Server, update the characteristic each time we are reached
} delay(1000); // Delay a second between loops. |
Hi chegewara, I am using the same "BLE CLient" in the BLE examples of ESP32 |
Ok, but if your server device is esp32 with BLE_uart example code, then you have to write to characteristic with this UUID: This part of code is useless for you:
|
Hi chegewara, thank you for figure out my fault. Now I am getting call back to here
After "Test01", nothing is printing. Am doing write here to read value in "BLE uart". |
This should be onWrite, not onRead:
|
I have both onRead() and onWrite() class MyCallbacks: public BLECharacteristicCallbacks {
} Not calling onWrite();, The same "BLE uart" when I tried with Android BLE Scanner app its calling onWrite() only but not now. Why this issue? |
I dont really know, you have 2 apps and hard to say why its not working.
|
Hi I am always getting this,
Backtrace: 0x40092dbc:0x3ffd5a30 0x40092fbf:0x3ffd5a50 0x40183bb3:0x3ffd5a70 0x40183bfa:0x3ffd5a90 0x4017166f:0x3ffd5ab0 0x400d2e91:0x3ffd5ad0 0x400d2f9b:0x3ffd5b30 0x400d1592:0x3ffd5b80 0x400d17ca:0x3ffd5bd0 0x4018b770:0x3ffd5c30 Rebooting...
Backtrace: 0x400d41c3:0x3ffd5b50 0x400d163a:0x3ffd5b80 0x400d17ca:0x3ffd5bd0 0x4018b770:0x3ffd5c30 Rebooting... Why its happening |
Hi I got it, the problem occurs because of this, const uint8_t v[]={0x1,0x0}; instead of this, pRemoteCharacteristic->registerForNotify(notifyCallback); I made this change to get the callbacks to "notifyCallback" on every notify from server. without adding above 2 lines I won't getting calback's on notify data from server. |
Please tell me write way to get the callbacks in BLE Client for every notify from Server |
Im guessing You are trying to write to descriptor to wrong characteristic (this characteristic does not have descriptor 2902): |
Hi, I am using below UUID's at server |
The same you have set with NOTIFY flag in BLE_uart code. |
Hi chegewara, I have not understood what you are trying to say. Can you please brief char valuetosend[8]; |
I came across this issue while looking for a full client UART example. This was close but fell short on the client sending data back to the server. If anyone else ends up here looking for the same thing this is my solution, built on Niel's BLE_client example. |
Hi Neil, as posted on the ESP-Arduino repo, a BLE Central scan for device, connect, enable notifications or other characteristic and read/write the value example would be really useful.
The text was updated successfully, but these errors were encountered: