-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.ino
231 lines (183 loc) · 5.6 KB
/
web.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
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
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
void setupServer() {
server.on("/config/input", handleConfigInput);
server.on("/config/mqtt", handleConfigMQTT);
server.on("/config/sensor", handleConfigSensor);
server.on("/config/wifi", handleConfigWifi);
server.on("/control", handleControl);
server.on("/debug/listen", handleListen);
server.on("/debug/query", handleQuery);
server.on("/restart", handleRestart);
server.on("/update", handleUpdate);
server.on("/generate_204", handleGenerate204);
server.begin();
}
void loopServer() {
server.handleClient();
}
void flushServer() {
server.client().flush();
schedule(true);
}
void handleGenerate204() {
server.send(204);
}
void handleConfigInput() {
const String typeArg = server.arg("type");
if (typeArg.length() == 0) {
String text = "type=";
text += inputTypeName(INPUT_TYPE::P10);
server.send(200, "text/plain", text.c_str());
return;
}
const INPUT_TYPE type = inputTypeByName(typeArg.c_str());
const boolean relayChanged = setActive(true);
double reading, voltage, resistance;
sensorRead(type, reading, voltage, resistance);
if (relayChanged) {
setActive(false);
}
String text = "value=";
text += to_string(resistance, 10, "%.2f");
text += "&type=";
text += inputTypeName(configInput(type, resistance));
for (uint8_t i = 0; i <= lastSensor; i++) {
const SENSOR sensor = static_cast<SENSOR>(i);
const double temperature = toTemperature(sensor, eichSensor(sensor), resistance);
text += "&";
text += sensorName(sensor);
text += "=";
text += to_string(temperature, 7, "%.2f");
}
server.send(200, "text/plain", text.c_str());
}
void handleConfigMQTT() {
const String serverArg = server.arg("server");
const String portArg = server.arg("port");
const String userArg = server.arg("user");
const String passArg = server.arg("pass");
config.mqtt = MQTTConfig(serverArg, portArg, userArg, passArg);
save();
server.send(204);
flushServer();
resetMQTT();
}
void handleConfigSensor() {
const String sensorArg = server.arg("sensor");
const String eichArg = server.arg("eich");
config.sensor = sensorByName(sensorArg.c_str());
config.eich = eichSensor(config.sensor);
double acc = config.eich.beta;
double count = 1;
int index = 0;
while (index < eichArg.length()) {
const int delimiter0 = eichArg.indexOf(' ', index);
assert(delimiter0 > 0);
int delimiter1 = eichArg.indexOf(' ', delimiter0 + 1);
if (delimiter1 < 0) {
delimiter1 = eichArg.length();
}
const double t = eichArg.substring(index, delimiter0).toDouble();
const double r = eichArg.substring(delimiter0 + 1, delimiter1).toDouble();
double weight;
const double beta = calcBeta(config.sensor, t, r, weight);
if (!isnan(beta) && weight >= 1) {
acc += beta * weight;
count += weight;
}
index = delimiter1 + 1;
}
config.eich.beta = acc / count;
save();
checkSoon = true;
const String text = inputTypeName(typeInput(config.sensor));
server.send(200, "text/plain", text.c_str());
}
void handleConfigWifi() {
const String ssidArg = server.arg("ssid");
const String passArg = server.arg("pass");
config.wifi = WifiConfig(ssidArg, passArg);
save();
server.send(204);
flushServer();
resetWifi();
}
void handleControl() {
for (int i = 0; i < server.args(); i++) {
const String key = server.argName(i);
const String value = server.arg(i);
if (value.length()) {
reconfig(key, value);
}
}
checkSoon = true;
save();
server.send(204);
}
void handleListen() {
checkSoon = true;
String text = "sensorReading=";
text += to_string(sensorReading, 7, "%.4f");
text += "&sensorVoltage=";
text += to_string(sensorVoltage, 6, "%.3f");
text += "&sensorResistance=";
text += to_string(sensorResistance, 10, "%.2f");
text += "&sensorTemperature=";
text += to_string(sensorTemperature, 7, "%.2f");
text += "&controlTemperature=";
text += to_string(controlTemperature, 7, "%.2f");
text += "&controlResistance=";
text += to_string(controlResistance, 10, "%.2f");
text += "&controlFrequency1=";
text += to_string(controlFrequency1, 7, "%.4f");
text += "&controlFrequency2=";
text += to_string(controlFrequency2, 7, "%.4f");
text += "&controlFrequency3=";
text += to_string(controlFrequency3, 7, "%.4f");
server.send(200, "text/plain", text.c_str());
}
void handleQuery() {
String text = "macSTA=";
text += WiFi.macAddress().c_str();
text += "&macAP=";
text += WiFi.softAPmacAddress().c_str();
text += "&version=";
text += VERSION;
text += "&version-data=";
text += VERSION_DATA;
text += "&wifi.ssid=";
text += config.wifi.ssid;
text += "&adccal.slope=";
text += to_string(config.adccal.slope, 7, "%.4f");
text += "&adccal.offset=";
text += to_string(config.adccal.offset, 8, "%.4f");
text += "&sensor=";
text += sensorName(config.sensor);
text += "&enabled=";
text += config.enabled ? "true" : "false";
text += "&noControlValue=";
text += to_string(config.noControlValue, 7, "%.2f");
text += "&controlValue=";
text += to_string(config.controlValue, 7, "%.2f");
text += "&nightValue=";
text += to_string(config.nightValue, 7, "%.2f");
text += "&slope=";
text += to_string(config.slope, 5, "%.2f");
text += "&mqtt.server=";
text += config.mqtt.server;
text += "&mqtt.port=";
text += config.mqtt.port;
text += "&mqtt.user=";
text += config.mqtt.user;
server.send(200, "text/plain", text.c_str());
}
void handleRestart() {
server.send(204);
flushServer();
ESP.restart();
}
void handleUpdate() {
updateSoon = true;
server.send(204);
}