forked from tbagro/Arra-oadorWemosD1min
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
282 lines (244 loc) · 11.2 KB
/
server.h
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//monta a pagina do webserver
#include "horaNTP.h" // busca hora NTP
#include "runanyway.h" // função geral para acionamentos dos pinos
//******************** botões comandos *******************//
uint8_t status_gpio = 0; // Define condição para GPIO
uint8_t status_auto; // Define status do botão auto
//******************** Função Temporizada *******************//
int horaLiga = 7, horaLiga1 = 12, horaLiga2 = 18, minutoLiga = 10;// define os horarios de acionamento
//uint8_t pin1 = D1, pin2 = D2, pin3 = D3;
int tempoD1, tempoD2, tempoD3;// tempo de acionamento dos pinos
String time1() {//
char time1[10];
sprintf( time1, "%d", (tempoD1 ));
return time1;
}
String time2() {
char time2[10];
sprintf( time2, "%d", (tempoD2));
return time2;
}
String time3() {
char time3[10];
sprintf( time3, "%d", (tempoD3));
return time3;
}
String time4() {
char time1[10];
sprintf( time1, "%d", minutoLiga);
return time1;
}
String time5() {
char time1[10];
sprintf( time1, "%d", horaLiga);
return time1;
}
String horaligar () {//
char horaligar[30];
sprintf( horaligar, "%02d:%02d ", horaLiga, minutoLiga); //
return horaligar;
}
String horaligar1 () {//
char horaligar1[30];
sprintf( horaligar1, ",%02d:%02d ", horaLiga1, minutoLiga); //
return horaligar1;
}
String horaligar2 () {//
char horaligar2[30];
sprintf( horaligar2, ",%02d:%02d ", horaLiga2, minutoLiga); //
return horaligar2;
}
// ------------------------------ - HTML------------------------ -
void TimedAction() { //executa
if (status_auto == true) {
if (((int(hour()) == (int)horaLiga) || (int(hour()) == (int)horaLiga1) || (int(hour()) == (int)horaLiga2)) && int(minute()) == (int)minutoLiga && (second() <= 2)) {
status_gpio = 1;
digitalWrite(BUILTIN_LED, 1); // all LEDs off to start
runAndWait(D5, status_gpio, tempoD1);
runAndWait(D7, status_gpio, tempoD2);
runAndWait(D8, status_gpio, tempoD3);
status_gpio = 0;
}
}
if (status_auto == true) {
if ((int(hour()) == 22) && int(minute()) == (int)minutoLiga && (second() <= 2)) {
status_gpio = 1;
digitalWrite(BUILTIN_LED, 1); // all LEDs off to start
runAndWait(D5, status_gpio, tempoD1/2);
runAndWait(D7, status_gpio, tempoD2/2);
runAndWait(D8, status_gpio, tempoD3/2);
status_gpio = 0;
}
}
}
#include "eeprom.h"
// ------------------------------ - HTML------------------------ -
WiFiServer server(80);
String HTMLHeader() {//monta o cabeçalho da pagina
String h = "HTTP/1.1 200 OK\r\n";
h += "Content-Type: text/html\r\n\r\n";
h += "<!DOCTYPE HTML><html><head><META HTTP-EQUIV='Refresh' CONTENT='10;URL=/'><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>\r\n";
h += "<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css' rel='stylesheet'></link><link rel=\"stylesheet\" href=\"http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css\"><center><body>\n";
h += "<link rel=\"stylesheet\" href=\"http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css\">";
h += "<title>WebServer ESP8266</title>";
h += "<style>.c{text-align: center;} div,input{padding:5px;font-size:1em;} input{width:80%;} body{text-align: center;font-family:verdana;}</style>";
h += "</head>";
h += "<body><div class=\"panel panel-primary\">";
h += "<div class=\"panel-heading\"><h3>Simple Webserver</h3></div>";
h += "<div class=\"panel-body\">";
h += "<div id=\"txt\" style=\"font-weight:bold;\"></div>";
return h;
}
String HTMLFooter() { // monta o rodapé da pagina
String f = "<BR><table width=\"100%\" bgcolor=\"black\" cellpadding=\"12\" border=\"0\">";
f += "<tr><td><p style = \"color: white; background: black;font-size: 0.8em; font-weight: bold; text-align: center; margin: 0px 10px 0px 10px;\">";
f += "Tiago Batista © 2017</p></td></tr>";
f += "</table></body></center></html>";
return f;
}
String HTMLPage() {// monta o corpo da pagina html
String p = "</p><div class='container'>";
p += "<h4>Wemos D1mini</h4>";
p += "<div class='btn-group'>";
p += "<BR><table><tr>";//cria tabela
//alterna os Botões de alimentação automatica para ON ou OFF exibindo apenas um botão na pagina
p += (status_auto) ? "<td><a href=\"/Auto/off\" class='btn btn-primary'>Auto ON <i class=\"fa fa-toggle-on\" aria-hidden=\"true\"></i></a>" : "<td><a href=\"/Auto/on\" class='btn btn-success'>Auto OFF <i class=\"fa fa-toggle-off\" aria-hidden=\"true\"></i></a>";
//De acordo com o status da GPIO
TimedAction();// verifica o horario de acionamento pré definido
//alterna os botões de alimentação manual para ON ou OFF
p += (status_gpio) ? "<a href=\"/rele/off\" class='btn btn-danger'><i class=\"fa fa-power-off\" aria-hidden=\"true\"></i> Desligar</a></td>" : "<a href=\"/rele/on\" class='btn btn-success'><i class=\"fa fa-power-off\" aria-hidden=\"true\"></i> Ligar</a></td>";
p += "</tr></table></div><BR>";// encerra tabela
p += (F("<div class='btn-group'><p>"));
p += "<BR><table><tr>";//exibe na pagina do webserver os horarios pré definidos para acionamento
p += (F("<td><span><a href=\"/tempoD1/u\"><button type='button' class='btn btn-info' style='margin: 5px'>D1 Up</button></a>"));
p += "<strong style='font-size:16px;'>" + time1() + " ms</strong>";//armazena na eeprom;// exibe o delay
p += (F("</span><a href=\"/tempoD1/d\"; URL=/'><button type='button' class='btn btn-info' style='margin: 5px'>D1 down</button></a></td>"));
p += "</tr><tr>";
p += (F("<td><span><a href=\"/tempoD2/u\"><button type='button' class='btn btn-info' style='margin: 5px'>D2 Up</button></a>"));
p += "<strong style='font-size:16px;'>" + time2() + " ms</strong>";// exibe o delay
p += (F("</span><a href=\"/tempoD2/d\"><button type='button' class='btn btn-info' style='margin: 5px'>D2 down</button></a></td>"));
p += "</tr><tr>";
p += (F("<td><span><a href=\"/tempoD3/u\"><button type='button' class='btn btn-info' style='margin: 5px'>D3 Up</button></a>"));
p += "<strong style='font-size:16px;'>" + time3() + " ms</strong>";// exibe o delay
p += (F("</span><a href=\"/tempoD3/d\"><button type='button' class='btn btn-info' style='margin: 5px'>D3 down</button></a></td>"));
p += "</tr></table>";
// //horaliga
// p += "<td><a href = \"/hora/d\" class='btn btn-default'>Down<i class=\"fa fa-angle-down\" aria-hidden=\"true\"></i></a>\n";
// p += "<strong style='font-size:16px;'>hora " + time5() + "</strong>"; // exibe o delay
// p += "<a href = \"/hora/u\" class='btn btn-default'>Up<i class=\"fa fa-angle-up\" aria-hidden=\"true\"></i></a></td>\n";
// //minutoliga
// p += "<td><a href = \"/min/d\" class='btn btn-default'>Down<i class=\"fa fa-angle-down\" aria-hidden=\"true\"></i></a>\n";
// p += "<strong style='font-size:16px;'>min" + time4() + "</strong>"; // exibe o delay
// p += "<a href = \"/min/u\" class='btn btn-default'>Up<i class=\"fa fa-angle-up\" aria-hidden=\"true\"></i></a></td>\n";
p += "</p></div>";//container
p += "</div> ";
//reset
p += (F("<a href=\"/reset\"><button type='button' class='btn btn-info' style='margin: 5px'>reset</button></a><BR>"));
//************************************
p += "<p>last update "; // DIV para hora
p += String(hora ());
p += "<p>Programado para ligar às <span class=\"label label-success\">";
p += horaligar();
p += horaligar1();
p += horaligar2();
p += " horas</span></p><BR>";
return p;
}
void webpage() {// monta o webserver
// ------------------------------ - server ------------------------ -
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println(F("*WifiRTC: Nova conexao requisitada..."));
while (!client.available()) {
delay(1);
}
Serial.println (F("*WifiRTC: Nova conexao OK..."));
String req = client.readStringUntil('\r'); //Le a string enviada pelo cliente
Serial.println(req); //Mostra a string enviada
client.flush(); //Limpa dados/pfer
TimedAction(); //verifica as condições da eeprom
// unsigned long currentMillis = millis();
if (req.indexOf(F("Auto/on")) != -1) {// verifica se o botão html é igual a ON
status_auto = true;// ativa o modo automatico
} else if (req.indexOf(F("Auto/off")) != -1) {// verifica se o botão html é igual a OFF
status_auto = false;//desativa o modo automatico
} else if (req.indexOf(F("rele/on")) != -1) { //verifica se o botão html é igual a ON
status_gpio = 1;// Muda botão ligar para ON
digitalWrite(BUILTIN_LED, 1); // all LEDs off to start
runAndWait(D5, status_gpio, tempoD1);//função para acionar os pinos no tempo definido
runAndWait(D7, status_gpio, tempoD2);
runAndWait(D8, status_gpio, tempoD3);
status_gpio = 0;
} else if (req.indexOf(F("rele/off")) != -1) {
// stateRelay = false;
status_gpio = 0; //Muda botão ligar para OFF
} else if (req.indexOf(F("tempoD1/u")) != -1) {// aumenta o valor do tempo D1
tempoD1 = tempoD1 + 20;
if (tempoD1 > 1000) {
tempoD1 = 0;
}
} else if (req.indexOf(F("tempoD1/d")) != -1) {// diminui o valor do tempo D1
tempoD1 = tempoD1 - 20;
if (tempoD1 < 0) {
tempoD1 = 1000;
}
} else if (req.indexOf(F("tempoD2/u")) != -1) {
tempoD2 = tempoD2 + 20;
if (tempoD2 > 1000) {
tempoD2 = 0;
}
} else if (req.indexOf(F("tempoD2/d")) != -1) {
tempoD2 = tempoD2 - 20;
if (tempoD2 < 0) {
tempoD2 = 1000;
}
} else if (req.indexOf(F("tempoD3/u")) != -1) {
tempoD3 = tempoD3 + 20;
if (tempoD3 > 1000) {
tempoD3 = 0;
}
} else if (req.indexOf(F("tempoD3/d")) != -1) {
tempoD3 = tempoD3 - 20;
if (tempoD3 < 0) {
tempoD3 = 1000;
}
} else if (req.indexOf(F("/reset")) != -1) {
ESP.restart();//
} //
//else if (req.indexOf(F("/hora/d")) != -1) { // controle do horario de acionamento
// horaLiga = horaLiga - 1;
// if (horaLiga < 0) {
// horaLiga = 23;
// }
// } else if (req.indexOf(F("/hora/u")) != -1) {
// horaLiga = horaLiga + 1;
// if (horaLiga > 23) {
// horaLiga = 00;
// }
// } else if (req.indexOf(F("/min/u")) != -1) {
// minutoLiga = minutoLiga + 1;
// if (minutoLiga > 59) {
// minutoLiga = 00;
// }
// } else if (req.indexOf(F("/min/d")) != -1) {
// minutoLiga = minutoLiga - 1;
// if (minutoLiga < 0) {
// minutoLiga = 59;
// }
// }
else {
// Serial.println(F("*WifiRTC: Requisicao invalida"));
}
Save_Data();//armazena na eeprom
//Prepara a resposta para o cliente e carrega a pagina
//Monta a pagina HTML
String r = HTMLHeader(); // monta o cabeçalho
r += HTMLPage(); //monta o corpo da pagina
r += HTMLFooter(); // monta rodapé
client.print(r); // send page to clients browser
client.flush();
client.stop();
//Serial.println(F("*WifiRTC: Cliente desconectado!"));
}