-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx80_server.ino
65 lines (54 loc) · 1.69 KB
/
x80_server.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
ESP8266WebServer server(80);
#define RESPONSE_START() String __response = ""
#define RESPONSE_ADD(_key,_value) __response += _key "="; __response += _value; __response += "\n"
#define RESPONSE_END(_code,_content) server.send(_code, _content, __response)
void serverHandleRoot() {
RESPONSE_START();
RESPONSE_ADD("id", chipId);
RESPONSE_ADD("mac", WiFi.macAddress());
RESPONSE_ADD("temp", tempCurrent);
RESPONSE_ADD("tempOrig", tempUnadjusted);
RESPONSE_ADD("tempAge", tempGetAge());
RESPONSE_ADD("tempA", tempConfig.a);
RESPONSE_ADD("tempB", tempConfig.b);
RESPONSE_END(200, "text/plain");
}
void serverGetTemp() {
String response = "{\"temperature\":";
response += tempCurrent;
response += ",\"age\":";
response += tempGetAge();
response += "}\n";
server.send(200, "application/json", response);
}
void serverGetTempRaw() {
String response = "";
response += tempCurrent;
server.send(200, "text/plain", response);
}
void serverSetConfig() {
if (!server.hasArg("a") || !server.hasArg("b")) {
server.send(400, "text/plain", "Missing a and b params.\n");
return;
}
long aInt = server.arg("a").toInt();
long bInt = server.arg("b").toInt();
if (aInt == 0) {
server.send(400, "text/plain", "Param a shouldn't be zero.\n");
return;
}
float a = (float)aInt / 1000.0;
float b = (float)bInt / 1000.0;
tempSetConfig(a, b);
RESPONSE_START();
RESPONSE_ADD("tempA", tempConfig.a);
RESPONSE_ADD("tempB", tempConfig.b);
RESPONSE_END(200, "text/plain");
}
void serverSetup() {
server.on("/", serverHandleRoot);
server.on("/temp", serverGetTemp);
server.on("/tempRaw", serverGetTempRaw);
server.on("/setConfig", serverSetConfig);
server.begin();
}