티스토리 뷰
개요
Arduino101은 Bluetooth LE(Low Energy) 통신 모듈을 내장하고 있다.
말 그대로 Bluetooth를 저전력으로 지원하는 것으로 배터리를 사용하는 모바일 기기 등에 활용하기 위함이다.
CurieBLE 라이브러리를 제공한다.
코드
#include CurieBLE.h
// service와 characteristic 선언
BLEService genuinoService1("BBB0");
BLEIntCharacteristic char1("BBB1", BLERead | BLENotify, 30); // arduino에서는 읽기
BLEService genuinoService2("0174");
BLEIntCharacteristic char2("0175", BLEWrite); // arduino에서는 쓰기
BLECharacteristic char3("0176", BLEWrite, 10); // arduino에서는 쓰기
int value1 = 0; // arduino에서 전송할 integer value
int value2 = 0; // central에서 읽어올 integer value
String value3 = 0; // central에서 읽어올 String value
boolean isConnectedCentral = false;
void setup() {
Serial.begin(19200);
initBLE();
}
void loop() {
blePeripheral.poll();
if (isConnectedCentral) char1.setValue(value1); // characteristic에 값을 쓰면 바로 전송된다.
value1++;
}
void initBLE() {
BLE.begin()
BLE.setLocalName("GENUINO101");
// service에 characteristic 등록
BLE.setAdvertisedService(genuinoService1);
genuinoService1.addCharacteristic(char1);
BLE.setAdvertisedService(genuinoService2);
genuinoService2.addCharacteristic(char2);
genuinoService2.addCharacteristic(char3);
// service 등록
BLE.addService(genuinoService1):
BLE.addService(genuinoService2):
// BLE 관련 event 처리 함수 등록
BLEsetEventHandler(BLEConnected, blePeripheralConnectHandler); // central과 통신 연결 시
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); // central과 통신 해제 시
char2.setEventHandler(BLEWritten, char2Written); // central에서 데이터 전송 시
char3.setEventHandler(BLEWritten, char3Written); // central에서 데이터 전송 시
}
// event 처리 함수
void blePeripheralConnectHandler(BLECentral central) {
Serial.print("Connected event, central : ");
Serial.println(central.address());
isConnectedCentral = true;
}
void blePeripheralDisconnectHandler(BLECentral central) {
Serial.print("Disconnected event, central : ");
Serial.println(central.address());
isConnectedCentral = false;
}
// integer value 처리 함수
void char2Written(BLECentral central, BLECharacteristic characteristic) {
value2 = char2.value();
}
// string value 처리 함수
void char3Written(BLECentral central, BLECharacteristic characteristic) {
const byte* byteBuffer = (const byte* )malloc(10);
byteBuffer = char3.value();
value3 = String((char *)byteBuffer);
}
각 데이터 타입별로 Characteristic을 선언할 수 있으나 String은 별도로 없고 기본 Characteristic으로 선언하여 byte*로 처리한다.
참조 링크
'개발 > Genuino101' 카테고리의 다른 글
| Using motor shield in Genuino (Arduino) 101 (0) | 2018.02.20 |
|---|---|
| Arduino101(Genuino101)의 IMU 기초 (0) | 2017.04.12 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- L293D
- 친구
- 에코랜드
- 민성이
- 렌즈
- 나현이
- 봄
- 일기
- 유아동요
- 라즈베리파이
- Android Studio
- shift register
- Genuino101
- 꽃
- 노자
- Arduino101
- 뽀로로
- 제주도
- 카메라
- 꽃전시회
- ble
- 도덕경
- 산책
- 휴애리
- 규슈
- 74HC565
- Kotlin
- 까꿍이
- 한화우림
- 동탄
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함