วันเสาร์ที่ 18 สิงหาคม พ.ศ. 2561

Bluetooth Module HC-05/HC-06ที่ใช้ในการควนคุมรถ

วิธีใช้งาน Bluetooth Module HC-05/HC-06 เชื่อมต่อกับ Arduino DHT11 ส่งค่าอุณหภูมิและความชื้นเข้ามือถือ แบบ Real Time



เราสามารถนำข้อมูลจาก Arduino ไปแสดงผลที่หน้าจอโทรศัพท์มือถือ หรือใช้โทรศัพท์มือถือ ควบคุมการทำงานของ Arduino ได้ง่าย ๆ ด้วยการเขียนโปรแกรมไม่กี่บรรทัด เพราะเรามี App สำเร็จรูป ที่เขียนมาเพื่อการติดต่อ/ควบคุม Arduino ผ่าน Bluetooth นี้โดยเฉพาะ ชื่อว่า App Virtuino


ตัวอย่างนี้จะเป็นการนำค่าตัวแปร ค่าอุณหภูมิและความชื่นจากเซนเซอร์ DHT11 ไปแสดง เพื่อเป็นแนวทางในการใช้งาน Arduino+Bluetooth กับ App Virtuino 

อุปกรณ์สำหรับทดลอง Bluetooth ส่งค่าจาก DHT11 เข้ามือถือ
ดาวน์โหลดและติดตั้งไลบารี 2 ตัวนี้

วิธีการต่อใช้งาน Arduino Bluetooth HC05 / HC06 กับ DHT11

Bluetooth Module HC06  -> Arduino
  • VCC -> 5V
  • GND -> GND
  • Tx -> 2
  • Rx ->3
DHT11 เซ็นเซอร์อุณหภูมิและความชื้น -> Arduino
  • VCC -> 5v
  • GND -> GND
  • OUT/DATA -> 6

ตัวอย่างการต่อวงจรดังรูปนี้ 
ตำแหน่งขาเซนเซอร์ DHT11 อาจมีตำแหน่งเปลี่ยนแปลงไปจากรูปนี้ ให้อ้างอิงตามที่เขียนไว้ในโมดูล

ก็อปปีโคดตัวอย่างนี้ แล้วอัพโหลดลงบอร์ด Arduino 

/* Virtuino example (Bluetooth)
Example name = "Read temperatures from two ds18b20 sensors "
Created by Ilias Lamprou
Updated 24 02 2017
This is the code you need to run on your arduino board to cummunicate whith VirtUino app via bluetooth
Before running this code config the settings below as the instructions on the right
Download latest Virtuino android app from the link: https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino
Contact address for questions or comments: iliaslampr@gmail.com
*/
/*========= VirtuinoBluetooth Class methods
vPinMode(int pin, int state) set pin as digital OUTPUT or digital INPUT. (Insteed default pinMode method
========= Virtuino General methods
void vDigitalMemoryWrite(int digitalMemoryIndex, int value) write a value to a Virtuino digital memory (digitalMemoryIndex=0..31, value range = 0 or 1)
int vDigitalMemoryRead(int digitalMemoryIndex) read the value of a Virtuino digital memory (digitalMemoryIndex=0..31, returned value range = 0 or 1)
void vMemoryWrite(int analogMemoryIndex, float value) write a value to Virtuino float memory (memoryIndex=0..31, value range as float value)
float vMemoryRead(int analogMemoryIndex) read the value of Virtuino analog memory (analogMemoryIndex=0..31, returned value range = 0..1023)
run() neccesary command to communicate with Virtuino android app (on start of void loop)
void vDelay(long milliseconds); Pauses the program (without block communication) for the amount of time (in miliseconds) specified as parameter
int getPinValue(int pin) read the value of a Pin. Usefull for PWM pins
*/
#include <DHT.h> // You have to download DHT11 library
//Attention: For new DHT11 version library you will need the Adafruit_Sensor library
//Download from here: https://github.com/adafruit/Adafruit_Sensor
#define DHTPIN 6
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include "VirtuinoBluetooth.h" // virtuino library
// Code to use SoftwareSerial
#include <SoftwareSerial.h> // Disable this line if you want to use hardware serial
SoftwareSerial bluetoothSerial = SoftwareSerial(2, 3); // arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to bluetooth module TX pin - connect the arduino TX pin to bluetooth module RX pin. Disable this line if you want to use hardware serial
VirtuinoBluetooth virtuino(bluetoothSerial); // Set SoftwareSerial baud rate. - Disable this line if you want to use hardware serial
// Code to use HardwareSerial
// VirtuinoBluetooth virtuino(Serial1); // enable this line and disable all SoftwareSerial lines
// Open VirtuinoBluetooth.h file on the virtuino library folder -> disable the line: #define BLUETOOTH_USE_SOFTWARE_SERIAL
// Connect HC-05 module to Arduino (MEGA or DUE) Serial1. (pins: 18,19)
//======================================================================== setup
//========================================================================
void setup(void) {
Serial.begin(9600); // start monitor serial port
bluetoothSerial.begin(9600); // Enable this line if you want to use software serial (UNO, Nano etc.)
//Serial1.begin(9600); // Enable this line if you want to use hardware serial (Mega, DUE etc.)
virtuino.DEBUG = true; // set this value TRUE to enable the serial monitor status
// Start up the library Dallas Temperature IC Control Library
dht.begin();
Serial.println("Setup completed.");
}
//======================================================================== setup
void readTemperatures() {
Serial.println("Read sensors' values...");
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT");
}
else {
Serial.println("Temp=" + String(temperature) + " *C");
Serial.println("Humidity=" + String(humidity) + " %");
virtuino.vMemoryWrite(0, temperature); // write temperature 1 to virtual pin V0. On Virtuino panel add a value display or an analog instrument to pin V0
virtuino.vMemoryWrite(1, humidity); // write temperature 1 to virtual pin V1. On Virtuino panel add a value display or an analog instrument to pin V1
}
}
//======================================================================== setup
//========================================================================
void loop(void) {
virtuino.run(); // necessary command to communicate with Virtuino android app
//------ enter your loop code below here
//------ avoid to use delay() function in your code. Use the command virtuino.vDelay() instead of delay()
//------ your code .....
readTemperatures(); // Dont' read the sensors values every cicle
virtuino.vDelay(5000); // Add a delay at least 1 second long.
}
view rawarduino_hc05.ino hosted with ❤ by GitHub


วิธีส่งค่าตัวแปรต่าง ๆ เช่น ค่าอุณหภูมิและความชื้นเข้าไปแสดงที่ เกจ V0 และ V1 ใน App

จากโคดตัวอย่าง มองหาบรรทัดนี้

virtuino.vMemoryWrite(0,temperature); 
ตัวเลข 0 แทนเกจ v0 ใน app
virtuino.vMemoryWrite(1,humidity); 
ตัวเลข 1 แทนเกจ v1 ใน app

ในมือถือ โหลด App virtuino ซึ่งใช้สำหรับควบคุมติดต่อ Bluetooth กับ Arduino ผ่านโทรศัพท์มือถือ ดาวน์โหลดได้ที่นี่


เมื่อโหลดโปรแกรมเสร็จแล้ว เปิดแอปขึ้นมาสร้างโปรเจคใหม่ แล้วกดรูปมือถือทางขวาบน เพื่อทำการเชื่อมต่อ Bluetooth Module HC06 กดบวก แล้ว เลือกอุปกรณ์ที่จะทำการเชื่อมต่อ 

ถ้าไม่พบรายชื่อ bluetooth ของเรา ให้ ไปที่ตั้งค่า bluetooth ของโทรศัพท์ก่อน กด connect และ pair ให้เรียบร้อย แล้วกลับมาเข้าโปรแกรมส่วนนี้ใหม่

ในโปรแกรมตั้งให้ไปเป็น Enabled แล้ว กด Conncet  
ถ้าเชื่อมต่อสำเร็จไฟสถานะที่  Bluetooth Module HC06 จะหยุดกระพริบ


สร้างส่วนแสดงผล ในที่นี่ ใช้เป็นเกจ สร้างมา 2 ชุด เพื่อแสดงค่าอุณหภูมิและความชื้น
เลือกที่เครื่องหมายบวก แล้วเลือก Analog instrument
ตั้งค่า Server ให้ตรงกับ Bluetooth HC05/HC06 และ Pin เป็น V0 ตัวอย่างดังรูปนี้



ลองเพิ่มส่วนแสดงผลแบบข้อความอีก ทำคล้าย ๆ กัน เลือก Pin เป็น V1 ก็จะได้หน้าตาเกจแสดงออกมาดังนี้ 
เพิ่ม label และ value ลากวางตำแหน่ง ตามใจชอบ



เมื่อตกแต่งหน้าจอแล้ว กดปุ่ม เครื่องหมายถูกรูปกุญแจ เพื่อแสดงผลการทำงาน



เพียงเท่านี้เราก็สามารถแสดงข้อมูลออกหน้าจอโทรศัพท์มือถือแบบง่าย ๆ ได้แล้ว


ใน App นี้เรายังสามารถ อ่านค่า/ส่งค่า ติดต่อกับ บอร์ด Arduino ได้ทั้งแบบ Digital และ Analog ควบคุมเปิด/ปิด อุปกรณ์ Arduino ได้ผ่านทาง Bluetooth 
App นี้ยังรองรับอุปกรณ์อื่นเช่น ESP8266 / ESP32 บอร์ด Arduino รุ่นต่าง ๆ อีกมากมาย สามารถดูข้อมูลเพิ่มเติมได้ในหน้าของ App Virtuino

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

หลักของมาตรฐาน ISO 9001:2000      หลักการมาตรฐานของ ISO 9001:2000 จะเป็นการระบุถึงข้อกำหนดที่ใช้ในระบบบริหารที่เกี่ยวกับคุณภาพ โดยเป้าหมา...