-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_server.cpp
147 lines (115 loc) · 4.73 KB
/
web_server.cpp
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
#include "WString.h"
#include "wifi_manager.h"
#include "web_server.h"
#include "web_pages.h"
#include "mqtt_manager.h"
#include "DHT_manager.h"
ESP8266WebServer server(80);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_SERVER, GMT_OFFSET_SEC, NTP_UPDATE_INTERVAL);
String getFormattedTime(bool formatted) {
timeClient.update();
if (formatted) {
String timeWithZone = timeClient.getFormattedTime().substring(0, 5);
return timeWithZone;
} else {
return timeClient.getFormattedTime();
}
}
String applyHtmlTemplate(const String& title, const String& header, const String& content) {
String page = String((const __FlashStringHelper*)html_template);
String placeholders[] = { "%TITLE%", "%HEADER%", "%CONTENT%" };
String values[] = { title + (" / ") + projectName, header, content };
for (int i = 0; i < 3; i++) {
String placeholder = placeholders[i];
String value = values[i];
while (page.indexOf(placeholder) != -1) {
page.replace(placeholder, value);
}
}
page.replace("%HEADER%", header);
return page;
}
void setupWebServer() {
server.on("/", HTTP_GET, []() {
String content = applyHtmlTemplate("Home", projectName, String((const __FlashStringHelper*)index_html));
server.send(200, "text/html", content);
});
server.on("/monitoring", HTTP_GET, []() {
server.send(200, "text/html", applyHtmlTemplate("Monitoring", projectName, String((const __FlashStringHelper*)monitoring_html)));
});
server.on("/sensor-data", HTTP_GET, []() {
float temperature = readDHTTemperature();
float humidity = readDHTHumidity();
String currentTime = getFormattedTime(false);
String jsonResponse = "{";
jsonResponse += "\"temperature\":";
if (temperature != -999.0) {
jsonResponse += String(temperature);
} else {
jsonResponse += "\"Error\"";
}
jsonResponse += ",\"humidity\":";
if (humidity != -999.0) {
jsonResponse += String(humidity);
} else {
jsonResponse += "\"Error\"";
}
jsonResponse += ",\"timeServer\":\"" + currentTime + "\"";
jsonResponse += "}";
server.send(200, "application/json", jsonResponse);
});
server.on("/device-info", HTTP_GET, []() {
String currentTime = getFormattedTime(true);
String jsonResponse = "{";
jsonResponse += "\"timeServer\":\"" + currentTime + "\",";
jsonResponse += "\"freeMemory\":" + String(ESP.getFreeHeap() / 1024) + ",";
jsonResponse += "\"wifiSSID\":\"" + WiFi.SSID() + "\",";
jsonResponse += "\"wifiSignalStrength\":" + String(WiFi.RSSI()) + ",";
jsonResponse += "\"deviceIP\":\"" + WiFi.localIP().toString() + "\"";
jsonResponse += "}";
server.send(200, "application/json", jsonResponse);
});
server.on("/config", HTTP_GET, []() {
String content = String((const __FlashStringHelper*)config_html);
content.replace("%MQTT_BROKER%", String(mqttConfig.broker));
content.replace("%MQTT_PORT%", String(mqttConfig.port));
content.replace("%MQTT_TOPIC%", String(mqttConfig.topic));
content.replace("%MQTT_USERNAME%", String(mqttConfig.username));
content.replace("%MQTT_PASSWORD%", String(mqttConfig.password));
server.send(200, "text/html", applyHtmlTemplate("MQTT Configuration", projectName, content));
});
server.on("/config", HTTP_POST, []() {
String broker = server.arg("mqtt_broker");
uint16_t port = server.arg("mqtt_port").toInt();
String topic = server.arg("mqtt_topic");
String username = server.arg("mqtt_username");
String password = server.arg("mqtt_password");
if (broker.length() > 0 && topic.length() > 0) {
strncpy(mqttConfig.broker, broker.c_str(), sizeof(mqttConfig.broker));
mqttConfig.port = port;
strncpy(mqttConfig.topic, topic.c_str(), sizeof(mqttConfig.topic));
strncpy(mqttConfig.username, username.c_str(), sizeof(mqttConfig.username));
strncpy(mqttConfig.password, password.c_str(), sizeof(mqttConfig.password));
saveMQTTConfig();
String content = applyHtmlTemplate("MQTT Configuration", projectName, String((const __FlashStringHelper*)config_post_html));
server.send(200, "text/html", content);
delay(1000);
ESP.restart();
}
});
server.on("/reset", HTTP_GET, []() {
String content = applyHtmlTemplate("Reset Configuration", projectName, String((const __FlashStringHelper*)reset_html));
server.send(200, "text/html", content);
});
server.on("/reset", HTTP_POST, []() {
String content = applyHtmlTemplate("Reset Configuration", projectName, String((const __FlashStringHelper*)reset_post_html));
content.replace("%DEFAULT_PASSWORD%", String(defaultPassword));
server.send(200, "text/html", content);
delay(1000);
wifiManager.resetSettings();
resetMQTTConfig();
ESP.restart();
});
server.begin();
}