Èkọ́ ESP32 37/55 - Lílò Bluetooth App pẹ̀lú ESP32 BLE | SunFounder's ESP32 IoT Learning kit
Nínú ẹ̀kọ́ yìí, a óò ṣàwárí bí a ṣe le lo ESP32 gẹ́gẹ́ bí olùpèsè Bluetooth láti fi àwọn ifiranṣẹ́ ránṣẹ́ sí àti gbà láti inú app fóónù alágbèéká. Èyí máa jẹ́ kí ìbánidọ̀rẹ̀pọ̀ pẹ̀lú bọ́ọ̀dù ESP32 rẹ àti ẹ̀rọ alágbèéká rẹ ṣiṣẹ́, tí ó sì mú kí ó jẹ́ àfikún tí ó wúlò fún àwọn iṣẹ́ IoT rẹ. Nígbà tí o bá parí ẹ̀kọ́ yìí, o óò le fi ọ̀rọ̀ ránṣẹ́ láti ẹ̀rọ alágbèéká rẹ sí ESP32 kí o sì rí i tí a fi hàn lórí ẹ̀rọ ìṣàkóso serial.
A óò lo app LightBlue Explorer, tí ó wà lórí iOS àti Android, láti bá ESP32 ṣe ìbánidọ̀rẹ̀pọ̀. Ẹ̀kọ́ yìí jẹ́ ìpìlẹ̀ fún òye ìbánidọ̀rẹ̀pọ̀ Bluetooth Low Energy (BLE) àti pé ó ṣètò ìpele fún àwọn iṣẹ́ tí ó gbòòrò sí i. Fún ìtọ́sọ́nà ojú, jọ̀wọ́ wo fídíò náà ní (nínú fídíò ní 02:00).
Ohun èlò Tí A Ṣàlàyé
Ohun èlò pàtàkì nínú iṣẹ́ yìí ni microcontroller ESP32, tí ó ní agbára Wi-Fi àti Bluetooth papọ̀. Èyí jẹ́ kí ESP32 ṣiṣẹ́ gẹ́gẹ́ bí olùpèsè, tí ó ń gba àti fi data ránṣẹ́ láìsí okun. Iṣẹ́ Bluetooth tí a kọ́ sínú ESP32 ń ṣe àtìlẹ́yìn fún BLE, tí ó sì mú kí ó ṣiṣẹ́ dáradára fún àwọn ohun èlò tí ń lo agbára díẹ̀.
Yàtọ̀ sí ESP32, a óò lo ẹ̀rọ alágbèéká kan tí a fi app LightBlue sí. App yìí jẹ́ kí àwọn olùṣàmúlò sopọ̀ sí ESP32 kí wọ́n sì fi data ránṣẹ́ nípasẹ̀ Bluetooth. Ìsopọ̀ àwọn ohun èlò wọ̀nyí jẹ́ kí ìbánidọ̀rẹ̀pọ̀ láìsí ìdíwọ́n wà láàárín ESP32 àti àwọn ẹ̀rọ alágbèéká, tí ó sì mú kí ìbáṣepọ̀ olùṣàmúlò pọ̀ sí i.
Àwọn Àlàyé Datasheet
| Olùṣelọ́pọ̀ | Espressif Systems |
|---|---|
| Nọ́mbà apá | ESP32-WROOM-32 |
| Fóltì Logic/IO | 3.3 V |
| Fóltì ìpèsè | 3.0 – 3.6 V |
| Ìjáde lọ́wọ́ (fún ikanni) | 40 mA |
| Ìjáde tí ó ga jù (fún ikanni) | 160 mA |
| Ìtọ́sọ́nà ìgbohùnsá PWM | 1 kHz |
| Àwọn ìpele ìṣàkóso ìwọ̀n | 0.2 VCC (rẹ̀lẹ̀), 0.8 VCC (ga) |
| Ìrẹwẹ́sì fóltì / RDS(on) / ìkún | 0.1 V |
| Àwọn ìwọ̀n ooru | 125 °C |
| Àpótí | QFN48 |
| Àwọn àkíyèsí / àwọn oríṣi | ESP32-WROOM-32, ESP32-WROVER |
- Rí i dájú pé ìpèsè agbára dúró ṣinṣin (3.3 V) láti yẹra fún àwọn ìdínkù agbára.
- Lo àwọn capacitors fún ìyàsọ́tọ̀ nítòsí àwọn ẹsẹ̀ agbára.
- Mú kí ìtújáde ooru tó yẹ bí o bá ń lo agbára gíga.
- Ṣọ́ra pẹ̀lú àwọn ìpele fóltì ẹsẹ̀ GPIO; wọ́n ní ìfaradà 3.3 V.
- Lo àwọn resistors ìfà sókè tàbí ìfà sílẹ̀ bí ó bá pọn dandan fún àwọn ìṣètò GPIO.
- Ṣàkíyèsí àwọn ìsopọ̀ BLE; rí i dájú pé app ti sopọ̀ dáradára.
- Rí i dájú pé àwọn UUID fún àwọn iṣẹ́ àti àwọn àbùdá jẹ́ aláìlẹ́gbẹ́.
- Ṣàkíyèsí ẹ̀rọ ìṣàkóso serial déédéé fún àwọn ifiranṣẹ́ ìtọ́jú.
- Rò nípa lílo olùtúwò logic fún ìṣàwárí àwọn ìṣòro ifihan agbára tí ó díjú.
Àwọn Ìtọ́sọ́nà Ìsopọ̀ Okun
Ìsopọ̀ okun fún iṣẹ́ yìí rọrùn nítorí pé ESP32 ní pàtàkì sopọ̀ nípasẹ̀ USB fún agbára àti ìṣètò. Sopọ̀ ESP32 sí kọ̀ǹpútà rẹ nípa lílo okun micro USB kan. Rí i dájú pé ibùdó USB ń pèsè agbára tí ó tó (ní gbogbogbòò 5 V). A óò lo ẹ̀rọ ìṣàkóso serial fún ìtọ́jú, nítorí náà kò sí àwọn ìsopọ̀ ohun èlò àfikún tí a nílò fún ohun èlò yìí.
Nígbà tí o bá ń lo ESP32 pẹ̀lú àwọn ohun èlò òde ní àwọn iṣẹ́ ọjọ́ iwájú, rántí láti sopọ̀ àwọn ẹsẹ̀ ilẹ̀ sí ilẹ̀ kan náà. Èyí rí i dájú pé ESP32 àti àwọn sẹ́nsà tàbí àwọn modulu tí a sopọ̀ pín ojú ìtọ́kasí kan náà. Pẹ̀lúpẹ̀, bí o bá ń lo bátìrí, sopọ̀ ẹsẹ̀ rere sí ẹsẹ̀ 3.3 V àti ẹsẹ̀ odi sí ẹsẹ̀ ilẹ̀ kan lórí ESP32.
Àwọn Àpẹẹrẹ Kóòdù & Ìtọ́sọ́nà
Kóòdù tí a pèsè ń bẹ̀rẹ̀ olùpèsè Bluetooth, ń ṣètò àwọn iṣẹ́ àti àwọn àbùdá tí a nílò, àti ń ṣàkóso àwọn ifiranṣẹ́ tí ń wọlé. Àwọn ìdámọ̀ pàtàkì ní bleName, tí ó ń ṣàlàyé orúkọ ẹ̀rọ Bluetooth, àti receivedText, tí ó ń fi àwọn ifiranṣẹ́ tí ń wọlé pamọ́ láti inú app fóónù alágbèéká.
const char *bleName = "ESP32_Bluetooth";
String receivedText = "";
Iṣẹ́ setup() ń bẹ̀rẹ̀ ìbánidọ̀rẹ̀pọ̀ serial àti ìṣètò BLE. Èyí ṣe pàtàkì fún ìdásílẹ̀ ìsopọ̀ pẹ̀lú app LightBlue.
void setup() {
Serial.begin(115200); // Bẹ̀rẹ̀ ibùdó serial
setupBLE(); // Bẹ̀rẹ̀ Bluetooth BLE
}
Nínú iṣẹ́ loop(), kóòdù ń ṣàkíyèsí àwọn ifiranṣẹ́ tí ń wọlé. Bí a bá gba ifiranṣẹ́ tuntun kan, a óò tẹ́ ẹ sílẹ̀ lórí ẹ̀rọ ìṣàkóso serial, àti pé a óò fi ìfiranṣẹ́ ránṣẹ́ sí ẹ̀rọ BLE tí a sopọ̀.
if (receivedText.length() > 0 && millis() - lastMessageTime > 1000) {
Serial.print("Received message: ");
Serial.println(receivedText);
pCharacteristic->setValue(receivedText.c_str());
pCharacteristic->notify();
receivedText = "";
}
Fún òye pípé, wo kóòdù kíkún tí ó ń gbé sílẹ̀ nísàlẹ̀ àpilẹ̀kọ yìí. Èyí yóò pèsè gbogbo àwọn àlàyé tí a nílò fún ọ láti ṣe iṣẹ́ náà ní àṣeyọrí.
Ìfihàn / Ohun Tí O Lè Rètí
Nígbà tí o bá ti ṣe ètò náà ní àṣeyọrí, o óò le fi àwọn ifiranṣẹ́ ránṣẹ́ láti app LightBlue sí ESP32. Bí o ṣe ń tẹ ifiranṣẹ́ kan, bíi "Hello," yóò farahàn lórí ẹ̀rọ ìṣàkóso serial. Pẹ̀lúpẹ̀, o le fi àwọn ifiranṣẹ́ ránṣẹ́ padà sí app, tí ó ń fi ìbánidọ̀rẹ̀pọ̀ ọ̀nà méjì mọ́lẹ̀. Bí o bá pàdé àwọn ìṣòro bíi àwọn ifiranṣẹ́ tí kò farahàn, rí i dájú pé ESP32 ti sopọ̀ dáradára pẹ̀lú app àti pé àwọn UUID bá mu.
Àwọn Àsìkò Fídíò
- 00:00 Ìbẹ̀rẹ̀
- 2:10 Ìfihàn sí iṣẹ́-ìdásílẹ̀ náà
- 2:45 Ojú-ewé ìwé-àkọsílẹ̀
- 4:04 Kóòdù Arduino
- 6:31 Fífi Ẹ̀rọ-ìkọ̀wé Bluetooth sílẹ̀
- 7:12 Yíyàn bọ́ọ̀dù ESP32 àti èbúté COM
- 8:54 Ìfihàn iṣẹ́-ìdásílẹ̀ náà
#include "BLEDevice.h"
#include "BLEServer.h"
#include "BLEUtils.h"
#include "BLE2902.h"
// Define the Bluetooth device name
const char *bleName = "ESP32_Bluetooth";
// Define the received text and the time of the last message
String receivedText = "";
unsigned long lastMessageTime = 0;
// Define the UUIDs of the service and characteristics
#define SERVICE_UUID "your_service_uuid_here"
#define CHARACTERISTIC_UUID_RX "your_rx_characteristic_uuid_here"
#define CHARACTERISTIC_UUID_TX "your_tx_characteristic_uuid_here"
// Define the Bluetooth characteristic
BLECharacteristic *pCharacteristic;
void setup() {
Serial.begin(115200); // Initialize the serial port
setupBLE(); // Initialize the Bluetooth BLE
}
void loop() {
// When the received text is not empty and the time since the last message is over 1 second
// Send a notification and print the received text
if (receivedText.length() > 0 && millis() - lastMessageTime > 1000) {
Serial.print("Received message: ");
Serial.println(receivedText);
pCharacteristic->setValue(receivedText.c_str());
pCharacteristic->notify();
receivedText = "";
}
// Read data from the serial port and send it to BLE characteristic
if (Serial.available() > 0) {
String str = Serial.readStringUntil('\n');
const char *newValue = str.c_str();
pCharacteristic->setValue(newValue);
pCharacteristic->notify();
}
}
// Define the BLE server callbacks
class MyServerCallbacks : public BLEServerCallbacks {
// Print the connection message when a client is connected
void onConnect(BLEServer *pServer) {
Serial.println("Connected");
}
// Print the disconnection message when a client is disconnected
void onDisconnect(BLEServer *pServer) {
Serial.println("Disconnected");
}
};
// Define the BLE characteristic callbacks
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
// When data is received, get the data and save it to receivedText, and record the time
std::string value = std::string(pCharacteristic->getValue().c_str());
receivedText = String(value.c_str());
lastMessageTime = millis();
Serial.print("Received: ");
Serial.println(receivedText);
}
};
// Initialize the Bluetooth BLE
void setupBLE() {
BLEDevice::init(bleName); // Initialize the BLE device
BLEServer *pServer = BLEDevice::createServer(); // Create the BLE server
// Print the error message if the BLE server creation fails
if (pServer == nullptr) {
Serial.println("Error creating BLE server");
return;
}
pServer->setCallbacks(new MyServerCallbacks()); // Set the BLE server callbacks
// Create the BLE service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Print the error message if the BLE service creation fails
if (pService == nullptr) {
Serial.println("Error creating BLE service");
return;
}
// Create the BLE characteristic for sending notifications
pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);
pCharacteristic->addDescriptor(new BLE2902()); // Add the descriptor
// Create the BLE characteristic for receiving data
BLECharacteristic *pCharacteristicRX = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE);
pCharacteristicRX->setCallbacks(new MyCharacteristicCallbacks()); // Set the BLE characteristic callbacks
pService->start(); // Start the BLE service
pServer->getAdvertising()->start(); // Start advertising
Serial.println("Waiting for a client connection..."); // Wait for a client connection
}
Common Course Links
Common Course Files
Orisun & itọkasi
-
Iwe-ẹriESP32 Tutorial 37/55- SunFounder doc page for Bluetooth appdocs.sunfounder.com
Fáìlìs📁
Ko si awọn faili ti o wa.