Fase 3 ยท Minggu 7

Pengenalan Bluetooth

Bluetooth Classic vs BLE, ESP32 BluetoothSerial, pairing, dan test komunikasi.

Bluetooth Classic vs BLE

ESP32 mendukung 2 jenis Bluetooth: Classic (SPP) dan BLE (Low Energy). Keduanya punya kelebihan masing-masing.

Bluetooth Classic ๐Ÿ“ก Jangkauan: ~10m โšก Daya: Tinggi ๐Ÿ“Š Data rate: ~3 Mbps ๐Ÿ”— Perlu pairing ๐Ÿ“ฑ Seperti serial port Cocok: streaming data banyak Bluetooth Low Energy ๐Ÿ“ก Jangkauan: ~50m โšก Daya: Sangat rendah ๐Ÿ“Š Data rate: ~1 Mbps ๐Ÿ”— Tanpa pairing (bisa) ๐Ÿ“ฆ GATT: Services + Char Cocok: sensor, hemat baterai

Perbandingan Bluetooth Classic vs BLE โ€” ESP32 mendukung keduanya!

โ„น๏ธ Strategi kita: Minggu 7-8 belajar Bluetooth Classic (lebih mudah, seperti serial). Minggu 9 belajar BLE (lebih modern & hemat daya).

ESP32: Bluetooth Classic Setup

Gunakan library BluetoothSerial bawaan ESP32 untuk komunikasi Bluetooth Classic.

C++ (Arduino)#include "BluetoothSerial.h"
#include <ArduinoJson.h>

BluetoothSerial SerialBT;

#define LED_PIN 2

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);

  // Inisialisasi Bluetooth
  SerialBT.begin("ESP32_IoT");  // Nama device BT
  Serial.println("Bluetooth started! Pair dengan 'ESP32_IoT'");
}

void loop() {
  // Terima command via Bluetooth
  if (SerialBT.available()) {
    String input = SerialBT.readStringUntil('\n');
    input.trim();

    JsonDocument doc;
    if (!deserializeJson(doc, input)) {
      const char* cmd = doc["command"];

      if (strcmp(cmd, "ON") == 0) {
        digitalWrite(LED_PIN, HIGH);
        SerialBT.println("{\"status\":\"OK\",\"led\":\"ON\"}");
        Serial.println("BT: LED ON");
      }
      else if (strcmp(cmd, "OFF") == 0) {
        digitalWrite(LED_PIN, LOW);
        SerialBT.println("{\"status\":\"OK\",\"led\":\"OFF\"}");
        Serial.println("BT: LED OFF");
      }
    }
  }

  // Kirim data sensor periodik
  static unsigned long lastSend = 0;
  if (millis() - lastSend > 2000) {
    lastSend = millis();

    JsonDocument doc;
    doc["suhu"] = 25.0 + random(0, 50) / 10.0;
    doc["cahaya"] = analogRead(34);
    doc["led"] = digitalRead(LED_PIN) ? "ON" : "OFF";

    String output;
    serializeJson(doc, output);
    SerialBT.println(output);
  }
}

Test dengan Serial Bluetooth Terminal

Sebelum bikin Flutter app, test dulu pakai app "Serial Bluetooth Terminal" dari Play Store.

Langkah-langkah:

  1. Upload kode di atas ke ESP32
  2. Install "Serial Bluetooth Terminal" dari Play Store
  3. Di HP: Settings โ†’ Bluetooth โ†’ Pair dengan "ESP32_IoT"
  4. Buka Serial Bluetooth Terminal โ†’ Connect ke ESP32_IoT
  5. Kirim: {"command":"ON"} โ†’ LED nyala!
  6. Kirim: {"command":"OFF"} โ†’ LED mati!
BT Terminal > {"command":"ON"} < {"status":"OK","led":"ON"} < {"suhu":27.3,"cahaya":2048} > {"command":"OFF"} < {"status":"OK","led":"OFF"} < {"suhu":26.8,"cahaya":1920} Bluetooth ESP32 "ESP32_IoT" Testing Steps: 1๏ธโƒฃ Pair bluetooth 2๏ธโƒฃ Open BT Terminal app 3๏ธโƒฃ Connect to ESP32_IoT 4๏ธโƒฃ Send {"command":"ON"} 5๏ธโƒฃ Verify LED turns on! โœ“ BT communication works!

Test Bluetooth pakai Serial Bluetooth Terminal sebelum coding Flutter

๐Ÿ’ก Tips: Selalu test hardware terlebih dahulu sebelum menulis kode Flutter. Ini mempermudah debugging โ€” kalau tidak jalan, kamu tahu masalahnya di sisi mana.

Konsep Pairing & SPP

Bluetooth Classic menggunakan SPP (Serial Port Profile) โ€” seperti komunikasi serial virtual melalui udara.

1. Discover Scan devices 2. Pair Confirm PIN 3. Connect SPP channel 4. Data โ†” JSON SPP = Serial Port Profile Data dikirim seperti serial biasa โ†’ SerialBT.print() โ‰ˆ Serial.print()

Flow Bluetooth Classic: Discover โ†’ Pair โ†’ Connect SPP โ†’ Send/Receive Data

Checklist Minggu 7