ENع
→BLE sensor
الدرس 50 · Connectivity and the capstone node

A BLE sensor: services and characteristics

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 50 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. A 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);
  3. 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 them
  4. Encode 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"
  5. 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();
  6. 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 = 0x0013
  7. Read: 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 °C
  8. Subscribe: 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 CCCD
  9. Notify: 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();
  10. 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)

اختبار يتاح بعد الخطوة الأخيرة.