-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathESP32_1_WebServer_LeftRight_Display.cpp
More file actions
429 lines (388 loc) · 11.7 KB
/
ESP32_1_WebServer_LeftRight_Display.cpp
File metadata and controls
429 lines (388 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoJson.h>
// 感測器通信參數
#define COM 0x55
#define BAUD_RATE 115200
#define TIMEOUT_MS 500 // 增加超時時間至500ms以確保接收數據
#define MAX_DISTANCE_CM 600 // 最大距離:600 cm
#define OLED_REFRESH_MS 1000 // OLED刷新間隔:1秒
// 第一個感測器(Left,使用UART2)
#define RXD2 16 // GPIO16 (RX2)
#define TXD2 17 // GPIO17 (TX2)
HardwareSerial SerialSensorLeft(2); // UART2
// 第二個感測器(Right,使用UART1)
#define RXD1 9 // GPIO9 (RX1)
#define TXD1 10 // GPIO10 (TX1)
HardwareSerial SerialSensorRight(1); // UART1
// OLED顯示屏參數
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_ADDRESS 0x3C
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Wi-Fi設置(Access Point模式)
const char* ssid = "ESP32_Webserver";
const char* password = "12345678";
IPAddress local_IP(192, 168, 40, 1);
IPAddress gateway(192, 168, 40, 1);
IPAddress subnet(255, 255, 255, 0);
// WebServer設置
WebServer server(80);
// 感測器數據
int currentDistanceLeft = -1;
int currentDistanceRight = -1;
int currentDistanceUnder = -1;
int currentDistanceFront = -1;
int currentDistanceBack = -1;
// 感測器數據緩衝區
unsigned char buffer_RTTLeft[4] = {0};
unsigned char buffer_RTTRight[4] = {0};
// OLED刷新計時
unsigned long lastRefreshTime = 0;
// 函數宣告
bool readSensorData(HardwareSerial &sensor, unsigned char *buffer);
bool validateChecksum(unsigned char *buffer);
int extractDistance(unsigned char *buffer);
void updateDisplay();
void clearSerialBuffer(HardwareSerial &sensor);
void debugRawData(HardwareSerial &sensor, const char *sensorName);
// HTML網頁內容(優化後的設計)
const char* index_html = R"rawliteral(
<!DOCTYPE HTML>
<html>
<head>
<title>Underwater Distance Sensors</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: 'Roboto', sans-serif;
background: linear-gradient(135deg, #1e1e2f, #2e2e4f);
color: #ffffff;
text-align: center;
margin: 0;
padding: 20px;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
}
h2 {
font-size: 2.5em;
margin-bottom: 20px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}
.sensor-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
}
.sensor {
background: rgba(255, 255, 255, 0.1);
border-radius: 15px;
padding: 20px;
width: 200px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.sensor:hover {
transform: translateY(-5px);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}
.sensor h3 {
font-size: 1.2em;
margin-bottom: 10px;
color: #a1c4fd;
}
.distance {
font-size: 1.8em;
color: #c3e88d;
opacity: 0;
animation: fadeIn 0.5s forwards;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
</head>
<body>
<h2>Underwater Distance Sensors</h2>
<div class="sensor-container">
<div class="sensor">
<h3>Left Sensor</h3>
<p class="distance" id="left">N/A</p>
</div>
<div class="sensor">
<h3>Right Sensor</h3>
<p class="distance" id="right">N/A</p>
</div>
<div class="sensor">
<h3>Under Sensor</h3>
<p class="distance" id="under">N/A</p>
</div>
<div class="sensor">
<h3>Front Sensor</h3>
<p class="distance" id="front">N/A</p>
</div>
<div class="sensor">
<h3>Back Sensor</h3>
<p class="distance" id="back">N/A</p>
</div>
</div>
<script>
function updateSensorData() {
fetch('/data')
.then(response => response.json())
.then(data => {
document.getElementById('left').innerText = data.left !== -1 ? data.left + ' cm' : 'N/A';
document.getElementById('right').innerText = data.right !== -1 ? data.right + ' cm' : 'N/A';
document.getElementById('under').innerText = data.under !== -1 ? data.under + ' cm' : 'N/A';
document.getElementById('front').innerText = data.front !== -1 ? data.front + ' cm' : 'N/A';
document.getElementById('back').innerText = data.back !== -1 ? data.back + ' cm' : 'N/A';
})
.catch(error => console.error('Error:', error));
}
setInterval(updateSensorData, 1000); // 每1秒更新數據
updateSensorData(); // 初次加載時更新
</script>
</body>
</html>
)rawliteral";
void handleRoot() {
server.send(200, "text/html", index_html);
}
void handleData() {
StaticJsonDocument<200> doc;
doc["left"] = currentDistanceLeft;
doc["right"] = currentDistanceRight;
doc["under"] = currentDistanceUnder;
doc["front"] = currentDistanceFront;
doc["back"] = currentDistanceBack;
String json;
serializeJson(doc, json);
server.send(200, "application/json", json);
}
void handleUpdate() {
if (server.hasArg("plain")) {
String body = server.arg("plain");
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, body);
if (!error) {
if (doc.containsKey("under")) {
currentDistanceUnder = doc["under"];
}
if (doc.containsKey("front")) {
currentDistanceFront = doc["front"];
}
if (doc.containsKey("back")) {
currentDistanceBack = doc["back"];
}
server.send(200, "text/plain", "Data updated");
} else {
server.send(400, "text/plain", "Invalid JSON");
}
} else {
server.send(400, "text/plain", "No data received");
}
}
void setup() {
Serial.begin(BAUD_RATE);
SerialSensorLeft.begin(BAUD_RATE, SERIAL_8N1, RXD2, TXD2);
SerialSensorRight.begin(BAUD_RATE, SERIAL_8N1, RXD1, TXD1);
// 初始化OLED顯示屏
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println("SSD1306 initialization failed");
for (;;);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(F("Initializing..."));
display.display();
// 設置Wi-Fi為Access Point模式
Serial.println("Setting AP (Access Point)...");
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(ssid, password, 1); // 設置通道為1,減少干擾
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
// 設置WebServer路由
server.on("/", HTTP_GET, handleRoot);
server.on("/data", HTTP_GET, handleData);
server.on("/update", HTTP_POST, handleUpdate);
// 啟動WebServer
server.begin();
Serial.println("WebServer started");
delay(1000); // 確保感測器啟動完成(啟動時間≤500ms)
}
void loop() {
server.handleClient(); // 處理WebServer請求
// 處理Left感測器
SerialSensorLeft.write(COM);
if (readSensorData(SerialSensorLeft, buffer_RTTLeft)) {
if (validateChecksum(buffer_RTTLeft)) {
int newDistance = extractDistance(buffer_RTTLeft);
if (newDistance >= 0 && newDistance <= MAX_DISTANCE_CM) {
currentDistanceLeft = newDistance;
} else {
currentDistanceLeft = -1; // 重置為無效值
}
} else {
Serial.println("Left: Checksum error");
debugRawData(SerialSensorLeft, "Left");
currentDistanceLeft = -1; // 重置為無效值
}
} else {
Serial.println("Left: No complete data received");
debugRawData(SerialSensorLeft, "Left");
clearSerialBuffer(SerialSensorLeft);
currentDistanceLeft = -1; // 重置為無效值
}
// 處理Right感測器
SerialSensorRight.write(COM);
if (readSensorData(SerialSensorRight, buffer_RTTRight)) {
if (validateChecksum(buffer_RTTRight)) {
int newDistance = extractDistance(buffer_RTTRight);
if (newDistance >= 0 && newDistance <= MAX_DISTANCE_CM) {
currentDistanceRight = newDistance;
} else {
currentDistanceRight = -1; // 重置為無效值
}
} else {
Serial.println("Right: Checksum error");
debugRawData(SerialSensorRight, "Right");
currentDistanceRight = -1; // 重置為無效值
}
} else {
Serial.println("Right: No complete data received");
debugRawData(SerialSensorRight, "Right");
clearSerialBuffer(SerialSensorRight);
currentDistanceRight = -1; // 重置為無效值
}
// 在串口監視器中顯示所有距離
Serial.print("Left: ");
Serial.print(currentDistanceLeft);
Serial.print(" cm, Right: ");
Serial.print(currentDistanceRight);
Serial.print(" cm, Under: ");
Serial.print(currentDistanceUnder);
Serial.print(" cm, Front: ");
Serial.print(currentDistanceFront);
Serial.print(" cm, Back: ");
Serial.print(currentDistanceBack);
Serial.println(" cm");
// 每1秒刷新OLED顯示屏
if (millis() - lastRefreshTime >= OLED_REFRESH_MS) {
updateDisplay();
lastRefreshTime = millis();
}
// 非阻塞延時,確保感測器響應時間(14ms)
unsigned long start = millis();
while (millis() - start < 100) {
// 可在此添加其他任務
}
}
// 從感測器讀取數據
bool readSensorData(HardwareSerial &sensor, unsigned char *buffer) {
unsigned long startTime = millis();
while (sensor.available() < 4 && millis() - startTime < TIMEOUT_MS) {
// 等待數據或超時
}
if (sensor.available() >= 4) {
if (sensor.read() == 0xFF) {
buffer[0] = 0xFF;
for (int i = 1; i < 4; i++) {
buffer[i] = sensor.read();
}
return true;
} else {
return false; // 第一個字節不是0xFF
}
}
return false; // 數據不足
}
// 驗證校驗和
bool validateChecksum(unsigned char *buffer) {
uint8_t cs = buffer[0] + buffer[1] + buffer[2];
return buffer[3] == cs;
}
// 提取距離值(單位:厘米)
int extractDistance(unsigned char *buffer) {
int distance_mm = (buffer[1] << 8) + buffer[2]; // 原始距離(毫米)
return distance_mm / 10; // 轉換為厘米
}
// 更新OLED顯示屏,顯示所有感測器的距離
void updateDisplay() {
display.clearDisplay();
display.setTextSize(1);
// 顯示Left
display.setCursor(0, 0);
display.print("Left: ");
if (currentDistanceLeft == -1) {
display.print("N/A");
} else {
display.print(currentDistanceLeft);
display.print(" cm");
}
// 顯示Right
display.setCursor(0, 10);
display.print("Right: ");
if (currentDistanceRight == -1) {
display.print("N/A");
} else {
display.print(currentDistanceRight);
display.print(" cm");
}
// 顯示Under
display.setCursor(0, 20);
display.print("Under: ");
if (currentDistanceUnder == -1) {
display.print("N/A");
} else {
display.print(currentDistanceUnder);
display.print(" cm");
}
// 顯示Front
display.setCursor(0, 30);
display.print("Front: ");
if (currentDistanceFront == -1) {
display.print("N/A");
} else {
display.print(currentDistanceFront);
display.print(" cm");
}
// 顯示Back
display.setCursor(0, 40);
display.print("Back: ");
if (currentDistanceBack == -1) {
display.print("N/A");
} else {
display.print(currentDistanceBack);
display.print(" cm");
}
display.display();
}
// 清空串口緩衝區,防止數據堆積
void clearSerialBuffer(HardwareSerial &sensor) {
while (sensor.available()) {
sensor.read();
}
}
// 調試:顯示感測器的原始數據
void debugRawData(HardwareSerial &sensor, const char *sensorName) {
Serial.print(sensorName);
Serial.print(" Raw Data: ");
if (sensor.available()) {
while (sensor.available()) {
Serial.print(sensor.read(), HEX);
Serial.print(" ");
}
} else {
Serial.print("No data");
}
Serial.println();
}3