Projects / BLE sensor with a custom GATT service

BLE sensor with a custom GATT service

Intermediate2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Test the sensor

    Print the temperature to serial.

  2. ESP32

    Start BLE

    Advertise with a name.

  3. ESP32

    Find it on the phone

    Use a scanner app.

  4. ESP32

    Add the temperature characteristic

    Read it from the app.

  5. ESP32

    Turn on notifications

    See it update every second.

  6. ESP3219.6 mA

    Add the LED characteristic

    Write 01 and 00.

  7. ESP32

    Try a slower interval

    Compare current use.

In short

How do you build a BLE sensor with a custom GATT service?

An ESP32 offers temperature and a settable LED through a custom BLE service that a phone app reads. You need ESP32 DevKit V1, BME280 Environmental Sensor, LED 5 mm red and Resistor 220 Ω. It takes 7 steps and about 120 minutes.

Level
Intermediate
Time
About 120 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
BME280 VCC and GNDESP32 3V3 and GNDPower
BME280 SDAESP32 GPIO21I2C data
BME280 SCLESP32 GPIO22I2C clock
Red LED anode220 Ω resistor to GPIO2Remote LED
Red LED cathodeESP32 GNDReturn

Code

A GATT service is a small table: one service with two characteristics. Temperature is a notifying read, sent as a 16-bit number in hundredths of a degree so that no decimal text is needed, and the LED is a read and write byte. Use a free BLE scanner app such as nRF Connect: find ElectroLark-Sensor, open the service, enable notifications on the first characteristic, and write 01 or 00 to the second to switch the LED. Generate your own UUIDs rather than reusing the examples. This build has no pairing or encryption, so anyone nearby can read the temperature and change the LED, which is acceptable for a learning project but not for a lock or any control of something important. Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// BLE sensor with a custom service. Library: Adafruit BME280, ESP32 BLE Arduino (built in).
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <Wire.h>
#include <Adafruit_BME280.h>

// Make your own UUIDs with an online generator; these are examples.
#define SERVICE_UUID  "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
#define TEMP_UUID     "6e400002-b5a3-f393-e0a9-e50e24dcca9e"   // read and notify: temperature in 0.01 C, int16
#define LED_UUID      "6e400003-b5a3-f393-e0a9-e50e24dcca9e"   // read and write: 0 or 1

Adafruit_BME280 bme;
BLECharacteristic *tempChar, *ledChar;
bool connected = false;
const int LED = 2;

class ServerCb : public BLEServerCallbacks {
  void onConnect(BLEServer*) { connected = true; }
  void onDisconnect(BLEServer* s) { connected = false; s->startAdvertising(); }   // advertise again
};
class LedCb : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic* c) {
    String v = c->getValue();
    if (v.length() > 0) digitalWrite(LED, v[0] ? HIGH : LOW);
  }
};

void setup() {
  pinMode(LED, OUTPUT);
  Wire.begin(21, 22);
  if (!bme.begin(0x76)) bme.begin(0x77);
  BLEDevice::init("ElectroLark-Sensor");
  BLEServer* server = BLEDevice::createServer();
  server->setCallbacks(new ServerCb());
  BLEService* svc = server->createService(SERVICE_UUID);
  tempChar = svc->createCharacteristic(TEMP_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
  tempChar->addDescriptor(new BLE2902());
  ledChar = svc->createCharacteristic(LED_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
  ledChar->setCallbacks(new LedCb());
  uint8_t zero = 0; ledChar->setValue(&zero, 1);
  svc->start();
  BLEAdvertising* adv = BLEDevice::getAdvertising();
  adv->addServiceUUID(SERVICE_UUID);
  adv->start();
}

void loop() {
  if (connected) {
    int16_t t = (int16_t)(bme.readTemperature() * 100);
    tempChar->setValue((uint8_t*)&t, 2);
    tempChar->notify();
  }
  delay(1000);                          // a longer interval saves power
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Scanner shows the deviceName appears
Notify onNew value every second
Write 01LED lights
DisconnectDevice advertises again

If it does not work

Not visible

Check Bluetooth is on and the device name is advertised.

Value looks odd

Remember it is an int16 in hundredths of a degree.

No notifications

Enable notifications on the characteristic in the app.

Cannot reconnect

Call startAdvertising in the disconnect callback.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the BLE sensor with a custom GATT service?

ESP32 DevKit V1, BME280 Environmental Sensor, LED 5 mm red and Resistor 220 Ω.

How long does the BLE sensor with a custom GATT service take to build?

About 120 minutes for a intermediate maker, in 7 steps.

How do I know the BLE sensor with a custom GATT service works?

Scanner shows the device: Name appears. Notify on: New value every second. Write 01: LED lights.

Not visible: what should I check?

Check Bluetooth is on and the device name is advertised.

Value looks odd: what should I check?

Remember it is an int16 in hundredths of a degree.

No notifications: what should I check?

Enable notifications on the characteristic in the app.

Cannot reconnect: what should I check?

Call startAdvertising in the disconnect callback.