چطور از Bluetooth توی ESP32 استفاده کنم؟

سلام وقتتون بخیر چجوری از Bluetooth توی esp32 استفاده کنم؟

من این ماژول رو گرفتم ولی نمیدونم چجوری میشه bluetooth رو  فعال کرد ممنونم میشم راهنمایی کنید.

سلام

میکروکنترلر ESP32 دارای بلوتوث کلاسیک و BLE می باشد. برای استفاده از هر کدام از این 2 نوع باید داخل برنامه ای که میخواهید از بلوتوث استفاده کنید فعال شده باشد. تمامی نمونه کد های بلوتوث در نمونه مثال های ESP32 در آردوینو آماده است. اما در اینجا یک ساده‌ترین کد برای راه‌اندازی بلوتوث کلاسیک را قرار می دهم.

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#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

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);
}

پس از آپلود این برنامه را بر روی گوشی خود نصب کنید و با اتصال به میکروکنترلر ESP32 از طریق سریال مانیتور می توانید ارتباط برقرار کنید.