디지털 라이프 電子的生活 My Digital Life/아두이노 (Arduino)

Bluno Beetle과 Blynk 간의 BLE 통신 실험

미친도사 2020. 2. 7. 20:55

아두이노 + 블루투스 모듈 + 스마트폰앱으로 탱크도 굴려보고, 자동차도 굴려봤는데요...
이렇게 해보면서 늘 고민인 게, 작은 공간에 회로를 집어넣는 것입니다.

[아카데미] 1/35 M113A1 장갑차 베트남전 - 완성

[Orlandoo] 1/35 F-150 4WD Crawler Kit - 완성

[엘리트모형] Gepard - Comback Tank Series 3의 무선 RC화 (2)회로 구성 및 기타

나름 작게 만든다고 했지만, 더 작게 만들면 활용할 수 있는 게 더 많을 것 같거든요.

그래서 찾다보니, Bluetooth 모듈이 내장된 것 같은 아두이노 보드들이지요.
Blueno 계열, 최근엔 Arduino Nano 33 BLE 같은 보드들이 BLE (Bluetooth Low Energy)란 기능이 내장되어 있습니다.
그런데, 이런 BLE 내장 아두이노 보드를 만져보면 HC-06 같은 블루투스처럼 연결하고자 하면 안 됩니다. 일단 RX/TX 포트 지정이란 개념이 없는 것 같더라고요.

그래서 사놓고는 안 만지고 있었는데, 최근에 다시 Blynk앱을 만지다 보니, 이 앱이 BLE를 지원하고 있어 다시 좀 들여다 보기 시작했습니다.

하루 정도 만져보고 대충 이해가 되어서 정리 차원에서 글 남겨 봅니다.

이번에 사용해본 보드는 DFRobot이란 곳에서 만든 Bluno Beetle이란 보드입니다.
https://www.dfrobot.com/product-1259.html?search=beetle&description=true&category_id=48

 

Beetle BLE - The smallest Arduino bluetooth 4.0 (BLE) - DFRobot

You have choosen:[[togetherChouseinfo.num]] Total amount: [[currency]][[togetherChouseinfo.price]] [[togetherChouseinfo.price]][[currency]]

www.dfrobot.com

보드가 500원짜리 동전만 한데, 디지털 I/O가 4개, 아날로그 I/O가 4개인 보드이며, IDE에서는 Arduino UNO로 설정해서업로드하면 됩니다. USB단자가 많이 쓰는 마이크로 B 단자여서 편하기도 합니다.

일단 Blynk 라이브러리를 설치하면 포함되어 있는 샘플 중에 DFRobot_Bluno_BLE_Link란 샘플로 시작해 봅니다.

/*************************************************************
  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  This example shows how to use DFRobot Bluno or BLE Link module
  to connect your project to Blynk.

  Please be sure to update your DFRobot firmware to at least V1.97:
    https://github.com/DFRobot/BLE_firmware_V1.9

  For Bluno, read instructions here:
    https://www.dfrobot.com/wiki/index.php/Bluno_SKU:DFR0267

  For BLE-Link, read instructions here:
    https://www.dfrobot.com/wiki/index.php/BLE-Link_(SKU:TEL0073)

  NOTE: BLE support is in beta!

 *************************************************************/

//#define BLYNK_USE_DIRECT_CONNECT

// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h> 
SoftwareSerial DebugSerial(2, 3); // RX, TX // [미친도사] 외부 디버거 안 쓸거니까 필요 없음

#define BLYNK_PRINT DebugSerial // [미친도사] 삭제 가능!

#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

void setup()
{
  // Debug console
  DebugSerial.begin(9600); // [미친도사] 삭제 가능

  DebugSerial.println("Waiting for connections..."); // [미친도사] 삭제 가능

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(115200);
  Blynk.begin(Serial, auth);
}

void loop()
{
  Blynk.run();
}

이 코드 중에서 일반적으로 필요 없는 부분도 있는데, 그 부분은 주석 달았으니 참고하시고요.

Blynk앱을 깔고 나고 컨트롤러를 생성할 때 만들어지는 인증 코드를 YourAuthToken에 넣고 업로드 해야 합니다.
업로드를 하고,  Blynk 앱에서 BLE 아이콘을 하나 추가해서 누르면 장치 연결 항목이 뜨고 Bluno를 찾아 선택하면 연결이 될 겁니다.

연결이 되었는데, 할 수 있는 게 없잖아요. 그럼 다음으로 생각해 볼 수 있는게, 보드에 내장된 LED(D13)를 한번 켜보는 거죠. 코드는 그대로 두고, 앱에서 Button을 하나 추가한 후에 그 버튼에 D13을 링크합니다.

앱이 수정되면 보드와 연결을 재설정해야 합니다. 끊었다 다시 연결하는 거죠. 그리고, 새로 만든 버튼을 누르면 보드의 테스트 LED가 깜빡일 겁니다. Button 속성에 따라 누를 때만 켜지게 할 수도 있고, 누를 때마다 켜졌다 꺼졌다를 토글하게 할 수도 있습니다. 

일단 BLE로 스마트폰 앱과 보드가 통신해서 제어 가능하다는 게 확인이 된 겁니다. 그럼 이제 다 된 거죠.

이제 아날로그 조이스틱 기능을 추가해 봅니다.

앱에서 아날로그 조이스틱을 하나 추가했고요, 코드를 좀 수정합니다.
어디를 손봐야 하는지 감이 없어서 시행착오를 좀 겪었으나, 아래와 같이 하면 입력을 읽어서 시리얼 모니터로 내볼 수 있었습니다.

#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>

char auth[] = "YourAuthToken";

void setup()
{
  Serial.begin(115200);
  Blynk.begin(Serial, auth);
}

BLYNK_WRITE(V0)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
}

void loop()
{
  Blynk.run();
  Serial.print(V0);
  Serial.print(" ");
  Serial.print(V1);
  Serial.print(" ");
  Serial.println(digitalRead(13));
  delay(100);
}

이렇게 해서 앱에 아날로그 조이스틱의 두 축을 V0, V1에 할당하고, 보드에 업로드한 후에 연결을 다시 해보면...
조이스틱을 움직이면 그 값이 시리얼 모니터로 나오긴 합니다. 그런데, 손을 떼거나 변화가 없으면 값이 안 옵니다.

그래서, 조금 고민하다가 다음과 같이 수정했습니다.

#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

int X;
int Y;

void setup()
{
  Serial.begin(115200);
  Blynk.begin(Serial, auth);
}

BLYNK_WRITE(V0)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  X = pinValue;
}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  Y = pinValue;
}

void loop()
{
  Blynk.run();
  Serial.print(X);
  Serial.print(" ");
  Serial.print(Y);
  Serial.print(" ");
  Serial.println(digitalRead(13));
  delay(100);
}

이렇게 하면 조이스틱에서 손을 떼더라도 X축, Y축 값이 읽힙니다. 버튼 누르면 LED 켜고 꺼지고요.

이 정도면 자동차, 탱크 제어하는 데 어려움이 없을 것 같습니다.

BLE를 포함한 보드와 통신이 된다고 하면, 앞으로 소형 RC 만들 때 큰 도움이 될 것 같습니다. 일단 지금 만들기 시작한 1/35 트럭부터 적용해 볼 예정입니다. 블로그를 통해 업데이트하겠습니다.

반응형