Getting Started with BLE on the ESP32

MarkIngle
Posts: 30
Joined: Wed Jan 18, 2017 4:48 am

Getting Started with BLE on the ESP32

Postby MarkIngle » Fri Feb 17, 2017 12:29 am

Hello! I am a huge fan of the ESP8266 and I have done a ton of really neat projects using the ESP8266. Several of my projects support some really practical uses on my boat....remotely controlling a trolling motor and the trim and tilt of my outboard motor. I have even made a cosplay battle lance with IMU and 8266 that has flashing LEDS. I recently received my first ESP32 and I am very interested in learning how BLE comms works from a programing standpoint so that I can replace the ESP8266 wifi in my projects.

Based on my research and study so far I understand that the BLE communication architecture is broken up into two part - GAP and GATT. GAP being the initial comms step between two devices...... a peripheral BLE device is advertizing its self in a "Hello, Im a here" format and then GATT function calls kick in to exchange information assuming the peripheral device allows it through its "Hello..." advertisement. But thats about as far I as I can get with a basic understanding. The examples from the SDK are really helpful getting me this far but I need for things to sink in my head and stick! With this post I am asking for any information on the sequence that I should follow to creating a program to support a peripheral device advertising "Hello, I am here!"....My strategy here is to learn one part at a time writing my own peripheral program. I will be using an Android to connect to my ESP32 since they seem to work better with BLE.

There are several function calls and it appears that esp_ble_gap_register_callback is one of the main ones. Any one that can provide the other function calls that are needed and their sequence I would really appreciate it. Until then I will be poking at the sequence and compiling a bunch of errors! :)

MarkIngle
Posts: 30
Joined: Wed Jan 18, 2017 4:48 am

Re: Getting Started with BLE on the ESP32

Postby MarkIngle » Sat Feb 18, 2017 3:07 am

So I have made some significant progress this week learning more about the ESP32 BLE and the code base provided in the examples. Focusing on the GAP functions first has proven to be beneficial. I am basically "hacking through the code" using printf statements in the main section and working my way through other functions defined in the code. In addition I have loaded several apps from Nordic, RFConnect and RF Logger, on my phones to scan and connect to the ESP32 dev board running the client or server code. I have these apps on an Android and iPhone. I also have LightBlue installed on the iPhone and it seems to be the most helpful at this point.

In addition I have been reading Getting Started with Bluetooth Low Energy from O'Reilly as I hack my way into the examples. Mainly gatt_server, gatt_client and blue_adv are matching up well with what is explained in the O'Reilly book. Also its helpful to have the Kolban ESP32 book as well as he breaks down the UUID for advertising. Which is a foundation piece in the BLE comms architecture. Below are some of the steps and notes in getting my environment stood up. This is required before you can start compiling.

First I had to setup my build environment on my Mac. I found this document to be the easiest to understand:

https://media.readthedocs.org/pdf/esp-i ... f-fork.pdf

It has a section for Windows, Linux and Mac in chapters 1, 2, and 3.

The tricky part was realizing how different the setup is for the ESP32 compared to the ESP8266. Before you can do anything with the examples you must configure the project level directory with a utility called menuconfig. In order to run the utility you use a command - "make menuconfig". This means the environment variables (namely PATH) must include the esp-idf directory references. If you don't do this the make command to start the utility will not work. So its important to follow the PATH setup instructions in the first three chapters for your OS. This is clearly explained in the link above... If you get this error you need to set your path...

make: *** No rule to make target `menuconfig'. Stop.

Next, the utility has an option for configuring the serial connection as well as the components for the examples you intend to play with and test. So if you are going to compile a Bluetooth example you need to configure the Bluetooth component in the utility first. This is probably the same for Wifi, PHY, etc. (Although I have not tried any of the other components yet so I am making an assumption). This updates the sdkconfig file that has variables with assigned values. The variables are used in some of the macros of the xxxx_main.c files of the examples. To learn the architecture I decided to crack the UUID format first. Its really challenging! Today I finally understood the format and the values used in the advertising format. I will post an explanation in another reply.

User avatar
pcbreflux
Posts: 28
Joined: Sat Feb 11, 2017 1:00 am
Location: germany
Contact:

Re: Getting Started with BLE on the ESP32

Postby pcbreflux » Sat Feb 18, 2017 11:45 am

Hi,

the easiest way to run make is to put the PATH and IDF_PATH to your $HOME/.profile:

like
export PATH=$PATH:~/esp/xtensa-esp32-elf/bin
export IDF_PATH=~/esp/esp-idf

then run . ~/.profile once or logout/login or restart the computer

In the examples mostly the needed settings are made.
Sometimes just run "make menuconfig" and only "exit" to save some new features (pulled from github with "git pull" in $IDF_PATH).
In "Serial flasher config" I am using "other baudrate" 1500000 successfully but mostly 921600 works also fine but don't get frustrated with just 115200.
To speedup make you can use make -j8 all (if you have 4 cores 8 thread CPU or an other value).

Here are more BLE examples: https://github.com/pcbreflux/espressif/ ... /esp32/app

MarkIngle
Posts: 30
Joined: Wed Jan 18, 2017 4:48 am

Re: Getting Started with BLE on the ESP32

Postby MarkIngle » Mon Feb 20, 2017 2:52 am

I was able to decipher the advertising UUID this weekend in the gatts_server example. Below is a copy of my notes and explanation for each hex value. I am currently working to understand how to send the characteristic description and value using the UUID format for Heart rate measurement....its proving to be difficult.

//This data is sent in the advertisment broadcast
//static uint8_t raw_adv_data[] = {
// 0x02, 0x01, 0x06, 0x0f, 0x09, 0x45, 0x53, 0x50, 0x5f, 0x47, 0x41, 0x54, 0x54, 0x53, 0x5f, 0x44,
// 0x45, 0x4d, 0x4f, 0x02, 0x0a, 0xeb, 0x05, 0x03, 0x04, 0x13, 0x03, 0x13
//};

// 0x02 is the length/number of positions next in the UUID string that will be used to communicate the <<FLAGS>> parameter and its value.
// (1) 0x01 is the <<FLAGS>> parameter per the GAP spec - see https://www.bluetooth.com/specification ... ss-profile
// (2) 0x10 is the decimal value of the FLAG parameter - google "Supplement to Bluetooth Core Specification Section 1.3 FLAGS"
// This value can be set to match the binary value of the 8-bit FLAG parameter. The decimal value determines which bits are set. A decimal value fo 10 give me "General Discoverable Mode"
// and "Simulataneous LE and BR/EDR to Same Device Capable (COntroller)""

// 0x0f is the length/number of positions (fithteen) next in the UUID that will be used to communicate the <<COMPLETE LOCAL NAME>> parameter and its value.
// (1) 0x09 is the <<COMPLETE LOCAL NAME>> parameter per the spec - see https://www.bluetooth.com/specification ... ss-profile
// The next 14 are hex values that spell the ESP_GATTS_DEMO:
// (2) 0x45='E'
// (3) 0x53='S'
// (4) 0x50='P'
// (5) 0x5f='-'
// (6) 0x47='G'
// (7) 0x41='A'
// (8) 0x54='T'
// (9) 0x54='T'
// (10) 0x53='S'
// (11) 0x5f='-'
// (12) 0x44='D'
// (13) 0x45='E'
// (14) 0x4d='M'
// (15) 0x4f='O'

// 0x02 is the length or umber of positions (two) next in the UUID that will be used to communicate the <TX POWER LEVEL>> parameter and its value.
// (1) 0x0a is the <<TX POWER LEVEL>> parameter per the GAP spec - see https://www.bluetooth.com/specification ... ss-profile
// (2) 0xeb is the hex value that represents the reading of power. This value translates in -22 in RFConnector but I am not sure why

// 0x05 is the length or number of positions (three) next in the UUID that will be used to communicate the <<COMPLETE LIST OF 16-BIT SERVICE CLASS UUIDs>>
// parameter and its values.
// (1) 0x03 is the <<COMPLETE LIST OF 16-BIT SERVICE CLASS UUIDs>> parameter per the GAP spec - see https://www.bluetooth.com/specification ... ss-profile
// (2) 0x04
// (3) 0x13
// (4) 0x03
// (5) 0x13
//

Who is online

Users browsing this forum: No registered users and 134 guests