BLE GATT Server: Environmental Sensing និង Notify
ប្រើបណ្ណាល័យ BLE ដែលភ្ជាប់មកជាមួយ Arduino-ESP32 ដើម្បីបង្កើត Service ស្តង់ដារ 0x181A (Temperature Notify) និង Service ផ្ទាល់ខ្លួនសម្រាប់បញ្ជា LED ពីទូរសព្ទ។
- បង្កើត BLE GATT Server ដែលមាន Service/Characteristic ស្តង់ដារ និង Notify
- ប្រើ Deep Sleep (Timer, ext0, RTC_DATA_ATTR) ហើយវាស់/គណនាចរន្តមធ្យម
- សង់ Battery MQTT Weather Node ដែលភ្ញាក់ វាស់ ផ្ញើ និងដេកវិញ ដោយមានការការពារថ្ម
BLE GATT រៀបចំទិន្នន័យជាឋានានុក្រម៖ Server (ESP32) → Service (ក្រុមមុខងារ) → Characteristic (តម្លៃមួយដែលមាន properties READ/WRITE/NOTIFY) → Descriptor (ឧ. CCCD 0x2902 ដែល Client សរសេរដើម្បីបើក Notify)។ UUID 16-bit ត្រូវបានកំណត់ដោយ Bluetooth SIG ឧ. Environmental Sensing Service 0x181A និង Temperature 0x2A6E (sint16 គិតជា 0.01 °C ដូច្នេះ 28.53 °C = 2853) — App ទូទៅដូចជា nRF Connect ស្គាល់ភ្លាម។ Service ផ្ទាល់ខ្លួនត្រូវប្រើ UUID 128-bit ចៃដន្យ (មិនត្រូវប្រើ 16-bit ដែល SIG បានកំណត់)។
វគ្គនេះប្រើបណ្ណាល័យ BLE ដែលភ្ជាប់មកជាមួយ Arduino-ESP32 core (BLEDevice.h) ដើម្បីកុំដំឡើងបន្ថែម។ ក្នុង core 3.x getValue() ត្រឡប់ String (មិនមែន std::string ដូច 2.x) ហើយចាប់ពី 3.3 CCCD 0x2902 ត្រូវបានបន្ថែមស្វ័យប្រវត្តិសម្រាប់ characteristic ដែលមាន NOTIFY (core ចាស់ជាងនេះត្រូវ addDescriptor(new BLE2902()))។ NimBLE-Arduino ជាជម្រើសដែលប្រើ RAM/Flash តិចជាង ប៉ុន្តែ API ខុសគ្នាបន្តិច — ជ្រើសមួយ ហើយប្រើឱ្យជាប់លាប់ពេញគម្រោង។
Notify ល្អជាងឱ្យ Client Read ជាប់ (polling) ព្រោះ ESP32 ផ្ញើតែពេលមានតម្លៃថ្មី សន្សំថាមពលទាំងសងខាង។ ពេល Client ផ្ដាច់ ត្រូវហៅ BLEDevice::startAdvertising() ឡើងវិញ បើមិនដូច្នោះទេ គ្មាន Client ណារកឃើញឧបករណ៍ទៀតទេ — ជាកំហុសញឹកញាប់បំផុត។ សុវត្ថិភាព៖ characteristic ដែលអាច WRITE បញ្ជា relay/motor គួរតម្រូវ pairing/bonding ឬយ៉ាងហោចណាស់ផ្ទៀងផ្ទាត់ទិន្នន័យ ព្រោះអ្នកណានៅចម្ងាយ ~10–30 m អាចភ្ជាប់បាន។
ESP32 classic ចែក radio 2.4 GHz តែមួយរវាង Wi-Fi និង BLE (coexistence ដោយ time-slicing)។ អាចប្រើព្រមគ្នាបាន ប៉ុន្តែ BLE + Wi-Fi + TLS ស៊ី RAM និង flash ច្រើន (ជារឿយៗលើស 1.25 MB ហើយត្រូវការ Partition Scheme ធំជាង)។ BLE ល្អសម្រាប់ការកំណត់ពីទូរសព្ទនៅក្បែរ (provisioning, បញ្ជាផ្ទាល់) ចំណែក MQTT/Wi-Fi សម្រាប់ទិន្នន័យទៅ Cloud។
- GATT៖ Server → Service → Characteristic → Descriptor (CCCD 0x2902 សម្រាប់ Notify)
- ប្រើ UUID SIG (0x181A/0x2A6E) សម្រាប់ទិន្នន័យស្តង់ដារ; UUID 128-bit សម្រាប់ Service ផ្ទាល់ខ្លួន
- ក្រោយ Client ផ្ដាច់ ត្រូវ startAdvertising() ម្ដងទៀត
- Characteristic WRITE ដែលបញ្ជា actuator ត្រូវការ pairing ឬការផ្ទៀងផ្ទាត់
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
// Arduino-ESP32 core 3.3+ adds the CCCD (0x2902) automatically for NOTIFY characteristics.
// On older 3.x cores, include <BLE2902.h> and call tempChar->addDescriptor(new BLE2902()).
constexpr uint8_t PIN_LED = 2;
constexpr uint32_t NOTIFY_INTERVAL_MS = 2000;
constexpr uint16_t ESS_SERVICE_UUID = 0x181A; // Environmental Sensing (Bluetooth SIG)
constexpr uint16_t TEMPERATURE_CHAR_UUID = 0x2A6E; // Temperature, sint16 in 0.01 degC
#define LED_SERVICE_UUID "a3c87500-8ed3-4bdf-8a39-a01bebede295" // Random 128-bit UUIDs
#define LED_CHAR_UUID "a3c87501-8ed3-4bdf-8a39-a01bebede295"
BLECharacteristic *tempChar = nullptr;
volatile bool clientConnected = false;
uint32_t lastNotifyMs = 0;
class ServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *server) override {
(void)server;
clientConnected = true;
Serial.println("BLE client connected");
}
void onDisconnect(BLEServer *server) override {
(void)server;
clientConnected = false;
Serial.println("BLE client disconnected, advertising again");
BLEDevice::startAdvertising(); // Otherwise nobody can find us again
}
};
class LedCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *c) override {
String value = c->getValue(); // Core 3.x returns Arduino String
bool on = value.length() > 0 && value[0] == '1';
digitalWrite(PIN_LED, on ? HIGH : LOW);
Serial.printf("LED <- %s\n", on ? "ON" : "OFF");
}
};
float readTemperatureC() {
// Replace with a real sensor (e.g. DHT22); simulated value for the lab
return 28.0f + random(0, 100) / 100.0f;
}
void setup() {
Serial.begin(115200);
pinMode(PIN_LED, OUTPUT);
BLEDevice::init("Stunity-ESS");
BLEServer *server = BLEDevice::createServer();
server->setCallbacks(new ServerCallbacks());
// Standard Environmental Sensing service: Temperature (read + notify)
BLEService *ess = server->createService(BLEUUID(ESS_SERVICE_UUID));
tempChar = ess->createCharacteristic(BLEUUID(TEMPERATURE_CHAR_UUID),
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY);
ess->start();
// Custom service: LED control (write "1" or "0")
BLEService *ledService = server->createService(LED_SERVICE_UUID);
BLECharacteristic *ledChar = ledService->createCharacteristic(
LED_CHAR_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
ledChar->setCallbacks(new LedCallbacks());
ledChar->setValue("0");
ledService->start();
BLEAdvertising *advertising = BLEDevice::getAdvertising();
advertising->addServiceUUID(BLEUUID(ESS_SERVICE_UUID));
advertising->setScanResponse(true);
BLEDevice::startAdvertising();
Serial.println("Advertising as Stunity-ESS");
}
void loop() {
uint32_t now = millis();
if (clientConnected && now - lastNotifyMs >= NOTIFY_INTERVAL_MS) {
lastNotifyMs = now;
int16_t centi = (int16_t)lroundf(readTemperatureC() * 100.0f); // 28.53 C -> 2853
uint8_t bytes[2] = {(uint8_t)(centi & 0xFF), (uint8_t)((centi >> 8) & 0xFF)}; // Little-endian
tempChar->setValue(bytes, sizeof(bytes));
tempChar->notify();
}
delay(10);
}ការពន្យល់៖ បើក nRF Connect លើទូរសព្ទ → Scan → Connect “Stunity-ESS”។ App បង្ហាញ Environmental Sensing → Temperature ជា °C ពេលចុចបើក Notify។ ក្នុង Service ផ្ទាល់ខ្លួន សរសេរ “1” ឬ “0” (UTF-8) ដើម្បីបើក/បិទ LED។
សំណួរវាស់សមត្ថភាព (Knowledge Check)
ឆ្លើយគ្រប់សំណួរ រួចចុច «ពិនិត្យចម្លើយ»។ បើត្រឹមត្រូវ ≥ ៨០% មេរៀននេះនឹងត្រូវបានកត់ត្រាថាបានបញ្ចប់ស្វ័យប្រវត្តិ។
- Bluetooth SIG Assigned Numbers (Services & Characteristics UUIDs) bluetooth.comបញ្ជី UUID ស្តង់ដារ ដូចជា Environmental Sensing 0x181A និង Temperature 0x2A6E។
- Arduino-ESP32 BLE Library (source & examples) github.com/espressifExamples Server, Notify និង Write callbacks សម្រាប់ core 3.x។