반응형

앞선 포스트 "DFplayer - 아두이노 사운드 모듈"의 ESP32 DevKit 제어 코드 입니다.

 

ESP32 + DFplayer + wifi / 블루투스 2.0 / 블루투스 4.0 BLE mp3 플레이어

 

  

아래 코드는  ESP32 블루투스 2.0 라이브러리 이용 기본 테스트 코드이다. 

DFplayer_esp32_BT_basic.zip
0.00MB

#include <HardwareSerial.h> 

#define DF_rxPin 17 // DFplayer RX -> arduino 17

#define DF_txPin 16 // DFplayer TX -> arduino 16 

HardwareSerial dfSerial(2);  // 이름 정의, 시리얼 이름이 Serial1, 2 가 아닌경우 사용 

 

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

 

void setup() {

  Serial.begin(115200);   // bluetooth Serial 

  dfSerial.begin(9600,SERIAL_8N1, DF_txPin, DF_rxPin);  // (통신속도, UART모드, RX핀번호, TX핀번호) - 핀번호 지정가능

  SerialBT.begin("ESP32test"); //Bluetooth device name

}

 

void loop() {

  if (dfSerial.available()) {    

    SerialBT.write(dfSerial.read());  // send to android via bluetooth

  }

  if (SerialBT.available()) {         

    dfSerial.write(SerialBT.read());  // send to DFplayer

  }

}

 

ESP32의 블루투스 2.0 시리얼 라이브러리 이용 제어하는 코드이다. 

ESP32에서 블루투스 2.0을 아두이노에서 모듈(HC-06)을 연결한 것과 같이 비슷하게 사용할 수 있도록 하는것이 블루투스 시리얼 라이브러리이다. 라이브러리 설정과 블루투스시리얼 시작코드 등이 아두이노의 모듈 사용과 매우 비슷하다.

 

 

 

블루투스2.0 시리얼 라이브러리 설정

#include "BluetoothSerial.h"

 

BluetoothSerial SerialBT;

 

SerialBT.begin("ESP32test"); //Bluetooth device name

 

DFplayer_esp32_BT.zip
0.00MB

 

아래코드는 ESP32 wifi 기능을 이용하여 제어하는 코드이다. 

NodeMcu의 wifi 이용 코드와 거의 비슷하며, 아래 코드만 변경 시켜주었다. 

 

wifi 라이브러리

 

#include <WiFi.h>

#include <WiFiClient.h>

#include <WiFiAP.h>

#define AP_SSID  "Esp32"

#define AP_PASS  "1234test"

 

시리얼 관련 코드 

 

#include <HardwareSerial.h> 

#define DF_rxPin 17 // DFplayer RX -> arduino 17

#define DF_txPin 16 // DFplayer TX -> arduino 16 

HardwareSerial dfSerial(2);  // 이름 정의, 시리얼 이름이 Serial1, 2 가 아닌경우 사용

 

dfSerial.begin(9600,SERIAL_8N1, DF_txPin, DF_rxPin);  // (통신속도, UART모드, RX핀번호, TX핀번호) - 핀번호 지정가능

 

랜덤 함수 아날로그값 읽기 핀번호

 

randomSeed(analogRead(36)+random(0,50)); // NodeMCU 아날로그 입력 A0

 

wifi 클라이언트 관련 코드: NodeMcu에서 아래 코드를 사용할 경우 데이터 수신에 문제가 있어 제거 했던 코드이다.

 

client.stop();

DFplayer_esp32_wifi.zip
0.00MB

 

아래코드는 ESP32 블루투스 4.0 BLE 기능을 이용하여 제어하는 코드이다. 

 

BLE라이브러리 관련 코드

 

#include <HardwareSerial.h> 

#define DF_rxPin 17 // DFplayer RX -> arduino 17

#define DF_txPin 16 // DFplayer TX -> arduino 16 

HardwareSerial dfSerial(2);  // 이름 정의, 시리얼 이름이 Serial1, 2 가 아닌경우 사용 

 

#include <BLEDevice.h>

#include <BLEServer.h>

#include <BLEUtils.h>

#include <BLE2902.h>

 

BLEServer *pServer = NULL;

BLECharacteristic * pTxCharacteristic;

bool deviceConnected = false;

bool rx_received = false;      // BLE 수신시 함수 실행 플래그

std::string rxValue; // BLE 클래스 수신 데이터 값 전역변수 설정

 

#define SERVICE_UUID           "0000FFE0-0000-1000-8000-00805F9B34FB" // UART service UUID

#define CHARACTERISTIC_UUID_RX "0000FFE1-0000-1000-8000-00805F9B34FB"

#define CHARACTERISTIC_UUID_TX "0000FFE2-0000-1000-8000-00805F9B34FB"

 

class MyServerCallbacks: public BLEServerCallbacks {

  void onConnect(BLEServer* pServer) {

    deviceConnected = true;

  };

  void onDisconnect(BLEServer* pServer) {

    deviceConnected = false;

  }

};

 

class MyCallbacks: public BLECharacteristicCallbacks {

  void onWrite(BLECharacteristic *pCharacteristic) {

    rxValue = pCharacteristic->getValue();

    rx_received = true;

  }

};

 

 

 

 

 

setup() 함수 BLE 시작

 

  BLEDevice::init("ESP32 UART");  // Create the BLE Device

  pServer = BLEDevice::createServer();  // Create the BLE Server

  pServer->setCallbacks(new MyServerCallbacks());

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

  pTxCharacteristic = pService->createCharacteristic (  // Create a BLE Characteristic

                      CHARACTERISTIC_UUID_TX,

                      BLECharacteristic::PROPERTY_NOTIFY

                    );

  pTxCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic * pRxCharacteristic = pService->createCharacteristic (

                                          CHARACTERISTIC_UUID_RX,

                                          BLECharacteristic::PROPERTY_WRITE

                                        );

  pRxCharacteristic->setCallbacks(new MyCallbacks());

  pService->start();  // Start the service

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

  Serial.println("Waiting a client connection to notify...");

 

블루투스 수신부

BLE 라이브러리 관련 코드에서 String으로 정의된 변수 rxValue에 1byte 값을 들어온 순서대로 저장하게 된다. 하지만 rxValue가 순수한 String 변수는 아닌것 같으며, String class 코드들이 적용되지 않은것을 확인했다. 

BLE는 한번에 최대 20byte 까지 송 수신하게 된다. 따라서 rxValue를 크기가 20byte인 배열로서 취급해야 코드 적용이 된다. 

 

void BLE_read() {

  if (rx_received == true) {         

    if (get_pin_val != true && rxValue[0] == 0xF0) get_pin_val = true;

    else if (get_pwm_val != true && rxValue[0] == 0xF3) get_pwm_val = true;

    if (get_pin_val == true) {  // 디지털 핀 제어값 수신

      pin_a[0] = rxValue[0];

      pin_a[1] = rxValue[1];

      pin_a[2] = rxValue[2];

      if (pin_a[0] == 0xF0 && pin_a[2] == 0xF1) { // 수신값 검증 완료시

        pin_control();    // 디지털 핀 출력함수 실행}

      }

      get_pin_val = false;

    }

    else if (get_pwm_val == true) { // pwm 제어값 수신

      pwm_a[0] = rxValue[0];

      pwm_a[1] = rxValue[1];

      pwm_a[2] = rxValue[2];

      pwm_a[3] = rxValue[3];

      pwm_a[4] = rxValue[4];

      if (pwm_a[0] == 0xF3 && pwm_a[4] == 0xF1) { // 수신값 검증 완료시

        pwm_control(); // PWM 출력함수 실행

      }

      get_pwm_val = false;

    }

    else {

      general_use();  // 텍스트 출력함수

    }

    rx_received = false;

  }

}

 

 

송신부

텍스트 송신

 

pTxCharacteristic->setValue("Play Next");

pTxCharacteristic->notify();

 

배열 송신 

 

uint8_t pwm_slide[] = {0xF4, 0, 0, 0, 0xF1};

 

pTxCharacteristic->setValue((uint8_t*)&pwm_slide, 5);

pTxCharacteristic->notify();

 

문자열 송신 : 스트링을 배열에 저장하고 배열을 전송해야한다. 전송가능 스트링 길이는 20byte이다.

 

String s = "";

 

char temp [21]; 

s.toCharArray(temp, s.length()+1); 

pTxCharacteristic->setValue(temp); 

pTxCharacteristic->notify();

 

arduino bluetooth controller PWM 앱에서 블루투스 연결시 연결옵션을 "ESP32 BLE"를 선택한 뒤 연결해주어야 한다. 

DFplayer_esp32_BLE.zip
0.01MB

 

 

관련 글

[arduino] - DFplayer - 아두이노 사운드 모듈

[arduino] - NodeMcu(ESP8266)에서 DFplayer를 제어하는 코드

[arduino] - ESP32(DevKit)로 DFplayer 제어하기

[arduino] - 안드로이드 앱 DFcontroller를 이용하여 DFplayer 제어

[arduino] - 아두이노시계 예제, ESP01 WiFi 이용 시간 동기화 하기

[arduino] - 아두이노 말하는알람시계 예제 - DFPlayer

[arduino] - 말하는알람시계 - 블루투스 연결 및 시간 동기화, DFPlayer 제어

[arduino] - NodeMcu - 말하는 알람시계, wifi이용 시간 동기화 및 DFPlayer 원격제어

 

 

+ Recent posts