개발자/Arduino

Arduino Nano 33 IoT 개발 가이드 7 - Bluetooth Low Energy (BLE)

지구빵집 2020. 5. 15. 15:44
반응형

 

Arduino Nano 33 IoT 개발 가이드 7 - Bluetooth Low Energy (BLE)

 

이 장에서는 Arduino Nano 33 IoT에서 온보드 BLE 모듈을 사용하는 방법에 대해 설명합니다. Arduino Nano 33 IoT에는 온보드 BLE 모듈이 있습니다. 이 장에서 BLE를 다루어 봅니다. Arduino Nano 33 IoT에서 BLE를 사용하려면 ArduinoBLE 라이브러리를 설치해야합니다.

 

전체 강의자료와 소스코드는 아래를 참고하십시요. 소스코드의 출처는 위의 책 마지막 페이지에 소개되어 있습니다.

 

Arduino Nano 33 IoT 개발 가이드 1 - 개발환경

Arduino Nano 33 IoT 개발 가이드 2 - 기본적인 예제 살펴보기

Arduino Nano 33 IoT 개발 가이드 3 - WiFi Network 실습

Arduino Nano 33 IoT 개발 가이드 4 - 내부 RTC와 Sleep Mode

Arduino Nano 33 IoT 개발 가이드 5 - Arduino Cloud

Arduino Nano 33 IoT 개발 가이드 6 - Accelerator and Gyroscope

Arduino Nano 33 IoT 개발 가이드 7 - Bluetooth Low Energy (BLE)

 

실습에 필요한 전체 소스코드 다운

 

 

스케치-> 라이브러리 포함-> 라이브러리 관리 메뉴를 클릭하여 ArduinoBLE 라이브러리를 추가 할 수 있습니다. 그런 다음 대화 상자가 나타납니다. 검색 텍스트 상자에 ArduinoBLE을 입력하면 ArduinoBLE이 표시됩니다. 아래 그림을 참고하여 라이브러리를 설치하십시오.

 

 

기술적으로 우리는 이것에 대해 ArduinoBLE을 배울 수 있는 문서를 참고하세요. BLE 라이브러리 레퍼런스 문서 링크를 참고하세요. 이 섹션에서는 Arduino의 프로그램 샘플인 LED를 사용합니다. 모바일 장치를 통해 LED를 켜거나 끕니다. 우리가받을 때 숫자 값이 0이면 LED가 켜집니다. 그렇지 않으면 꺼집니다. 이제 Arduino Sketch를 엽니 다. 그런 다음이 프로그램을 Arduino Sketch에 작성하십시오.

 

파일 이름은 led.ino 입니다.

 

/*
  LED

  This example creates a BLE peripheral with service that contains a
  characteristic to control an LED.

  The circuit:
  - Arduino MKR WiFi 1010 or Arduino Uno WiFi Rev2 board

  You can use a generic BLE central app, like LightBlue (iOS and Android) or
  nRF Connect (Android), to interact with the services and characteristics
  created in this sketch.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN; // pin to use for the LED

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        int val = switchCharacteristic.value();
        Serial.println(val);
        if (val>0) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW);          // will turn the LED off
        }
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

 

프로그램을 컴파일하여 Arduino Nano 33 IoT 보드에 업로드하십시오. 그런 다음 직렬 모니터를 열어 프로그램 출력을 볼 수 있습니다. 아래와 같이 볼 수 있습니다.

 

 

BLE 테스트를 위해 Android / iOS에서 BLE 애플리케이션을 사용합니다. 앱은 nRF Connect를 사용하는 것이 좋습니다.

 

 

스마트 폰에 앱을 설치하고 아래 화면을 따라 테스트 한다. 블루투스를 인에이블 시키고 스캔을 하면 LED 가 보인다. 연결을 한다.

 

아래의 Unknown Service를 클릭하여 들어간다.

 

 

서비스 아래 위를 클릭하여 값을 0 이나 0 이상으로 넣어 보낸다. 시리얼 모니터에 LED on, off 상태가 표시됨을 확인한다.  

 

 

실습을 하면 아래 화면과 같은 시리얼모니터를 볼 수 있다.

 

 

여기까지 전부 다 했다. 긴 시간이었다. 내가 좋아하는 게 이런건가 보다. 다음 할 것을 또 찾아보자. 여기까지 하고 또 놀러나가야 겠다. 비오면 비가 와도 좋은 곳이 있으니 그곳으로 가자. 

 

 

 

 

 

 

반응형