TOOLS GAMES DEVLOG DOCS ABOUT CONTACT
HOME > DOCS > Embedded / IoT > ESP32 BLE(Bluetooth Low Energy) GATT 서버 구성 및 무선 센서 데이터 Notify 전송
Embedded / IoT Intermediate Arduino ESP32 Core / NimBLE

ESP32 BLE(Bluetooth Low Energy) GATT 서버 구성 및 무선 센서 데이터 Notify 전송

모바일 스마트폰 앱이나 웹 브라우저(Web Bluetooth)로 초저전력 센서 데이터를 20Hz 주기로 푸시 전송하는 ESP32 BLE GATT 서버 구축 가이드.

#Embedded #ESP32 #BLE #Bluetooth #IoT
핵심 개념BLE 통신은 클라이언트(스마트폰)가 매번 읽기를 요청하는 Polling 방식보다, 데이터가 바뀔 때 서버(ESP32)가 비동기로 패킷을 밀어주는 Notification(알림) 속성이 배터리 효율과 응답성 면에서 가장 우수합니다.

1. ESP32 BLE Notify 서버 아두이노 코드

ble_notify_server.ino
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) { deviceConnected = true; }
    void onDisconnect(BLEServer* pServer) { deviceConnected = false; }
};

void setup() {
    BLEDevice::init("LYSC_SENSOR_NODE");
    BLEServer *pServer = BLEDevice::createServer();
    pServer->setCallbacks(new MyServerCallbacks());

    BLEService *pService = pServer->createService(SERVICE_UUID);
    pCharacteristic = pService->createCharacteristic(
        CHARACTERISTIC_UUID,
        BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
    );
    pCharacteristic->addDescriptor(new BLE2902());
    pService->start();

    pServer->getAdvertising()->start();
}

void loop() {
    if (deviceConnected) {
        int sensorVal = analogRead(34);
        pCharacteristic->setValue((uint8_t*)&sensorVal, sizeof(sensorVal));
        pCharacteristic->notify(); // 구독한 스마트폰으로 패킷 푸시
    }
    delay(50); // 20Hz 주기
}

참고자료 & 관련 링크