-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.ino
154 lines (145 loc) · 4.32 KB
/
http.ino
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
// Indoor Air Quality monitor
//
// Written by Michele <o-zone@zerozone.it> Pinassi
// Released under GPLv3 - No any warranty
String templateProcessor(const String& var)
{
//
// System values
//
if(var == "hostname") {
return String(config.hostname);
}
if(var == "fw_name") {
return String(FW_NAME);
}
if(var=="fw_version") {
return String(FW_VERSION);
}
if(var=="uptime") {
return String(millis()/1000);
}
if(var=="timedate") {
return String(timeClient.getFormattedTime());
}
//
// Config values
//
if(var=="wifi_essid") {
return String(config.wifi_essid);
}
if(var=="wifi_password") {
return String(config.wifi_password);
}
if(var=="wifi_rssi") {
return String(WiFi.RSSI());
}
if(var=="broker_host") {
return String(config.broker_host);
}
if(var=="broker_port") {
return String(config.broker_port);
}
if(var=="broker_timeout") {
return String(config.broker_timeout);
}
if(var=="client_id") {
return String(config.client_id);
}
if(var=="ota_enable") {
if(config.ota_enable) {
return String("checked");
} else {
return String("");
}
}
// Alarm values
if(var=="alarm_temp") {
return String(config.alarm_temp);
}
if(var=="alarm_hum") {
return String(config.alarm_hum);
}
if(var=="alarm_vocs") {
return String(config.alarm_vocs);
}
if(var=="alarm_hdex") {
return String(config.alarm_hdex);
}
//
return String();
}
// ************************************
// initWebServer
//
// initialize web server
// ************************************
void initWebServer() {
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html").setTemplateProcessor(templateProcessor);
server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request){
String message;
if(request->hasParam("wifi_essid", true)) {
strcpy(config.wifi_essid,request->getParam("wifi_essid", true)->value().c_str());
}
if(request->hasParam("wifi_password", true)) {
strcpy(config.wifi_password,request->getParam("wifi_password", true)->value().c_str());
}
if(request->hasParam("broker_host", true)) {
strcpy(config.broker_host,request->getParam("broker_host", true)->value().c_str());
}
if(request->hasParam("broker_port", true)) {
config.broker_port = atoi(request->getParam("broker_port", true)->value().c_str());
}
if(request->hasParam("broker_timeout", true)) {
config.broker_timeout = atoi(request->getParam("broker_timeout", true)->value().c_str());
}
if(request->hasParam("client_id", true)) {
strcpy(config.client_id, request->getParam("client_id", true)->value().c_str());
}
if(request->hasParam("hostname", true)) {
strcpy(config.hostname, request->getParam("hostname", true)->value().c_str());
}
if(request->hasParam("ota_enable", true)) {
config.ota_enable=true;
} else {
config.ota_enable=false;
}
//
if(request->hasParam("alarm_temp", true)) {
config.alarm_temp = atoi(request->getParam("alarm_temp", true)->value().c_str());
}
if(request->hasParam("alarm_hum", true)) {
config.alarm_hum = atoi(request->getParam("alarm_hum", true)->value().c_str());
}
if(request->hasParam("alarm_vocs", true)) {
config.alarm_vocs = atoi(request->getParam("alarm_vocs", true)->value().c_str());
}
if(request->hasParam("alarm_hdex", true)) {
config.alarm_hdex = atoi(request->getParam("alarm_hdex", true)->value().c_str());
}
//
saveConfigFile();
request->redirect("/?result=ok");
});
server.on("/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
ESP.restart();
});
server.on("/ajax", HTTP_POST, [] (AsyncWebServerRequest *request) {
String action,response="";
char outputJson[256];
if (request->hasParam("action", true)) {
action = request->getParam("action", true)->value();
if(action.equals("env")) {
serializeJson(env,outputJson);
response = String(outputJson);
}
}
request->send(200, "text/plain", response);
});
server.onNotFound([](AsyncWebServerRequest *request) {
Serial.printf("NOT_FOUND: ");
Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
request->send(404);
});
server.begin();
}