-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_server.cpp
More file actions
72 lines (63 loc) · 1.95 KB
/
web_server.cpp
File metadata and controls
72 lines (63 loc) · 1.95 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
#include <Arduino.h>
#include <WebServer.h>
#include <Preferences.h>
#include "modbus_rtu.h"
#include "modbus_scan.h"
#include "mstp_scan/mstp_scan.h"
WebServer server(80);
void handleModbusConfig() {
if (server.hasArg("baud")) {
int baud = server.arg("baud").toInt();
if (baud >= 1200 && baud <= 115200) {
Preferences prefs;
prefs.begin("modbus", false);
prefs.putInt("baud", baud);
prefs.end();
server.send(200, "text/plain", "Baud rate set. Rebooting...");
delay(500);
ESP.restart();
} else {
server.send(400, "text/plain", "Invalid baud rate");
}
}
}
void handleRS485Protocol() {
if (server.hasArg("protocol")) {
String proto = server.arg("protocol");
if (proto == "modbus" || proto == "bacnet") {
Preferences prefs;
prefs.begin("rs485", false);
prefs.putString("protocol", proto);
prefs.end();
server.send(200, "text/plain", "Protocol set. Rebooting...");
delay(500);
ESP.restart();
} else {
server.send(400, "text/plain", "Invalid protocol selection");
}
}
}
void handleModbusScan() {
server.send(200, "text/plain", "Modbus scan started. Check serial log.");
scanModbusRTUDevices();
}
void handleMSTPScan() {
server.send(200, "text/plain", "BACnet MS/TP scan started. Check serial log.");
startMSTPScan();
}
void handleRoot() {
server.send(200, "text/html", "<html><body><h1>BAS-ESP32 Web Server</h1></body></html>");
}
void startWebServer() {
server.on("/", handleRoot);
server.on("/modbus-config", HTTP_POST, handleModbusConfig);
server.on("/rs485-protocol", HTTP_POST, handleRS485Protocol);
server.on("/start-modbus-scan", HTTP_POST, handleModbusScan);
server.on("/start-mstp-scan", HTTP_POST, handleMSTPScan);
server.on("/start-ip-scan", HTTP_POST, []() {
server.send(200, "text/plain", "IP scan started. Check serial log.");
scanIPNetwork();
});
server.begin();
Serial.println("Web server started.");
}