광고2


ESP32의 블루투스 데이터보내기 예제 by uikyo93

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
 
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
 
#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
 
 
class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };
    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};
 
 
void setup() {
  
  Serial.begin(115200);
 
  BLEDevice::init("MyESP32");
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
 
  BLEService *pService = pServer->createService(SERVICE_UUID);
 
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );
  
  pCharacteristic->addDescriptor(new BLE2902());
  pService->start();
 
  pServer->getAdvertising()->start();
 
}
 
void loop() {
 
  if (deviceConnected) {
 
    char measure[2= {1025};
    pCharacteristic->setValue(std::string(measure, sizeof(measure)));
    pCharacteristic->notify();
   
  }
  delay(3000);
 
}
cs



ESP32에 센서를 달고 센서값의 데이터를 보낼때 사용한 코드입니다

if (deviceConnected) { // 블루투스가 디바이스에 연결됬을시
 
    char measure[2= {센서값1센서값2}; // 크기는 늘려도 상관없습니다
    pCharacteristic->setValue(std::string(measure, sizeof(measure)));
    pCharacteristic->notify();
  }
  delay(3000);
}




덧글

댓글 입력 영역


구글 광고2


통계 위젯 (화이트)

00
1
191091

광고