อุณหภูมิและความชื้นผ่าน Bluetooth ด้วย Arduino Uno + HC-05 (DHT11)
ดูอุณหภูมิและความชื้นในห้องแบบสดบนโทรศัพท์ Arduino Uno อ่าน DHT11 ทุก 2 วินาที และ HC-05 ส่งค่าผ่าน Bluetooth ไม่ต้องใช้อินเทอร์เน็ต ใช้ได้เฉพาะ Android: iPhone เชื่อมต่อกับ HC-05 ไม่ได้ ถ้าใช้ iPhone ให้ใช้ HM-10 แทน จับคู่ HC-05 ในการตั้งค่า Bluetooth ก่อน แล้วไปที่ คลังของฉัน › อุปกรณ์ › เพิ่มอุปกรณ์ › Bluetooth เลือก Arduino Uno + HC-05/06 และอุณหภูมิและความชื้น แล้วอัปโหลดโค้ดจาก รับโค้ด

วิดีโอ
แผนผังวงจร

ซอร์สโค้ด
1// ┌────────────────────────────────────┐
2// │ BlueGrays · bluegrays.com │
3// │ Bluetooth · Arduino Uno + HC-05/06 │
4// │ BG-Hava │
5// └────────────────────────────────────┘
6// อุณหภูมิและความชื้น
7// บอร์ดใน Arduino IDE: Arduino Uno
8// HC-05/06 เป็น Bluetooth แบบคลาสสิก ใช้ได้เฉพาะ Android iPhone เชื่อมต่อไม่ได้ เปลี่ยนชื่อได้ในโหมด AT (ไม่บังคับ)
9
10#include <SoftwareSerial.h>
11#include <DHT.h> // Library Manager: 'DHT sensor library' (Adafruit)
12
13const char BLE_NAME[] = "BG-Hava"; // บอร์ดจะประกาศชื่อนี้ และแอปจะค้นหาบอร์ดในรายการด้วยชื่อนี้
14
15// ขา: เปลี่ยนตรงนี้ถ้าต่อสายต่างออกไป
16const int DHT_PIN = 4;
17const unsigned long READ_EVERY_MS = 2000; // ส่งค่าที่วัดใหม่ทุก 2 วินาที
18
19void btWrite(const char *text);
20void onValue(const char *key, const char *value);
21void sendAll();
22void onLinkLost();
23
24// ── แอปส่งบรรทัดแบบ "คีย์=ค่า" เมื่อได้รับ "?" บอร์ดจะส่งค่าทั้งหมดกลับมา ──
25char line[65];
26uint8_t lineLen = 0;
27unsigned long lastRxMs = 0;
28
29bool is(const char *a, const char *b) { return strcmp(a, b) == 0; }
30bool isOn(const char *v) { return v[0] == '1'; }
31
32void sendText(const char *key, const char *value) {
33 char out[72];
34 snprintf(out, sizeof(out), "%s=%s\n", key, value);
35 btWrite(out);
36}
37
38void sendInt(const char *key, long value) {
39 char text[12];
40 snprintf(text, sizeof(text), "%ld", value);
41 sendText(key, text);
42}
43
44void sendFloat(const char *key, float value) {
45 char text[16];
46 dtostrf(value, 1, 1, text);
47 sendText(key, text);
48}
49
50void handleLine(char *text) {
51 char *eq = strchr(text, '=');
52 if (eq == nullptr) {
53 if (text[strlen(text) - 1] == '?') {
54 btWrite("@v=1.0\n"); // บรรทัด "@v" บอกเวอร์ชันโค้ดให้แอปรู้
55 sendAll();
56 }
57 return;
58 }
59 *eq = '\0';
60 onValue(text, eq + 1);
61}
62
63void feed(char c) {
64 lastRxMs = millis();
65 if (c == '\n' || c == '\r') {
66 if (lineLen == 0) return;
67 line[lineLen] = '\0';
68 lineLen = 0;
69 handleLine(line);
70 } else if (lineLen < sizeof(line) - 1) {
71 line[lineLen++] = c;
72 }
73}
74
75// ── Bluetooth (SoftwareSerial) ──
76SoftwareSerial bt(2, 3); // โมดูล TXD → D2, RXD → D3 (ผ่านวงจรแบ่งแรงดัน 1k/2k), 9600 baud
77
78void btWrite(const char *text) { bt.print(text); }
79
80void btBegin() { bt.begin(9600); }
81
82void btPoll() {
83 while (bt.available()) feed(bt.read());
84}
85
86// ── Arduino Uno + HC-05/06 ──
87DHT dht(DHT_PIN, DHT11);
88float temperature = NAN, humidity = NAN;
89unsigned long lastReadMs = 0;
90
91// ถ้าอ่านเซ็นเซอร์ไม่ได้ แอปจะได้รับ "@err=dht" ให้ตรวจสอบการต่อสาย
92void readSensor() {
93 float t = dht.readTemperature();
94 float h = dht.readHumidity();
95 if (isnan(t) || isnan(h)) {
96 btWrite("@err=dht\n");
97 return;
98 }
99 temperature = t;
100 humidity = h;
101}
102
103void sendReadings() {
104 if (isnan(temperature)) return;
105 sendFloat("sicaklik", temperature);
106 sendInt("nem", lround(humidity));
107}
108
109void onValue(const char *key, const char *value) {
110}
111
112void sendAll() {
113 if (isnan(temperature)) readSensor();
114 sendReadings();
115}
116
117void onLinkLost() {
118}
119
120void setup() {
121 Serial.begin(115200);
122 dht.begin();
123 btBegin();
124 Serial.print(F("BlueGrays Bluetooth: "));
125 Serial.println(BLE_NAME);
126}
127
128void loop() {
129 btPoll();
130 if (millis() - lastReadMs >= READ_EVERY_MS) {
131 lastReadMs = millis();
132 readSensor();
133 sendReadings();
134 }
135}
136FAQ
ใช้กับ iPhone ได้ไหม
ไม่ได้ โปรเจกต์นี้ใช้ได้เฉพาะ Android เพราะ HC-05 ใช้ Bluetooth แบบคลาสสิก และ iPhone เชื่อมต่อกับโมดูล Bluetooth แบบคลาสสิกไม่ได้ ถ้าใช้ iPhone ให้ใช้ HM-10 แทน โดยเลือกบอร์ด Arduino Uno + HM-10 ในแอป คู่มืออุณหภูมิและความชื้นผ่าน Bluetooth ด้วย ESP32 ก็ใช้กับ iPhone ได้เช่นกัน บน Android ให้จับคู่ HC-05 ในการตั้งค่า Bluetooth ของโทรศัพท์ก่อน (PIN มักเป็น 1234 หรือ 0000) แล้วค่อยเชื่อมต่อจากแอป
ใช้ DHT22 แทน DHT11 ได้ไหม
ได้ DHT22 (AM2302) วัดได้กว้างกว่าและแม่นกว่า ประมาณ -40 ถึง 80 °C, ±0.5 °C การต่อสายยังเหมือนเดิม เทมเพลตในแอปทำมาสำหรับ DHT11 จึงต้องเปลี่ยน DHT11 เป็น DHT22 ในบรรทัด DHT dht(DHT_PIN, DHT11) ของโค้ดแล้วอัปโหลดใหม่ สเกลของกราฟบนแผงควบคุมตั้งไว้ตามช่วงของ DHT11 (0 ถึง 50 °C)
ทำไมค่าเปลี่ยนทุก 2 วินาที และแม่นแค่ไหน
DHT11 อ่านถี่กว่าประมาณวินาทีละครั้งไม่ได้ โค้ดจึงวัดและส่งทุก 2 วินาทีด้วย READ_EVERY_MS = 2000 อุณหภูมิส่งเป็นทศนิยมหนึ่งตำแหน่ง (°C) ส่วนความชื้นส่งเป็นจำนวนเต็ม (%) เซ็นเซอร์วัดได้ 0 ถึง 50 °C และความชื้น 20% ถึง 90% ความคลาดเคลื่อนของอุณหภูมิประมาณ ±2 °C เพียงพอสำหรับดูสภาพห้อง ถ้าต้องการความแม่นยำมากขึ้นให้เปลี่ยนเป็น DHT22
ถ้าการเชื่อมต่อหลุดจะเกิดอะไรขึ้น ค่าที่วัดได้ถูกบันทึกไว้ไหม
บอร์ดส่งเฉพาะค่าปัจจุบันและไม่เก็บประวัติ HC-05 ไม่แจ้ง Uno เมื่อการเชื่อมต่อหลุด บอร์ดยังวัดต่อไป แต่ค่าจะไปไม่ถึงโทรศัพท์ กราฟวาดจากค่าที่เข้ามาขณะเปิดแผงควบคุมและบอร์ดเชื่อมต่ออยู่ ระยะประมาณ 10 เมตรในที่โล่ง เมื่อเชื่อมต่อใหม่ แอปจะส่ง ? และบอร์ดจะส่งค่าล่าสุดมาทันที
