NodeMCU ESP32 Bluetooth Communication with Serial Bluetooth Terminal Application

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


รูปที่ 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


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


ในรูปที่ 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 ใหม่



ในรูปที่ 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 ใหม่อีกครั้ง




ในรูปที่ 10 และรูปที่ 13 แสดงผลการทดลองที่ 3 ด้วยการให้แอพพลิเคชั่น Serial Bluetooth Terminal สามารถควบคุมการติดหรือดับแอลอีดีที่อยู่บนบอร์ด NodeMCU ESP32 ได้เช่นเดิม ในรูปที่ 9 จะเห็นข้อความที่แสดงในกรอบสีแดงและสีเหลือง จากนั้นบอร์ด NodeMCU ESP32 ส่งข้อมูลที่สุ่มตัวเลขกลับมา ดังแสดงในกรอบสีฟ้าของแอพพลิเคชั่น Serial Bluetooth Terminal และสำหรับบทความนี้คงพอจะเป็นแนวทางให้ผู้อ่านเข้าใจการสื่อสารและนำไปประยุกต์ใช้งานระหว่างแอพพลิเคชั่น Serial Bluetooth Terminal บนโทรศัพท์มือถือและโมดูลบูลทูธในบอร์ด NodeMCU ESP32 เพิ่มขึ้นครับ.
Reference
- https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/
- https://circuits4you.com/2018/12/31/esp32-wroom32-devkit-analog-read-example/
- https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/
- https://www.electronicwings.com/esp32/esp32-bluetooth-getting-started
- https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/DiscoverConnect/DiscoverConnect.ino
- https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial