ES-111เริ่มต้น

กลอนเซอร์โวผ่าน Bluetooth ด้วย ESP32

ควบคุม เซอร์โว SG90 เป็นกลอนเล็กๆ จากโทรศัพท์ผ่าน Bluetooth สั่งล็อก ปลดล็อก หรือปรับมุมได้โดยไม่ใช้อินเทอร์เน็ตหรือ Wi-Fi คู่มือนี้สอนใช้เทมเพลต Bluetooth กลอนเซอร์โว ด้วย ESP32 เซอร์โว 1 ตัวและสายไฟ 3 เส้น ไปที่ คลังของฉัน › อุปกรณ์ › เพิ่มอุปกรณ์ › Bluetooth เลือกกลอนเซอร์โวแล้วกด รับโค้ด เพื่ออัปโหลดลงบอร์ด หรือกด ตั้งค่าเป็นแผงควบคุม Bluetooth ในหน้านี้เพื่อเปิดตัวช่วยตั้งค่าพร้อมเทมเพลต

0.0(0)
กลอนเซอร์โวผ่าน Bluetooth ด้วย ESP32
แตะสวิตช์ล็อกบนโทรศัพท์ เซอร์โวจะหมุนไปที่ 90 องศาเพื่อลงกลอน แตะอีกครั้งก็จะหมุนกลับไปที่ 0 องศาเพื่อปลดกลอน ไม่ต้องใช้อินเทอร์เน็ตหรือเราเตอร์ เพราะโทรศัพท์เชื่อมต่อกับ ESP32 โดยตรงผ่าน Bluetooth แถบเลื่อนใช้ปรับมุมได้ตั้งแต่ 0 ถึง 180 องศา บอร์ดเริ่มทำงานในสถานะล็อก และหากการเชื่อมต่อขาดขณะปลดล็อกอยู่ ก็จะล็อกกลับเอง SG90 เป็นเซอร์โวขนาดเล็กสำหรับงานอดิเรก โปรเจกต์นี้ใช้เรียนรู้การทำกลอนกล่อง ลิ้นชัก หรือประตูจำลอง ไม่ใช้แทนกลอนประตูจริงหรือกุญแจล็อกของมีค่า ในแอปให้เลือก Bluetooth และเทมเพลตกลอนเซอร์โว กดรับโค้ดเพื่ออัปโหลด แล้วค้นหาบอร์ดของคุณในรายการ สิ่งที่จะได้เรียนรู้: มุมของเซอร์โว การส่งข้อความสั้นๆ ทีละบรรทัดผ่าน Bluetooth Low Energy และการให้บอร์ดรายงานสถานะไปยังแผงควบคุม

วิดีโอ

แผนผังวงจร

กลอนเซอร์โวผ่าน Bluetooth ด้วย ESP32 - แผนผังวงจร

ซอร์สโค้ด

C++ (Arduino IDE)
1// ┌───────────────────────────┐
2// │ BlueGrays · bluegrays.com │
3// │ Bluetooth · ESP32         │
4// │ BG-Kilit                  │
5// └───────────────────────────┘
6// Servo lock
7// Arduino IDE board: ESP32 Dev Module
8// the BLE library comes with the ESP32 board package, nothing extra to install
9
10#include <BLEDevice.h>
11#include <BLEServer.h>
12#include <BLE2902.h>
13#include <ESP32Servo.h>  // Library Manager: 'ESP32Servo'
14
15const char BLE_NAME[] = "BG-Kilit";  // the board advertises with this name, the app finds it in the list by it
16
17// Pins: change them here if you wired differently
18const int SERVO_PIN = 13;
19const int LOCKED_ANGLE = 90, OPEN_ANGLE = 0;  // servo angles for locked and open
20const bool LOCK_ON_DISCONNECT = true;  // If the connection drops the lock closes (set false to leave it as it is)
21
22void btWrite(const char *text);
23void onValue(const char *key, const char *value);
24void sendAll();
25void onLinkLost();
26
27// ── The app sends "key=value" lines; on "?" the board sends all its values ──
28char line[65];
29uint8_t lineLen = 0;
30unsigned long lastRxMs = 0;
31
32bool is(const char *a, const char *b) { return strcmp(a, b) == 0; }
33bool isOn(const char *v) { return v[0] == '1'; }
34
35void sendText(const char *key, const char *value) {
36  char out[72];
37  snprintf(out, sizeof(out), "%s=%s\n", key, value);
38  btWrite(out);
39}
40
41void sendInt(const char *key, long value) {
42  char text[12];
43  snprintf(text, sizeof(text), "%ld", value);
44  sendText(key, text);
45}
46
47void sendFloat(const char *key, float value) {
48  char text[16];
49  dtostrf(value, 1, 1, text);
50  sendText(key, text);
51}
52
53void handleLine(char *text) {
54  char *eq = strchr(text, '=');
55  if (eq == nullptr) {
56    if (text[strlen(text) - 1] == '?') {
57      btWrite("@v=1.0\n");  // the "@v" line tells the app the code version
58      sendAll();
59    }
60    return;
61  }
62  *eq = '\0';
63  onValue(text, eq + 1);
64}
65
66void feed(char c) {
67  lastRxMs = millis();
68  if (c == '\n' || c == '\r') {
69    if (lineLen == 0) return;
70    line[lineLen] = '\0';
71    lineLen = 0;
72    handleLine(line);
73  } else if (lineLen < sizeof(line) - 1) {
74    line[lineLen++] = c;
75  }
76}
77
78// ── Bluetooth (Nordic UART) ──
79#define UART_SERVICE "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
80#define UART_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
81#define UART_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
82
83BLECharacteristic *txChar = nullptr;
84volatile bool linked = false;
85volatile bool linkLost = false;
86// incoming bytes move from the Bluetooth task to loop() through a small ring buffer
87const int RX_SIZE = 256;
88volatile uint8_t rxBuf[RX_SIZE];
89volatile int rxHead = 0, rxTail = 0;
90
91class ServerEvents : public BLEServerCallbacks {
92  void onConnect(BLEServer *server) override { linked = true; }
93  void onDisconnect(BLEServer *server) override { linked = false; linkLost = true; }
94};
95
96class RxEvents : public BLECharacteristicCallbacks {
97  void onWrite(BLECharacteristic *c) override {
98    auto v = c->getValue();
99    for (size_t i = 0; i < v.length(); i++) {
100      int next = (rxHead + 1) % RX_SIZE;
101      if (next == rxTail) break;
102      rxBuf[rxHead] = (uint8_t)v[i];
103      rxHead = next;
104    }
105  }
106};
107
108// lines go out in 20-byte packets (works with every phone)
109void btWrite(const char *text) {
110  if (!linked || txChar == nullptr) return;
111  size_t n = strlen(text);
112  for (size_t i = 0; i < n; i += 20) {
113    size_t len = n - i < 20 ? n - i : 20;
114    txChar->setValue((uint8_t *)text + i, len);
115    txChar->notify();
116    delay(4);
117  }
118}
119
120void btBegin() {
121  BLEDevice::init(BLE_NAME);
122  BLEServer *server = BLEDevice::createServer();
123  server->setCallbacks(new ServerEvents());
124  BLEService *uart = server->createService(UART_SERVICE);
125  txChar = uart->createCharacteristic(UART_TX, BLECharacteristic::PROPERTY_NOTIFY);
126  txChar->addDescriptor(new BLE2902());
127  BLECharacteristic *rx = uart->createCharacteristic(UART_RX, BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
128  rx->setCallbacks(new RxEvents());
129  uart->start();
130  BLEAdvertisementData adv;
131  adv.setFlags(0x06);
132  adv.setCompleteServices(BLEUUID(UART_SERVICE));
133  BLEAdvertisementData scan;
134  scan.setName(BLE_NAME);
135  BLEAdvertising *advertising = BLEDevice::getAdvertising();
136  advertising->setAdvertisementData(adv);
137  advertising->setScanResponseData(scan);
138  BLEDevice::startAdvertising();
139}
140
141void btPoll() {
142  while (rxTail != rxHead) {
143    char c = (char)rxBuf[rxTail];
144    rxTail = (rxTail + 1) % RX_SIZE;
145    feed(c);
146  }
147  if (linkLost) {
148    linkLost = false;
149    onLinkLost();
150    delay(100);
151    BLEDevice::startAdvertising();
152  }
153}
154
155// ── ESP32 ──
156Servo servo;
157bool locked = true;
158int angle = LOCKED_ANGLE;
159
160void moveTo(int a) {
161  angle = constrain(a, 0, 180);
162  servo.write(angle);
163}
164
165void onValue(const char *key, const char *value) {
166  if (is(key, "kilit")) {
167    locked = isOn(value);
168    moveTo(locked ? LOCKED_ANGLE : OPEN_ANGLE);
169    sendInt("aci", angle);
170  } else if (is(key, "aci")) {
171    moveTo(atoi(value));
172    if (locked != (angle == LOCKED_ANGLE)) {
173      locked = !locked;
174      sendInt("kilit", locked);
175    }
176  }
177}
178
179void sendAll() {
180  sendInt("kilit", locked);
181  sendInt("aci", angle);
182}
183
184void onLinkLost() {
185  if (LOCK_ON_DISCONNECT && !locked) {
186    locked = true;
187    moveTo(LOCKED_ANGLE);
188  }
189}
190
191void setup() {
192  Serial.begin(115200);
193  servo.setPeriodHertz(50);
194  servo.attach(SERVO_PIN, 500, 2500);
195  moveTo(LOCKED_ANGLE);
196  btBegin();
197  Serial.print(F("BlueGrays Bluetooth: "));
198  Serial.println(BLE_NAME);
199}
200
201void loop() {
202  btPoll();
203}
204

FAQ

ใช้กับ iPhone ได้ไหม

ใช้ได้ Bluetooth Low Energy (BLE) ในตัว ESP32 สื่อสารได้ทั้งกับ iPhone และ Android ส่วน iPhone เชื่อมต่อกับโมดูล Bluetooth แบบคลาสสิกอย่าง HC-05 และ HC-06 ไม่ได้ ซึ่งโปรเจกต์นี้ไม่จำเป็นต้องใช้ ไม่ต้องจับคู่บอร์ดจากการตั้งค่า Bluetooth ของโทรศัพท์ บอร์ด BLE อาจไม่ปรากฏในรายการนั้น ซึ่งเป็นเรื่องปกติ ให้เชื่อมต่อผ่านแอป BlueGrays

ทำไมกลอนถึงล็อกเองเมื่อการเชื่อมต่อขาด

เป็นการทำงานที่ตั้งใจไว้ เพราะค่า LOCK_ON_DISCONNECT ในโค้ดตั้งเป็น true เมื่อโทรศัพท์ออกนอกระยะหรือปิดแอป บอร์ดจะหมุนกลอนกลับไปยังมุมล็อก (90 องศา) เพื่อไม่ให้เผลอปล่อยกลอนค้างไว้ในสถานะปลดล็อก บอร์ดจะเริ่มทำงานในสถานะล็อกด้วย หากต้องการให้ปลดล็อกค้างไว้ ให้เปลี่ยนค่านี้เป็น false แล้วอัปโหลดโค้ดใหม่ ระบบป้องกันนี้ยังทำงานแม้คุณจะปลดกลอนด้วยแถบเลื่อนมุมเซอร์โวแทนสวิตช์ล็อก เพราะบอร์ดถือว่ามุมใดก็ตามที่ไม่ใช่ 90 องศาคือสถานะปลดล็อก

ใช้ล็อกประตูจริงหรือตู้เซฟได้ไหม

ไม่ได้ โปรเจกต์นี้มีไว้เพื่อเรียนรู้ SG90 เป็นเซอร์โวขนาดเล็กสำหรับงานอดิเรกที่ใช้เฟืองพลาสติก แขนเซอร์โวจะหมุนได้หากถูกฝืน และเมื่อไฟดับก็จะไม่มีแรงยึดตำแหน่ง นอกจากนี้การเชื่อมต่อ Bluetooth ในโค้ดนี้ไม่มีรหัสผ่าน โทรศัพท์เครื่องอื่นที่อยู่ใกล้ก็เชื่อมต่อกับบอร์ดและส่งคำสั่งได้ ลองใช้กับกลอนกล่อง ลิ้นชัก หรือประตูจำลอง อย่าใช้ล็อกประตูจริงหรือปกป้องของมีค่า

สวิตช์ล็อกกับแถบเลื่อนมุมต่างกันอย่างไร

สวิตช์ล็อกจะหมุนกลอนไปยังตำแหน่ง 2 จุดที่กำหนดในโค้ด คือล็อกที่ 90 องศาและปลดล็อกที่ 0 องศา ทุกครั้งที่เปลี่ยนตำแหน่ง บอร์ดจะรายงานมุมใหม่ไปยังแผงควบคุม แถบเลื่อนจึงขยับตามค่านั้น ส่วนแถบเลื่อนมุมเซอร์โวใช้หมุนแขนไปยังมุมใดก็ได้ตั้งแต่ 0 ถึง 180 องศา เหมาะสำหรับหามุมที่กลอนของคุณปิดได้พอดี นำค่าที่ได้ไปใส่ในบรรทัด LOCKED_ANGLE และ OPEN_ANGLE ของโค้ด เมื่อเลื่อนเป็นมุมอื่นที่ไม่ใช่ 90 องศา บอร์ดจะถือว่าปลดล็อกอยู่ และสวิตช์ล็อกบนแผงควบคุมจะแสดงสถานะตามนั้น เมื่อกลับมาที่ 90 องศาพอดีจะแสดงว่าล็อกอีกครั้ง