본문 바로가기

ESPRESSIF/ESP32-C3

[ESP32-C3 SSM] BLE 5.0 테스트 - HTM

ESP32-C3에서 온도센서 테스트 코드를 바탕으로 BLE로 온도 값을 전송하는 프로토콜 HTM 를 구현해 보자.

 

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


#define SERVICE_UUID        BLEUUID((uint16_t)0x181A)
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

BLEDescriptor outdoorHumidityDescriptor(BLEUUID((uint16_t)0x2901));
BLEDescriptor outdoorTemperatureDescriptor(BLEUUID((uint16_t)0x2901));

BLECharacteristic outHumidityCharacteristic(BLEUUID((uint16_t)0x2A6F), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLECharacteristic outTemperatureCharacteristic(BLEUUID((uint16_t)0x2A6E), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);

bool _BLEClientConnected = false;

class MyServerCallbacks : public BLEServerCallbacks {
	void onConnect(BLEServer* pServer) {
		_BLEClientConnected = true;

    Serial.println("connect");
	};

	void onDisconnect(BLEServer* pServer) {
		_BLEClientConnected = false;

    	Serial.println("dis connect");
	}
};


void InitBLE() 
{
    BLEDevice::init("ESP32_Temp");

    // Create the BLE Server
    BLEServer *pServer = BLEDevice::createServer();
    pServer->setCallbacks(new MyServerCallbacks());

    // Create the BLE Service
    BLEService *pService = pServer->createService(SERVICE_UUID);

    outdoorTemperatureDescriptor.setValue("Temperature -40-60°C");

    outTemperatureCharacteristic.addDescriptor(&outdoorTemperatureDescriptor);

    outTemperatureCharacteristic.addDescriptor(new BLE2902());

    //pService->addCharacteristic(&outHumidityCharacteristic);
    pService->addCharacteristic(&outTemperatureCharacteristic);
    pService->start();

    // Start advertising
    pServer->getAdvertising()->start();
}

void setup() {
  Serial.begin(115200);

  Serial.println("ESP32 BLE Write Test");
  InitBLE();
}

unsigned char temp_buf[2] = {0,};

void loop() {
  // put your main code here, to run repeatedly:

  if(_BLEClientConnected)
  {
    temp_buf[0]++;

	  outTemperatureCharacteristic.setValue(temp_buf, 2);
	  outTemperatureCharacteristic.notify();
  }
  delay(2000);
}

 

HTM BLE 서비스 잘 등록 되고 특성 온도 값도 정상적으로 올라오는 것을 확인 할 수 있다.

 


 

NRF52 에서 테스트 했던 HTS 앱 연동 테스트를 진행 해 보자

static void hts_sim_measurement(ble_hts_meas_t * p_meas)
{
    static ble_date_time_t time_stamp = { 2012, 12, 5, 11, 50, 0 };

    uint32_t celciusX100;

    p_meas->temp_in_fahr_units = false;
    p_meas->time_stamp_present = true;
    p_meas->temp_type_present  = (TEMP_TYPE_AS_CHARACTERISTIC ? false : true);

    celciusX100 = 35;//sensorsim_measure(&m_temp_celcius_sim_state, &m_temp_celcius_sim_cfg);

    p_meas->temp_in_celcius.exponent = -2;
    p_meas->temp_in_celcius.mantissa = celciusX100;
    p_meas->temp_in_fahr.exponent    = -2;
    p_meas->temp_in_fahr.mantissa    = (32 * 100) + ((celciusX100 * 9) / 5);
    p_meas->time_stamp               = time_stamp;
    p_meas->temp_type                = BLE_HTS_TEMP_TYPE_FINGER;

    // update simulated time stamp
    time_stamp.seconds += 27;
    if (time_stamp.seconds > 59)
    {
        time_stamp.seconds -= 60;
        time_stamp.minutes++;
        if (time_stamp.minutes > 59)
        {
            time_stamp.minutes = 0;
        }
    }
}



void loop() {
  if(_BLEClientConnected)
  {
    temp_buf[1]++;
    Serial.println(temp_buf[1]);

    ble_hts_meas_t simulated_meas;
    hts_sim_measurement(&simulated_meas);

	  outTemperatureCharacteristic.setValue((unsigned char*)&simulated_meas, sizeof(simulated_meas));
	  outTemperatureCharacteristic.notify();

    _batteryLevelCharacteristics.setValue(&temp_buf[1], 1);
    _batteryLevelCharacteristics.notify();
  }
  delay(2000);
}

 

 

프로그램 다운로드후 테스트 해보면 nRF ToolBox에서 온도 값이 정상적으로 출력 되는 것을 확인 할 수 있다.

반응형