• ESP32 Bluetooth Bật Tắt Thiết Bị Cơ Bản(Classic )

QC

ESP32 Bluetooth Bật Tắt Thiết Bị Cơ Bản(Classic )

 ESP32 Bluetooth Bật Tắt Thiết Bị Cơ Bản(Classic ).

PHẦN 1: GIỚI THIỆU.

   Bài này ta sẽ Điều khiển led sáng tắt Thông qua điện thoại cơ bản nhất ! dùng Bluetooth, Những bài tiếp theo mình sẽ hướng dẫn bạn nâng cao  nhé !

PHẦN 1 : HƯỚNG DẪN GỬI KÝ TỰ " HELLO " TỪ APP :" SERIAL  BLUETOOTH TERMINAL ".

PHẦN 2: ĐIỀU KHIỂN  LED SÁNG TẮT TỪ PHẦN MỀM " SERIAL  BLUETOOTH TERMINAL "


PHẦN 1 : HƯỚNG DẪN GỬI KÝ TỰ " HELLO " TỪ APP :" SERIAL  BLUETOOTH TERMINAL ".

Bước 1: Settup Code Arduino nạp vào Esp32.

CODE NHẬN KÍ TỰ :  VÍ DỤ BẠN GỬI "HELLO" thì ESP32 NHẬN RỒI IN RA SERIAL MONITOR "HELLO".

* Chú ý :   SerialBT.begin("ESP32test"); Trong đó "ESP32test" Là tên của ESP32 của bạn. khi bạn nạp vào thì ESP32 BẠN SẼ CÓ TÊN VẬY.


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

 

Bước 2: Các bạn Tải Phần mền Serial Bluetooth. 

    Nhấp vào biểu tượng cài đặt và chọn Ghép nối thiết bị mới (Pair new device). Bạn sẽ nhận được danh sách các thiết bị Bluetooth có sẵn, bao gồm cả ESP32test. Ghép nối với ESP32test.

BẬT PHẦN MỀN LÊN VÀ  TÌM TÊN THIẾT BỊ CẦN KẾT NỐI !


"ESP32test" Là tên ESP32 Mà ta nạp vào. Click ( nhấn vào kết nối ).  


GỬI HELLO XUỐNG ESP32.



ĐÃ NHẬN "HELLO"

PHẦN 2: ĐIỀU KHIỂN  LED SÁNG TẮT TỪ PHẦN MỀM " SERIAL  BLUETOOTH TERMINAL ".
- ĐIỀU KHIỂN LED SỐ 2 SÁNG VÀ TẮT NHÉ !
- Chân số 2 OUTPUTS.

 Code như sau :

#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; // GPIO where LED is connected to const int ledPin = GPIO_NUM_2; String inputString = ""; // a String to hold incoming data bool stringComplete = false; // whether the string is complete bool ledOn = false; // whether the string is complete void setup() { pinMode(ledPin, OUTPUT); Serial.begin(115200); SerialBT.begin("ESP32LED"); //Bluetooth device name Serial.println("The device started, now you can pair it with bluetooth!"); } void loop() { if (SerialBT.available()) { // get the new byte: char inChar = (char)SerialBT.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag so the main loop can // do something about it: if (inChar == '\n') { stringComplete = true; } } if (stringComplete) { inputString.trim(); Serial.println(inputString); if (inputString == "ON") { digitalWrite(ledPin, HIGH); SerialBT.print("ON\r\n"); ledOn = true; } else if (inputString == "OFF") { digitalWrite(ledPin, LOW); SerialBT.print("OFF\r\n"); ledOn = false; } else if (inputString == "Sync") { if (ledOn) SerialBT.print("ON\r\n"); else SerialBT.print("OFF\r\n"); } // clear the string: inputString = ""; stringComplete = false; delay(20);//Time for Bluetooth stack handle } }


Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...