ESP32 BLE server

I wanted to try out the BLE capabilities of the ESP32. I will not go into deep explanations how BLE works. Adafruit has a nice tutorial that explains details. To learn about BLE, check out the Introduction to Bluetooth Low Energy. As I have temperature and humidity sensors attached the logical step would be to set it up as an environment sensor. I checked  the Bluetooth GATT services to find out if there is something matching defined. And there is already a matching service, the Environmental Sensing. There are many characteristics defined for this service, to keep it simple I chose to implement only the following:

Just for fun I added to the list

  • Date Time – gives the current date and time and is used as well as notification for the client that updated data is available
  • Digital Output – to simulate a virtual keypad on the BLE client and trigger functions in the ESP32
  • Device Name – the name of the BLE server device
  • Two String – to transmit the comfort ratio and the perception

Now using BLE on the ESP32 has been made quite easy by nkolban who put together the ESP32 BLE for Arduino library. The library is already included in the ESP32 Arduino framework, so there is no need to install it separate.

To use the library, we first need to include the header files

Then we define the UUIDs for the service and the characteristics

Next is the pointers to the service class, the characteristic classes and the advertising class. In addition some flags and a global variable was defined for handling the BLE communications

The BLE library works with callbacks for BLE events. The first callback needed is for client connection and disconnection. Here we set as well a flag to see if any BLE client is connected.

Next callback is for client write requests. As we have defined a characteristic Digital Output, the client might write values to this characteristic. If this happens, this callback is called.

Now we can initialize the BLE server, create a server and attach the callbacks.

Next we create the characteristic for Date time. This characteristic is different from the other ones, because it has an additional descriptor which required if the client should be notified if data has been updated.

The other characteristics are created similar, but the do not have PROPERTY_NOTIFY set and no descriptor attached. All these characteristics are read only. That is indicated by PROPERTY_READ

The characteristic for the Digital Output is different again, as the client can write to it. So we have to attach the above defined callback function to receive the value the client has written to it

And that’s it, now we can start the service and the advertising

The complete code for the BLE initialization can be found in my Github Repo.

Now we have to fill the characteristic with the values from our sensors.

Most of it is done in the temperature module. First we check if a BLE client is attached. If there is no client attached, no actions are necessary

If a client is connected we fill the characteristics with the measured/calculated values

Next is to setup the Date Time characteristic, which is used to notify the client about updated data

And then we send the notification to the client

For changes in the Digital Output we just check the status of digOutChanged. This flag is set if the callbacks for client write requests was called. For a simple example, I just check the lowest three bits and start different tasks, depending on whether the bit was set or not.





Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory