본문 바로가기

[ST_MICRO]/STM32WB

STM32WB55 SSM - Arduino BLE Custom 테스트

 

Arduino STM32WB BLE UART 테스트에서는 라이브러리에서 기본 제공하는 코드를 이용하였지만 Custom 데이터 송수신이 필요한 어플리케이션을 위해 테스트 코드를 작성해 보았다. 

 

nRF52 UART 테스트 코드 와 유사하게 코드를 작성했다.

 

#include "Arduino.h"
#include "BLE.h"
//#define Serial USBSerial
#define PA10        0
#define PA4         4
#define LED_BUILTIN PA4
#define SW1_PORT PA10
//#define LED_BUILTIN 23 //PA4

#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

BLEService BleUartService(SERVICE_UUID);
BLECharacteristic tx_caracteristic(CHARACTERISTIC_UUID_TX, (BLE_PROPERTY_READ | BLE_PROPERTY_NOTIFY), 0, 128, false);
BLECharacteristic rx_caracteristic(CHARACTERISTIC_UUID_RX, (BLE_PROPERTY_WRITE | BLE_PROPERTY_NOTIFY), 0, 128, false);

static const uint8_t manufacturer_data[6] = {
    0x01,
    0x00,
    (uint8_t)(0x20000000 >> 24),
    (uint8_t)(0x20000000 >> 16),
    (uint8_t)(0x20000000 >> 8),
    (uint8_t)(0x20000000 >> 0),
};

void OnSendCallback() {
     if (tx_caracteristic.subscribed()) {
          Serial.print("OnSendCallback:");
         //tx_caracteristic.writeValue(ledFeature);
     }
}

int toggle = 0;
void OnReceiveCallback() {
    uint8_t data[128];
    int size;
    
    if (rx_caracteristic.written()) {
        
        size = rx_caracteristic.valueLength();
        rx_caracteristic.getValue(data, size);
        //feature = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3] << 0);
        toggle ^= 1;
        digitalWrite(LED_BUILTIN, toggle);
        Serial.print("OnReceiveCallback: size=");
        Serial.print(size);
        Serial.print(", value=");
        Serial.print(data[0], HEX);
        Serial.println();
    }
}
void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    //digitalWrite(LED_BUILTIN, (ledFeature.led != 0));
    digitalWrite(LED_BUILTIN, 1);
    pinMode(SW1_PORT, INPUT_PULLUP);
    Serial.begin(115200);
    
    while (!Serial) { }
    Serial.println("BLE UART Start");
    BLE.begin();
    BLE.setIncludeTxPowerLevel(true);
    BLE.setLocalName("STM32WB-UART");
    BLE.setManufacturerData(manufacturer_data, sizeof(manufacturer_data));
    BleUartService.addCharacteristic(tx_caracteristic);
    BleUartService.addCharacteristic(rx_caracteristic);
    BLE.addService(BleUartService);
    //BLE.addService(commandService);
    tx_caracteristic.onSubscribed(OnSendCallback);
    rx_caracteristic.onWritten(OnReceiveCallback);
    
    BLE.advertise();
    delay(1000);
}

void loop()
{ 
    if (!BLE.advertising() && !BLE.connected()) {
        BLE.advertise();
    }
    
    if(!digitalRead(SW1_PORT))
    {
      tx_caracteristic.writeValue('a');
      delay(200);
    }
    delay(10);
}

 

 

컴파일후 운로드 하면 STM32WB-UART로 정상적으로 인식된다.

 

 

 

BLE UART를 지원하는 다양한 앱에서도 정상적으로 인식하고 동작 하는 것을 확인 할 수 있다.

 

 

반응형