BLE sensor with a custom GATT service
Build it step by step
0 of 7 doneTest the sensor
Print the temperature to serial.
Start BLE
Advertise with a name.
Find it on the phone
Use a scanner app.
Add the temperature characteristic
Read it from the app.
Turn on notifications
See it update every second.
Add the LED characteristic
Write 01 and 00.
Try a slower interval
Compare current use.
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
- Development BoardsBuyESP32 DevKit V1 × 1
- SensorsBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| BME280 VCC and GND | ESP32 3V3 and GND | Power |
| BME280 SDA | ESP32 GPIO21 | I2C data |
| BME280 SCL | ESP32 GPIO22 | I2C clock |
| Red LED anode | 220 Ω resistor to GPIO2 | Remote LED |
| Red LED cathode | ESP32 GND | Return |
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.
| Check | Expected |
|---|---|
| Scanner shows the device | Name appears |
| Notify on | New value every second |
| Write 01 | LED lights |
| Disconnect | Device 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 →
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.
