[Video]: Bluetooth BLE and C++ classes

enitalp
Posts: 60
Joined: Thu Jan 12, 2017 10:03 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby enitalp » Thu Aug 24, 2017 9:55 pm

Fun fact.

I'm using an ESP32, Single Core at 80 mhz. My code handle different external device, in my main loop, maximum speed of this main loop is 60 fps. I send a simple counter over BLE to an android Galaxy Tab S2 1.8ghz. On it running a custom test application with Unity 3D (C#).

Every time i receive a number over bluetooth, i respond Hello Number. i only send the answer if the BLE is not surcharged as the library in Unity 3D is asynchronous. If i can't send it, i put it in a stack to try to send it the next frame (60fps).

On the ESP32 i display a message when i send the number, and every response i get frorm the galaxy tab s2. Here is the log after some time :

Sending : 2655
Sending : 2656
Receiving : Hello :1565
Sending : 2657
Sending : 2658
Sending : 2659
Receiving : Hello :1566
Receiving : Hello :1567
Sending : 2660
Sending : 2661
Receiving : Hello :1568
Sending : 2662
Sending : 2663
Receiving : Hello :1569
Sending : 2664
Sending : 2665
Sending : 2666
Receiving : Hello :1570
Receiving : Hello :1571
Sending : 2667
Sending : 2668
Receiving : Hello :1572
Sending : 2669
Sending : 2670
Receiving : Hello :1573
Sending : 2671
Sending : 2672
Sending : 2673
Receiving : Hello :1574
Receiving : Hello :1575
Sending : 2674
Sending : 2675
Receiving : Hello :1576
Sending : 2676
Sending : 2677
Receiving : Hello :1577
Receiving : Hello :1578
Sending : 2678
Sending : 2679
Sending : 2680

Yup the GALAXY is getting behind pretty fast. I don't loose any packet that's the purpose of my code, trying to send in both direction data as fast as possible without loosing anything.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video]: Bluetooth BLE and C++ classes

Postby kolban » Thu Aug 24, 2017 9:57 pm

Since the classes are C++, I looked for a pre-supplied class that encapsulates a data buffer ... it appears that std::string is that class. In C, the concept of a "string" is an illusion ... it is just a sequence of characters which have an implicit 0 byte value terminator. In the C++ standard library, it is my understanding that std::string is an array of bytes with a separate length property. I have used std::string to hold arbitrary binary data. For example:

std:string myData(buffer, length);

and now myData will hold a copy of buffer for length bytes.

I'm thinking if we dis-regard the name string in std::string and just treat it as a "byte buffer" all is well. Again, I could be very wrong here and if we can think of a better model for data, speak up. Obviously we could create our own class to act as a buffer, but I'd like to first validate that the use of std::string is or is not a convention for this practice already.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

enitalp
Posts: 60
Joined: Thu Jan 12, 2017 10:03 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby enitalp » Thu Aug 24, 2017 10:07 pm

kolban wrote:Since the classes are C++, I looked for a pre-supplied class that encapsulates a data buffer ... it appears that std::string is that class. In C, the concept of a "string" is an illusion ... it is just a sequence of characters which have an implicit 0 byte value terminator. In the C++ standard library, it is my understanding that std::string is an array of bytes with a separate length property. I have used std::string to hold arbitrary binary data. For example:

std:string myData(buffer, length);

and now myData will hold a copy of buffer for length bytes.

I'm thinking if we dis-regard the name string in std::string and just treat it as a "byte buffer" all is well. Again, I could be very wrong here and if we can think of a better model for data, speak up. Obviously we could create our own class to act as a buffer, but I'd like to first validate that the use of std::string is or is not a convention for this practice already.
I understand why you used the std:string, but with no additional variable to handle the lenght of the data we can't use the lenght of the string to get the lenght of the data. If the data are not string, a 0 in the data will be considered as terminal for string lenght != data lenght.

You can use the std::vector<char> myData.

and there you can get an accurate lenght of the data

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video]: Bluetooth BLE and C++ classes

Postby kolban » Fri Aug 25, 2017 12:18 am

It is my belief that is we were to code:

Code: Select all

 
uint8_t data[] = { 'A', 'B', '\0', 'C', 'D' };
std::string data1((char*)data, 5);
int length = data1.length();
That the value of "length" would be 5.

See also:
http://www.cplusplus.com/reference/stri ... ng/length/

Is it your understanding that the length() method of a std::string instance would return 2 for the the above data?
Last edited by kolban on Fri Aug 25, 2017 10:15 pm, edited 1 time in total.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

enitalp
Posts: 60
Joined: Thu Jan 12, 2017 10:03 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby enitalp » Fri Aug 25, 2017 7:22 pm

My expectation was 2, but verified and stunned all the c++ programmers working with me when it shows 5. That add to the reason we don't use std/stl.

So "it works" but i would still replace it by a vector, so somebody fluent in c++ will not be surprised liked me.

I saw that you don't limit the send length, on BLE 4.0 the max length of a characteristic is 20 octets. So if your lib is used to talk to an old android or ipad, you could have problem.

I'm currently adding a behavior to cut a big message into multiple 20 octets messages.

ajcalderont
Posts: 4
Joined: Thu Sep 07, 2017 5:03 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby ajcalderont » Thu Sep 07, 2017 5:30 pm

Hi Neil, thank you very much for your excellent work! I have a situation here: I'm trying to understand your BLE test based on Sample1.cpp, but when I compile and run the application it doesn't show the "Unknown Service" on my cell phone:
photo_2017-09-07_11-14-50.jpg
photo_2017-09-07_11-14-50.jpg (35.48 KiB) Viewed 12853 times
The weird thing is, if I change the default log verbosity from "Info" to "Debug" in "make menuconfig" and then recompile the project, then now I can see the "Unknown Service" on my cell phone:
photo_2017-09-07_11-14-59.jpg
photo_2017-09-07_11-14-59.jpg (42.69 KiB) Viewed 12853 times
Why do you think it is not working with "Info" verbosity?

Thanks in advance!

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video]: Bluetooth BLE and C++ classes

Postby kolban » Tue Sep 26, 2017 2:50 am

Howdy Sir,
I think this may be related to this issue:

https://github.com/espressif/esp-idf/issues/857

I also have seen strange symptoms with the latest BLE code is the pre-GA 3.0 BLE support related to "race conditions". Inside the ESP-IDF BLE classes there are multiple tasks in flight. I believe that some of these may be racing each other. When we switch on additional logging, we are effectively synchronizing some work on the underlying (relatively slow) serial output. The unfortunate thing is thus that when we have a problem and switch on debugging, the problem disappears.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: [Video]: Bluetooth BLE and C++ classes

Postby fly135 » Wed Jan 10, 2018 7:35 pm

Just tried to duplicate this based on your youtube video. Looks like there are significant changes that have made it more complicated than the video. Thanks for all videos and contributions.

John

kalimanmer
Posts: 15
Joined: Wed Sep 20, 2017 10:10 am

Re: [Video]: Bluetooth BLE and C++ classes

Postby kalimanmer » Wed Feb 21, 2018 12:25 pm

Thanks for this add, now I am testing cpp_utils and I flashed it on ESP32 chip. When a I search for btl services, I found the Generic Attribute and Generic Access services but NOT my customized Service (then service is not advertised according the debug messages). Do you have one idea why?

kalimanmer
Posts: 15
Joined: Wed Sep 20, 2017 10:10 am

Re: [Video]: Bluetooth BLE and C++ classes

Postby kalimanmer » Wed Feb 21, 2018 12:47 pm

I need to add, that I have change in menuconfig the log verbosity, from Info to Degug and it still is not working.

Who is online

Users browsing this forum: Bhaydar, ok-home and 100 guests