MENYALAKAN LED MENGGUNAKAN BLUETOOTH LOW ENERGY (BLE) DENGAN BOARD ESP32
Praktikum kali ini yaitu menyalakan led pada board Esp32 menggunakan Bluetooth low energy atau biasa disingkat BLE,
BLE merupakan kepanjangan dari Bluetooth Low Energy. Secara marketing lebih dikenal dengan nama Bluetooth Smart. Bluetooth Low Energy adalah protokol terbaru dari bluetooth dan merupakan bagian dari protokol yang lebih besar yaitu Bluetooth 4.0, spec ini mencakup Bluetooth LE, Bluetooth High Speed dan juga Bluetooth klasik.
Bisa dikatakan BLE ini merupakan generasi kekinian dari Bluetooth klasik. Yang berarti BLE memiliki keunggulan dibanding Bluetooth klasik. Keunggulan Bluetooth LE dibandingkan Bluetooth klasik adalah konsumsi energi listrik dari BLE untuk transfer data jauh lebih kecil dibandingkan dengan Bluetooth klasik tapi dengan jangkauan konektifitas dan kapasitas payload transfer data yang sama.
Alat dan bahan yang digunakan adalah :
- Board Esp32
- Arduino IDE
- Aplikasi BLE Scanner
- Kabel USB untuk menghubungkan board dengan komputer
- Led yang digunakan Led pada board Esp32
![]() |
Board Esp32 |
Sebelum melangkah ke tahap sketch program hal yang perlu disiapkan adalah :
Sketch program :
Setelah selesai menuliskan kode pada Arduino IDE, kemudian pilih board ESP32 dan sesuaikan port yang digunakan selanjutnya upload Sketch Program, tunggu hingga proses upload selesai dan buka serial monitor pilih range 115200 baud, tekan EN pada board.
- Install terlebih dahulu library BLE pada arduino IDE, masuk ke menu tools → manage libraries kemudian pada kotak pencarian tulis "BLEDevice" kemudian install.
BLE library |
- Langkah selanjutnya yaitu install BLE scanner pada smartphone yang akan digunakan, download disini (android).
- Kemudian buka https://www.uuidgenerator.net/ , untuk mendapatkan kode UUID yang akan digunakan di sketch program nanti. UUID singkatan dari Universally Unique Identifier yang dijadikan sebagai standar identifier dengan Open Software Foundation (OSF) sebagai bagian dari Distributed Computing Environment (DCE).
- Pada halaman https://www.uuidgenerator.net/ , salin kode UUID yang muncul pada halaman web.
Kode UUID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <BLEDevice.h> | |
#include <BLEServer.h> | |
#include <BLEUtils.h> | |
#include <BLE2902.h> | |
BLECharacteristic *pCharacteristic; | |
bool deviceConnected = false; | |
float txValue = 0; | |
const int readPin = 32; // Use GPIO number. See ESP32 board pinouts | |
const int LED = 2; // PIN LED PADA BOARD | |
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // MASUKKAN KODE DARI UUID | |
#define CHARACTERISTIC_UUID_RX "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // MASUKKAN KODE DARI UUID | |
#define CHARACTERISTIC_UUID_TX "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // MASUKKAN KODE DARI UUID | |
class MyServerCallbacks: public BLEServerCallbacks { | |
void onConnect(BLEServer* pServer) { | |
deviceConnected = true; | |
}; | |
void onDisconnect(BLEServer* pServer) { | |
deviceConnected = false; | |
} | |
}; | |
class MyCallbacks: public BLECharacteristicCallbacks { | |
void onWrite(BLECharacteristic *pCharacteristic) { | |
std::string rxValue = pCharacteristic->getValue(); | |
if (rxValue.length() > 0) { | |
Serial.println("*********"); | |
Serial.print("Received Value: "); | |
for (int i = 0; i < rxValue.length(); i++) { | |
Serial.print(rxValue[i]); | |
} | |
Serial.println(); | |
if (rxValue.find("ON") != -1) { Serial.println("Turning ON!"); digitalWrite(LED, HIGH); } | |
else if (rxValue.find("OFF") != -1) { Serial.println("Turning OFF!"); digitalWrite(LED, LOW); } Serial.println(); Serial.println("*********"); } } }; | |
void setup() { | |
Serial.begin(115200); pinMode(LED, OUTPUT); // Create the BLE Device | |
BLEDevice::init("ESP32 UART Test"); //ATUR NAMA BLUETOOTH YANG ANDA INGINKAN | |
// Create the BLE Server | |
BLEServer *pServer = BLEDevice::createServer(); | |
pServer->setCallbacks(new MyServerCallbacks()); | |
// Create the BLE Service | |
BLEService *pService = pServer->createService(SERVICE_UUID); | |
// Create a BLE Characteristic | |
pCharacteristic = pService->createCharacteristic( | |
CHARACTERISTIC_UUID_TX, | |
BLECharacteristic::PROPERTY_NOTIFY | |
); | |
pCharacteristic->addDescriptor(new BLE2902()); | |
BLECharacteristic *pCharacteristic = pService->createCharacteristic( | |
CHARACTERISTIC_UUID_RX, | |
BLECharacteristic::PROPERTY_WRITE | |
); | |
pCharacteristic->setCallbacks(new MyCallbacks()); | |
// Start the service | |
pService->start(); | |
// Start advertising | |
pServer->getAdvertising()->start(); | |
Serial.println("Waiting a client connection to notify..."); | |
} | |
void loop() { | |
if (deviceConnected) { | |
// Fabricate some arbitrary junk for now... | |
txValue = analogRead(readPin) / 3.456; // This could be an actual sensor reading! | |
// Let's convert the value to a char array: | |
char txString[8]; // make sure this is big enuffz | |
dtostrf(txValue, 1, 2, txString); // float_val, min_width, digits_after_decimal, char_buffer | |
// pCharacteristic->setValue(&txValue, 1); // To send the integer value | |
// pCharacteristic->setValue("Hello!"); // Sending a test message | |
pCharacteristic->setValue(txString); | |
pCharacteristic->notify(); // Send the value to the app! | |
Serial.print("*** Sent Value: "); | |
Serial.print(txString); | |
Serial.println(" ***"); | |
// You can add the rxValue checks down here instead | |
// if you set "rxValue" as a global var at the top! | |
// Note you will have to delete "std::string" declaration | |
// of "rxValue" in the callback function. | |
// if (rxValue.find("ON") != -1) { | |
// Serial.println("Turning ON!"); | |
// digitalWrite(LED, HIGH); | |
// } | |
// else if (rxValue.find("OFF") != -1) { | |
// Serial.println("Turning OFF!"); | |
// digitalWrite(LED, LOW); | |
// } | |
} | |
delay(1000); | |
} | |
Setelah selesai menuliskan kode pada Arduino IDE, kemudian pilih board ESP32 dan sesuaikan port yang digunakan selanjutnya upload Sketch Program, tunggu hingga proses upload selesai dan buka serial monitor pilih range 115200 baud, tekan EN pada board.
Maka aka muncul seperti gambar dibawah ini:
![]() |
Serial Monitor |
Selanjutnya hubungkan Bluetooth pada board dengan aplikasi BLE Scanner yang telah diinstall di smartphone dengan menekan CONNECT.
Kemudian akan diarahkan ke menu device yang terhubung, pilih CUSTOM SERVICE dan klik huruf W pada sisi kanan layar, maka akan muncul jendela "input text", pada sketch program kita telah mengatur bahwa jika TEXT yang dimasukkan ON maka led akan menyala dan jika OFF maka led akan mati.
*Source IoTbyhvm.ooo
Kemudian akan diarahkan ke menu device yang terhubung, pilih CUSTOM SERVICE dan klik huruf W pada sisi kanan layar, maka akan muncul jendela "input text", pada sketch program kita telah mengatur bahwa jika TEXT yang dimasukkan ON maka led akan menyala dan jika OFF maka led akan mati.
![]() |
Input ON |
Tampilan pada serial monitor |
*Source IoTbyhvm.ooo
Komentar
Posting Komentar