-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
219 lines (176 loc) · 5.07 KB
/
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
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
#include "server.h"
#include "rtc.h"
#include "voltage.h"
#include "motor.h"
#define SERVER_TIMEOUT 5 * 60 // seconds
const char *apName = "chicken-gate"; // Try http://esp8266.local
// DNS server
const byte DNS_PORT = 53;
DNSServer dnsServer;
// Web server
ESP8266WebServer server(80);
static boolean setupMode = true;
void handleRoot () {
Serial.println("Handle root");
// If caprive portal, redirect instead of displaying the page
if (captivePortal()) {
return;
}
String page = ROOT_PAGE;
server.sendHeader("Content-Length", String(page.length()));
server.send(200, "text/html", page);
}
void handleUpdateTime () {
char date[12] = {0};
char time[9] = {0};
for (uint8_t i = 0; i < server.args(); i++) {
if (server.argName(i) == "date") {
strcpy(date, server.arg(i).c_str());
}
if (server.argName(i) == "time") {
strcpy(time, server.arg(i).c_str());
}
}
RtcDateTime newDate = RtcDateTime(date, time);
Rtc.SetDateTime(newDate);
Serial.println("New date set:");
Serial.println(date);
Serial.println(time);
server.send(200, "text/plain", "Success");
}
void handleSetTimer () {
char date[12] = {0};
char time[9] = {0};
String mode;
for (uint8_t i = 0; i < server.args(); i++) {
if (server.argName(i) == "date") {
strcpy(date, server.arg(i).c_str());
}
if (server.argName(i) == "time") {
strcpy(time, server.arg(i).c_str());
}
if (server.argName(i) == "mode") {
mode = server.arg(i);
}
}
Serial.println(mode);
RtcDateTime alarmTime = RtcDateTime(date, time);
if (mode == "timerOne") {
DS3231AlarmOne alarm1(
alarmTime.Day(),
alarmTime.Hour(),
alarmTime.Minute(),
alarmTime.Second(),
DS3231AlarmOneControl_HoursMinutesSecondsMatch
);
Rtc.SetAlarmOne(alarm1);
Serial.println("New alarm 1 set:");
}
if (mode == "timerTwo") {
DS3231AlarmTwo alarm2(
alarmTime.Day(),
alarmTime.Hour(),
alarmTime.Minute(),
DS3231AlarmTwoControl_HoursMinutesMatch
);
Rtc.SetAlarmTwo(alarm2);
Serial.println("New alarm 2 set:");
}
Serial.println(date);
Serial.println(time);
server.send(200, "text/plain", "Success");
}
void handleDone () {
server.send(200, "text/plain", "Success");
setupMode = false;
}
void handleReadVoltage () {
float voltage = readVoltage();
char voltageString[10];
sprintf(voltageString, "%.2fV", voltage);
String payload = String("{") +
"\"voltage\":\"" + voltageString + "\"" +
"}";
server.send(200, "application/json", payload);
}
void handleReadTime () {
RtcDateTime now = Rtc.GetDateTime();
String timeString = String("") +
"Date:" + now.Year() + '/' + now.Month() + '/' + now.Day() + " " +
"Time:" + now.Hour() + ':' + now.Minute() + ':' + now.Second();
String payload = String("{") +
"\"time\":\"" + timeString + "\"" +
"}";
server.send(200, "application/json", payload);
}
void handleOpenNow () {
openGate();
server.send(200, "text/plain", "Success");
}
void handleCloseNow () {
closeGate();
server.send(200, "text/plain", "Success");
}
void handleNotFound () {
// If caprive portal, redirect instead of displaying the page
if (captivePortal()) {
return;
}
}
void startServer () {
WiFi.softAP(apName);
delay(500);
Serial.println("AP IP address: ");
Serial.println(WiFi.softAPIP());
/* Setup the DNS server redirecting all the domains to the apIP */
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
server.on("/", handleRoot);
server.on("/updateTime", handleUpdateTime);
server.on("/setTimer", handleSetTimer);
server.on("/readVoltage", handleReadVoltage);
server.on("/readTime", handleReadTime);
server.on("/open", handleOpenNow);
server.on("/close", handleCloseNow);
server.on("/done", handleDone);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
long int startTime = millis();
bool timeoutPassed = false;
while (setupMode && !timeoutPassed) {
dnsServer.processNextRequest();
server.handleClient();
if (millis() - startTime > SERVER_TIMEOUT * 1000) {
timeoutPassed = true;
}
}
}
boolean captivePortal() {
// Redirect to captive portal if we got a request for another domain
if (!isIp(server.hostHeader()) ) {
Serial.println("Request redirected to captive portal");
server.sendHeader("Location", String("http://") + toStringIp(server.client().localIP()), true);
server.send( 302, "text/plain", "");
server.client().stop(); // Empty content inhibits Content-length header so we have to close the socket ourselves.
return true;
}
return false;
}
boolean isIp(String str) {
for (size_t i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (c != '.' && (c < '0' || c > '9')) {
return false;
}
}
return true;
}
String toStringIp(IPAddress ip) {
String res = "";
for (int i = 0; i < 3; i++) {
res += String((ip >> (8 * i)) & 0xFF) + ".";
}
res += String(((ip >> 8 * 3)) & 0xFF);
return res;
}