아두이노

[아두이노] ESP32를 이용해서 안드로이드와 데이터 주고 받기

eunda_coding 2023. 3. 10. 12:08

처음으로 아두이노 UNO말고 ESP32를 이용해보았어요

안드로이드와 bluetooth로 데이터를 주고 받는 작업을 해야해서 BT기능을 제공하는 esp32로 구현했습니다

ESP32

아두이노 IDE에서 파일 > 예제 > BluetoothSerial에 들어가면 다양한 예제를 볼 수 있어요

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

setup함수에서 기본 설정을 해줍니다. SerialBT.begin("ESP32test"); → 나중에 안드로이드 기기에서 보일 장치이름입니다

 

loop함수 안에 SerialBT.write()은 블루투스로 데이터를 보내는 것입니다

그 다음 안드로이드에서 보내는 데이터는 SerialBT.read()를 통해 받아서 처리하면 됩니다.

 

 

'아두이노' 카테고리의 다른 글

[아두이노] Arduino IDE에서 ESP32 사용하기  (0) 2023.03.10