-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.ino
211 lines (194 loc) · 5.51 KB
/
webserver.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
/* Copyright (C) 2009 Nagy Attila Gabor <[email protected]>
*
* This file is part of AHC - AHC Humidity Control.
*
* AHC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AHC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AHC. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Handle webserver codes
*/
// Prepared pieces of code
static const char okHeader[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html; charset=utf8\r\n"
"Pragma: no-cache\r\n"
;
static const char redirect[] PROGMEM =
"HTTP/1.0 302 found\r\n"
"Location: /\r\n"
"\r\n"
;
static const char htmlHeader[] PROGMEM =
"<!DOCTYPE HTML>\n"
"<html>"
"<head>"
"<title>parologtato v0.2</title>"
"</head>"
"<body>"
;
static const char htmlFooter[] PROGMEM =
"</body></html>"
;
/**
* Service any request on the webserver
* @param BufferFiller buf the buffer filler to be used for output
* @param word pos Position in the ethernet packet
*/
void serviceHttpRequest(BufferFiller& buf, const word pos) {
buf = ether.tcpOffset();
char * data = (char *) Ethernet::buffer + pos;
#if DSERIAL
Serial.println(data);
#endif
// Check the input and prepare the returned content
if (strncmp("GET / HTTP", data, 10) == 0)
homePage(buf);
else if (strncmp("GET /?io=", data, 8) == 0)
updateIO(buf, data);
else if (strncmp("GET /?min=", data, 9) == 0)
updateTarget(buf, data);
else
send404(buf);
// send web page data
ether.httpServerReply(buf.position());
}
/**
* Display a radio button with a label
* @param BufferFiller buf the buffer filler to be used for output
* @param const char * label label for the radio button
* @param const char * name name of the radio button
* @param const char * value value of the radio button
* @param byte state wether the radio button should be marked on or off
*/
void printRadio(BufferFiller& buf, const char* label, const char* name, const char * value, const byte state) {
buf.emit_p(PSTR(
"<label>$F<input type=\"radio\" name=\"$F\" value=\"$F\""
), label, name, value);
if (state) {
buf.emit_p(PSTR(" checked"));
}
buf.emit_p(PSTR(" onchange=\"this.form.submit()\"></label> "));
}
/**
* Display the main page
* @param BufferFiller buf the buffer used to write to the output
*/
void homePage(BufferFiller& buf) {
buf.emit_p(PSTR("$F\r\n"
"$F"
"<h1>Párologtató vezérlés</h1>"
"<form>"
), okHeader, htmlHeader);
printRadio(
buf,
PSTR("Auto:"),
PSTR("io"),
PSTR("2"),
IOMode == IOMODE_AUTO
);
printRadio(
buf,
PSTR("Be:"),
PSTR("io"),
PSTR("1"),
IOMode == IOMODE_ON
);
printRadio(
buf,
PSTR("Ki:"),
PSTR("io"),
PSTR("0"),
IOMode == IOMODE_OFF
);
buf.emit_p(PSTR(
"</form>"));
buf.emit_p(PSTR("<form>"
"<h2>elvárt páratartalom:</h2>"
"<p><label>min: <input name=\"min\" size=\"2\" maxlength=\"2\" type=\"number\" min=\"20\" max=\"90\" pattern=\"[0-9]{2}\" value=\"$D\">%</label> "
"<label>max: <input name=\"max\" size=\"2\" maxlength=\"2\" type=\"number\" min=\"20\" max=\"90\" pattern=\"[0-9]{2}\" value=\"$D\">%</label></p>"
"<input type=\"submit\" value=\"ok\">"
"</form>$F"), targetHumidity_min, targetHumidity_max, htmlFooter);
}
int getIntArg(const char* data, const char* key, int value =-1) {
char temp[10];
if (ether.findKeyVal(data + 6, temp, sizeof temp, key) > 0)
value = atoi(temp);
return value;
}
/**
* Update the target humidity value
*
* @param BufferFiller buf the buffer used to write to the output
* @param const char* data the incoming tcp data
*/
void updateTarget(BufferFiller& buf, const char* data) {
byte min = getIntArg(data, "min");
byte max = getIntArg(data, "max");
#if DSERIAL
Serial.print(F("Received new target value, min: "));
Serial.print(min, DEC);
Serial.print(F(" max: "));
Serial.println(max, DEC);
#endif
if (checkTargetHumidity(min, max)) {
targetHumidity_min = min;
targetHumidity_max = max;
IOController(true);
saveSettings();
}
buf.emit_p(redirect);
}
/**
* Check the target humidity values for correctness
* @param byte min the minimum value
* @param byte max the maximum value
* @return boolean true if the values are correct
*/
boolean checkTargetHumidity(const byte min, const byte max) {
if (min<20) return false;
if (max>90) return false;
if (min>max) return false;
return true;
}
/**
* Check the io state and update it
*
* @param BufferFiller buf the buffer used to write to the output
* @param const char* data the incoming tcp data
*/
void updateIO(BufferFiller& buf, const char* data) {
byte d = getIntArg(data, "io");
#if DSERIAL
Serial.print(F("Received new io state"));
Serial.println(d, DEC);
#endif
if (d>=0 && d<=2) {
IOMode = d;
IOController(true);
saveSettings();
}
buf.emit_p(redirect);
}
/**
* Send a 404 reply
* @param BufferFiller buf the buffer used to write to the output
*/
void send404(BufferFiller& buf) {
buf.emit_p(PSTR(
"HTTP/1.0 404 Not found\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<h1>404 Not found</h1>"));
}