A BLE sensor: services and characteristics
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Turn an ESP32 into a Bluetooth Low Energy temperature sensor that any phone can read. You build a GATT server: a small table of attributes holding the standard Environmental Sensing service (0x181A) with one Temperature characteristic (0x2A6E). Then follow the bytes: 23.45 °C becomes 2345, sent low byte first as 29 09; the phone finds the board by its adverts, connects, reads the value, writes 01 00 into the CCCD to subscribe, and from then on gets every new value pushed to it. The Networking lesson “Bluetooth and personal area networks” explains the radio side (channels, hopping and pairing); here you use it from firmware.
Server and client
Our ESP32 measures temperature and keeps it in a GATT server: a small table of values. The phone is the GATT client: it asks for values. In connection terms (GAP) the ESP32 is the peripheral and the phone the central.
GATT Generic Attribute Profile: how BLE devices share data server ESP32: holds the table, answers, can push values client phone: sends requests, receives answers and pushes GAP ESP32 = peripheral (advertises), phone = central (connects) Board ESP32-DevKitC, Arduino-ESP32 core, its BLE library
اقرأ الدرس خطوة بخطوة
Server and client
Our ESP32 measures temperature and keeps it in a GATT server: a small table of values. The phone is the GATT client: it asks for values. In connection terms (GAP) the ESP32 is the peripheral and the phone the central.
GATT Generic Attribute Profile: how BLE devices share data server ESP32: holds the table, answers, can push values client phone: sends requests, receives answers and pushes GAP ESP32 = peripheral (advertises), phone = central (connects) Board ESP32-DevKitC, Arduino-ESP32 core, its BLE libraryA table of attributes
The server keeps its data in rows called attributes. Each row has a handle (its row number), a type given by a UUID, and a value. createService adds the first row: service 0x181A, Environmental Sensing, a number the Bluetooth SIG assigns.
attribute = handle + type (UUID) + permissions + value handle 0x0010 type 0x2800 Primary Service value 0x181A 0x181A = Environmental Sensing (Bluetooth SIG) 16-bit UUIDs are SIG numbers; your own data uses 128-bit UUIDs line 1 s = srv->createService(ESS);A characteristic: one value
createCharacteristic adds 0x2A6E, Temperature. It adds two rows: a declaration saying what clients may do (READ and NOTIFY), and the value itself. BLE2902 adds a third, the CCCD descriptor 0x2902, where a client switches notifications on.
0x0011 declaration READ | NOTIFY, value at 0x0012, type 0x2A6E 0x0012 value the temperature, 2 bytes 0x0013 CCCD 0x2902, starts at 00 00 (notify off) lines 2-4 createCharacteristic(TEMP, PROPS); addDescriptor(new BLE2902()); handles here are illustrative: the stack assigns themEncode 23.45 °C as bytes
The SIG fixes the format: a signed 16-bit number in hundredths of a degree. loop() turns 23.45 °C into 2345, which is 0x0929. Bluetooth sends the low byte first, so b holds 29 then 09. setValue copies them into the value row.
format sint16, 0.01 °C per step (GATT Specification Supplement) 23.45 * 100 = 2345 = 0x0929 low byte first: b[0] = 0x29, b[1] = 0x09 line 6 c->setValue(b, 2); value row = 29 09 0x8000 would mean "value is not known"Advertise on three channels
setup() ended with startAdvertising. Every advertising interval, between 20 ms and 10.24 s, the ESP32 sends a short advert on channels 37, 38 and 39. It carries the name Thermo and service 0x181A. The phone scans and hears it.
channels 37, 38, 39 (advertising only) interval 20 ms to 10.24 s, in 0.625 ms steps advert at most 31 bytes of data (legacy) content name "Thermo", service UUID 0x181A line 5 BLEDevice::startAdvertising();The phone connects
The user taps Connect. The phone answers an advert with a connect request, and the two now meet on a fixed rhythm: a connection event every 7.5 ms to 4 s. Then the phone discovers the table, learning each handle.
phone -> ESP32 connect request, sent on an advertising channel interval 7.5 ms to 4 s, in 1.25 ms steps (the central sets it) discovery the client asks for services, then characteristics and learns: Temperature value = 0x0012, CCCD = 0x0013Read: ask and answer
The phone sends an ATT (Attribute Protocol) read request for handle 0x0012. The server answers with the value bytes, 29 09. The phone puts them back together: 0x0929 is 2345, so 23.45 °C. It works, but the phone would have to keep asking.
request 0A 12 00 (0x0A read request, handle 0x0012) response 0B 29 09 (0x0B read response, value) decode 0x0929 = 2345 -> 23.45 °CSubscribe: write the CCCD
To hear about changes, the phone writes 0x0001 into the CCCD, low byte first: 01 00. Bit 0 means notify me. The server answers with a write response, and from now on it may push the value.
request 12 13 00 01 00 (0x12 write request, handle 0x0013, value) response 13 (0x13 write response) CCCD bits bit 0 = notifications, bit 1 = indications line 4 new BLE2902() is this CCCDNotify: the server pushes
2 seconds later the sensor reads 23.50 °C: 2350, or 0x092E, bytes 2E 09. setValue stores them and notify() sends them to the phone unasked. A notification gets no reply, so it is quick but unconfirmed.
23.50 * 100 = 2350 = 0x092E -> 2E 09 notification 1B 12 00 2E 09 (0x1B, handle 0x0012, value) no response message: the server does not learn it arrived line 7 c->notify();Limits: 20 bytes, no receipt
An ATT message holds at most ATT_MTU bytes, 23 unless both sides agree on more. A notification spends 3 on its header: 1 opcode byte, 2 handle bytes. That leaves 20 for the value; ours needs 2. For a receipt, use indicate.
ATT_MTU 23 bytes by default (LE) header 1 opcode + 2 handle = 3 bytes value up to 23 - 3 = 20 bytes per notification notify 0x1B, no reply indicate 0x1D, the client answers 0x1E (confirmation)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01In this lesson, which device is the GATT server?
The server is the device that holds the attributes and answers requests, here the ESP32. The phone is the client: it sends requests and receives answers and notifications. (In radio terms the ESP32 is also the peripheral and the phone the central.)
02The sensor reads 21.30 °C. Which two bytes go into the Temperature value, in order?
21.30 × 100 = 2130 = 0x0852. GATT fields are little endian, so the low byte goes first: 52 08.
03What must the phone do before the ESP32's notify() actually sends anything to it?
Bit 0 of the Client Characteristic Configuration Descriptor (0x2902) switches notifications on. Until a client writes it, notify() sends nothing.
04What is the difference between a notification and an indication?
Notifications (opcode 0x1B) have no response, so they are quick but unconfirmed. Indications (0x1D) are answered by a confirmation (0x1E), so the server knows each one arrived.
05With the default ATT_MTU of 23 bytes, how many bytes of value fit in one notification?
A notification spends 1 byte on the opcode and 2 on the handle, so 23 − 3 = 20 bytes are left for the value. Our temperature needs only 2.
المسرد
- GATT
- Generic Attribute Profile: the rules BLE devices use to share data as a table of attributes on a server.
- Attribute
- One row in the server's table: a handle (its number), a type given by a UUID, permissions and a value.
- Service
- A group of related attributes, such as Environmental Sensing (0x181A). It starts with a service declaration row.
- Characteristic
- One data item inside a service, such as Temperature (0x2A6E): a declaration, the value and optional descriptors.
- UUID
- A number naming a type. 16-bit ones like 0x2A6E are assigned by the Bluetooth SIG; your own data uses 128-bit ones.
- CCCD
- Client Characteristic Configuration Descriptor (0x2902). A client writes bit 0 to turn notifications on, bit 1 for indications.
- Notification
- A value the server pushes to a subscribed client without being asked and without a reply.
- ATT and ATT_MTU
- ATT, the Attribute Protocol, is the set of request, response and notification messages that read and write the table. ATT_MTU is the largest ATT message: 23 bytes on BLE unless both agree on more.
الملاحظات والمصادر
- The handles 0x0010 to 0x0013 are made up for the picture. The BLE stack hands out real ones when the sketch runs, and the phone learns them during discovery. The service row's type is 0x2800 (Primary Service) and the declaration's is 0x2803.
- A real server table also starts with the GAP service (0x1800, which holds the device name) and usually the GATT service (0x1801), as in the Core Specification's example table. The figure shows only our service.
- The Temperature format is sint16 in units of 0.01 °C, little endian, range -273.15 to 327.67 °C, with 0x8000 meaning "not known" (Bluetooth SIG GATT Specification Supplement). The sketch builds the two bytes by hand, so it does not depend on the processor's byte order.
- notify() only sends when a client has written 01 00 into the CCCD. With the newer NimBLE stack the library adds the CCCD by itself; with Bluedroid you add BLE2902 as here.
- readTempC() is a stand-in that returns 23.45; replace it with your own sensor driver. The phone could be any BLE app that can scan, connect, read and subscribe.
- Bluetooth SIG Assigned Numbers: service UUIDs (0x181A Environmental Sensing)
- Bluetooth SIG Assigned Numbers: characteristic UUIDs (0x2A6E Temperature)
- Bluetooth SIG Assigned Numbers: descriptors (0x2902 CCCD)
- Bluetooth SIG: GATT Specification Supplement (§3.235 Temperature)
- Bluetooth Core Specification 6.0, Vol 3 Part G: Generic Attribute Profile (GATT)
- Bluetooth Core Specification 6.0, Vol 3 Part F: Attribute Protocol (ATT)
- Espressif ESP-IDF: BLE device discovery (advertising)
- Espressif ESP-IDF: BLE connection
- Espressif ESP-IDF: BLE data exchange (GATT, CCCD, notify and indicate)
- Arduino-ESP32 BLE library (source)
- Arduino-ESP32 BLE library: Notify example
اختبار يتاح بعد الخطوة الأخيرة.