NodeMCU ESP32 Bluetooth Communication with Serial Bluetooth Terminal Application

บทความนี้เป็นการทดลองการสื่อสารระหว่าง NodeMCU ESP32 และแอพพลิเคชั่นบนโทรศัพท์มือถือผ่านโมดูลบูลทูธ สำหรับเป็นแนวความคิดของการประยุกต์ใช้งานสำหรับสื่อสารข้อลหรือการควบคุมอุปกรณ์ไฟฟ้าต่างๆ เป็นต้น โดยในการทดลองจะเป็นจะแบ่งสำหรับทบความนี้จะแบ่งออกเป็น 3 ส่วนคือ ส่วนแรกลักษณะของโปรแกรม Arduino และการใช้ไลบารี่สำหรับการเชื่อมกับแอพพลิเคชั่น ส่วนที่สองการควบคุม แอลอีดีบน NodeMCU ESP32 แบบ ON/OFF ผ่านแอพพลิเคชั่น และส่วนที่สามการควบคุมแอลอีดีพร้อมทั้งการส่งข้อมูลจาก NodeMCU ESP32 มายังแอพพลิเคชั่นบนโทรศัพท์มือถือตามลำดับ

NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 1 ดาวน์โหลดแอพพลิเคชั่น Serial Bluetooth Terminal
NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 2 แสดงลักษณะของแอพพลิเคชั่น Serial Bluetooth Terminal ที่ติดตั้งเสร็จแล้ว

รูปที่ 1 ดาวน์โหลดแอพพลิเคชั่น Serial Bluetooth Terminal ใน Google Play store จากนั้นทำการติดตั้ง ซึ่งเมื่อเราติดตั้งเสร็จเรียบร้อยแล้ว ขั้นตอนต่อไปจะเป็นการเชื่อมต่อกับ NodeMCU ESP32 ในรูปที่ 2 ลักษณะของแอพพลิเคชั่น Serial Bluetooth Terminal ที่ติดตั้งเสร็จแล้ว

Arduino Library for BluetoothSerial.h

/*
  Arduino Code By : https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/
*/ 

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

การทดลองที่ 1 เป็นการทดลองการเชื่อมต่อระหว่าง NodeMCU ESP32 และ Serial Bluetooth Terminal ด้วยการใช้โปรแกรมคำสั่ง Arduino ที่แสดงข้างบน โปรแกรมไปยัง NodeMCU ESP32 โดยใช้ไลบารี BluetoothSerial.h จากนั้นให้เปิดแอพพลิเคชั่นและสังเกตข้อความแจ้งการเชื่อมต่อที่แสดงให้ทราบดังรูปที่ 3 และรูปที่ 4

รูปที่ 3 การเชื่อมต่อบลูทูธผ่านแอพลิเคชั่น (รูปซ้าย) หรือเชื่อมต่อผ่านเมนูตั้งค่าในโทรศัพท์
NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 4 การเชื่อมต่อระหว่าง NodeMCU ESP32 และ Serial Bluetooth Terminal

จากนั้นทดลองการส่งข้อความเบื้องต้นระหว่าง NodeMCU ESP32 และแอพพลิเคชั่น Serial Bluetooth Terminal ด้วยการส่งข้อความ 1234 จากแอพพลิเคชั่น Serial Bluetooth Terminal ไปยังโปรแกรม Arduino IDE ที่เชื่อมต่อกับ บอร์ด NodeMCU ESP32 อยู่ และจากนั้นทดลองส่งข้อความ 56789 จากโปรแกรม Arduino IDE กลับมายังแอพพลิเคชั่น Serial Bluetooth Terminal อีกครั้ง ผลการทดลองแสดงดังในรูปที่ 5 และรูปที่ 6

NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 5 แสดงข้อความบนแอพพลิเคชั่น Serial Bluetooth Terminal
NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 6 แสดงข้อความบนโปรแกรม Arduino IDE ที่เชื่อมต่อกับบอร์ด NodeMCU ESP32

ในรูปที่ 5 ข้อความบนแอพพลิเคชั่น Serial Bluetooth Terminal โดยในตัวอย่างจะเป็นเลข 1234 เป็นข้อความสีฟ้า เพื่อให้ทราบว่าข้อความถูกส่งออกไปและจะแสดงที่โปรแกรม Arduino IDE ช่องรับข้อความในรูปที่ 6 เป็น 1234 เช่นกัน จากนั้นในโปรแกรม Arduino IDE จะส่งข้อความ 56789 ออกไป โดยแอพพลิเคชั่น Serial Bluetooth Terminal จะแสดงข้อความเป็นสีเขียวให้ทราบ

/*  
  Arduino Code By : https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/
*/

// Load libraries
#include "BluetoothSerial.h" 

// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object
BluetoothSerial SerialBT;

// GPIO where LED is connected to
const int ledPin =  2;  // Pin 25
// Handle received and sent messages
String message = "";
char incomingChar;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);  
  SerialBT.begin("ESP32test");  // Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
 
  // Read received messages (LED control command)
  if (SerialBT.available()){
    char incomingChar = SerialBT.read();
    if (incomingChar != '\n'){
      message += String(incomingChar);
    }
    else{
      message = "";
    }
    Serial.write(incomingChar);  
  }
  // Check received message and control output accordingly
  if (message =="led_on"){
    digitalWrite(ledPin, HIGH); 
  }
  else if (message =="led_off"){
    digitalWrite(ledPin, LOW);
  }
  delay(10);
}

การทดลองที่ 2 เป็นการให้แอพพลิเคชั่น Serial Bluetooth Terminal สามารถควบคุมการติดหรือดับแอลอีดีที่อยู่บนบอร์ด NodeMCU ESP32 ได้ ด้วยการส่งข้อความ led_on เพื่อให้แอลอีดีติดสว่างและเมื่อส่งข้อความ led_off แอลอีดีจะดับลง และใช้โปรแกม Arduino ที่แสดงข้างบน โปรแกรมไปยัง NodeMCU ESP32 ใหม่

NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 7 การส่งข้อความเพื่อให้แอลอีดีติดหรือดับ
NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 8 แอลอีดีติดสว่างเมื่อส่งข้อความ led_on
NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 9 แอลอีดีดับลงเมื่อส่งข้อความ led_off

ในรูปที่ 7 ถึงรูปที่ 9 เป็นผลการทดลองที่ 2 โดยการให้แอพพลิเคชั่น Serial Bluetooth Terminal สามารถควบคุมการทำงานของแอลอีดีบนบอร์ด NodeMCU ESP32 แบบ ON/OFF เพื่อให้เข้าใจโปรแกรมคำสั่งได้ง่าย

/*  
  Arduino Code By : https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/
*/

// Load libraries
#include "BluetoothSerial.h" 

// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object
BluetoothSerial SerialBT;

// GPIO where LED is connected to
const int ledPin =  2; // Pin 25

// Handle received and sent messages
String message = "";
char incomingChar;

// Timer: auxiliar variables
unsigned long previousMillis = 0;    // Stores last time temperature was published
const long interval = 5000;          // interval at which to publish sensor readings

void setup() {
  pinMode(ledPin, OUTPUT); 
  Serial.begin(115200);  
  SerialBT.begin("ESP32test");  // Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  unsigned long currentMillis = millis();
  // Send temperature readings
  if (currentMillis - previousMillis >= interval){
    previousMillis = currentMillis; 
    int int_random = random(1,1023);
    SerialBT.println(int_random); 
  }
  // Read received messages (LED control command)
  if (SerialBT.available()){
    char incomingChar = SerialBT.read();
    if (incomingChar != '\n'){
      message += String(incomingChar);
    }
    else{
      message = "";
    }
    Serial.write(incomingChar);  
  }
  // Check received message and control output accordingly
  if (message =="led_on"){
    digitalWrite(ledPin, HIGH); 
  }
  else if (message =="led_off"){
    digitalWrite(ledPin, LOW);
  }
  delay(10);
}

การทดลองที่ 3 เป็นการให้แอพพลิเคชั่น Serial Bluetooth Terminal สามารถควบคุมการติดหรือดับแอลอีดีที่อยู่บนบอร์ด NodeMCU ESP32 ได้เช่นเดิม นอกจากนี้ให้บอร์ด NodeMCU ESP32 ส่งข้อมูลที่สุ่มตัวเลขในช่วง (0-1023) กลับมาที่แอพพลิเคชั่น Serial Bluetooth Terminal เพิ่มเติม โดยใช้โปรแกม Arduino ที่แสดงข้างบนโปรแกรมไปยัง NodeMCU ESP32 ใหม่อีกครั้ง

NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 10 บอร์ด NodeMCU ESP32 ส่งข้อมูลที่สุ่มตัวเลขในช่วง (0-1023) และการส่งข้อความเพื่อให้แอลอีดีติดหรือดับ
รูปที่ 11 การใช้ปุ่ม M1 และ M2 เพื่อส่งข้อความ led_on และ led_off ด้วยการกดค้างไว้
รูปที่ 12 การกำหนดข้อความให้ปุ่ม M1 และ M2
NodeMCU ESP32 Bluetooth Communication with Arduino
รูปที่ 13 แอลอีดีติดสว่างเมื่อส่งข้อความ led_on (รูปซ้าย) แอลอีดีดับลงเมื่อส่งข้อความ led_off

ในรูปที่ 10 และรูปที่ 13 แสดงผลการทดลองที่ 3 ด้วยการให้แอพพลิเคชั่น Serial Bluetooth Terminal สามารถควบคุมการติดหรือดับแอลอีดีที่อยู่บนบอร์ด NodeMCU ESP32 ได้เช่นเดิม ในรูปที่ 9 จะเห็นข้อความที่แสดงในกรอบสีแดงและสีเหลือง จากนั้นบอร์ด NodeMCU ESP32 ส่งข้อมูลที่สุ่มตัวเลขกลับมา ดังแสดงในกรอบสีฟ้าของแอพพลิเคชั่น Serial Bluetooth Terminal และสำหรับบทความนี้คงพอจะเป็นแนวทางให้ผู้อ่านเข้าใจการสื่อสารและนำไปประยุกต์ใช้งานระหว่างแอพพลิเคชั่น Serial Bluetooth Terminal บนโทรศัพท์มือถือและโมดูลบูลทูธในบอร์ด NodeMCU ESP32 เพิ่มขึ้นครับ.

Reference

  1. https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/
  2. https://circuits4you.com/2018/12/31/esp32-wroom32-devkit-analog-read-example/
  3. https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/
  4. https://www.electronicwings.com/esp32/esp32-bluetooth-getting-started
  5. https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/DiscoverConnect/DiscoverConnect.ino
  6. https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial